A command-line argument to transform file names to *.java

This commit is contained in:
Andrey Breslav
2012-01-30 22:01:27 +04:00
parent 73100134f9
commit 5595e55ab9
10 changed files with 67 additions and 11 deletions
+1
View File
@@ -73,6 +73,7 @@
<arg value="-output"/> <arg value="-output"/>
<arg value="${output}/classes/jdk-headers"/> <arg value="${output}/classes/jdk-headers"/>
<arg value="-ignoreErrors"/> <arg value="-ignoreErrors"/>
<arg value="-transformNamesToJava"/>
</java> </java>
</target> </target>
@@ -11,7 +11,6 @@ import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.psi.JetDeclarationWithBody; import org.jetbrains.jet.lang.psi.JetDeclarationWithBody;
import org.jetbrains.jet.lang.psi.JetElement; import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetExpression; import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetFunctionLiteralExpression;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
@@ -112,7 +111,7 @@ public class ClosureCodegen extends ObjectOrClosureCodegen {
funClass, funClass,
new String[0] new String[0]
); );
cv.visitSource(fun.getContainingFile().getName(), null); cv.visitSource(state.transformFileName(fun.getContainingFile().getName()), null);
generateBridge(name, funDescriptor, fun, cv); generateBridge(name, funDescriptor, fun, cv);
@@ -10,6 +10,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.util.containers.Stack; import com.intellij.util.containers.Stack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethods; import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethods;
import org.jetbrains.jet.compiler.FileNameTransformer;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory; import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor; import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
@@ -33,11 +34,18 @@ public class GenerationState {
private final JetStandardLibrary standardLibrary; private final JetStandardLibrary standardLibrary;
private final IntrinsicMethods intrinsics; private final IntrinsicMethods intrinsics;
private final FileNameTransformer fileNameTransformer;
public GenerationState(Project project, ClassBuilderFactory builderFactory) { public GenerationState(Project project, ClassBuilderFactory builderFactory) {
this(project, builderFactory, FileNameTransformer.IDENTITY);
}
public GenerationState(Project project, ClassBuilderFactory builderFactory, FileNameTransformer fileNameTransformer) {
this.project = project; this.project = project;
this.standardLibrary = JetStandardLibrary.getJetStandardLibrary(project); this.standardLibrary = JetStandardLibrary.getJetStandardLibrary(project);
this.factory = new ClassFileFactory(builderFactory, this); this.factory = new ClassFileFactory(builderFactory, this);
this.intrinsics = new IntrinsicMethods(project, standardLibrary); this.intrinsics = new IntrinsicMethods(project, standardLibrary);
this.fileNameTransformer = fileNameTransformer;
} }
@NotNull @NotNull
@@ -186,4 +194,9 @@ public class GenerationState {
return answer.toString(); return answer.toString();
} }
@NotNull
public String transformFileName(@NotNull String fileName) {
return fileNameTransformer.transformFileName(fileName);
}
} }
@@ -14,7 +14,6 @@ import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
import org.objectweb.asm.commons.InstructionAdapter; import org.objectweb.asm.commons.InstructionAdapter;
import org.objectweb.asm.commons.Method; import org.objectweb.asm.commons.Method;
@@ -91,7 +90,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
signature.getSuperclassName(), signature.getSuperclassName(),
signature.getInterfaces().toArray(new String[0]) signature.getInterfaces().toArray(new String[0])
); );
v.visitSource(myClass.getContainingFile().getName(), null); v.visitSource(state.transformFileName(myClass.getContainingFile().getName()), null);
ClassDescriptor container = getContainingClassDescriptor(descriptor); ClassDescriptor container = getContainingClassDescriptor(descriptor);
if(container != null) { if(container != null) {
@@ -41,7 +41,7 @@ public class NamespaceCodegen {
new String[0] new String[0]
); );
// TODO figure something out for a namespace that spans multiple files // TODO figure something out for a namespace that spans multiple files
v.visitSource(sourceFile.getName(), null); v.visitSource(state.transformFileName(sourceFile.getName()), null);
} }
public void generate(JetFile file) { public void generate(JetFile file) {
@@ -54,7 +54,7 @@ public class TraitImplBodyCodegen extends ClassBodyCodegen {
"java/lang/Object", "java/lang/Object",
new String[0] new String[0]
); );
v.visitSource(myClass.getContainingFile().getName(), null); v.visitSource(state.transformFileName(myClass.getContainingFile().getName()), null);
} }
private String jvmName() { private String jvmName() {
@@ -0,0 +1,19 @@
package org.jetbrains.jet.compiler;
import org.jetbrains.annotations.NotNull;
/**
* @author abreslav
*/
public interface FileNameTransformer {
FileNameTransformer IDENTITY = new FileNameTransformer() {
@NotNull
@Override
public String transformFileName(@NotNull String fileName) {
return fileName;
}
};
@NotNull
String transformFileName(@NotNull String fileName);
}
@@ -2,8 +2,10 @@ package org.jetbrains.jet.cli;
import com.sampullara.cli.Args; import com.sampullara.cli.Args;
import com.sampullara.cli.Argument; import com.sampullara.cli.Argument;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.compiler.CompileEnvironment; import org.jetbrains.jet.compiler.CompileEnvironment;
import org.jetbrains.jet.compiler.CompileEnvironmentException; import org.jetbrains.jet.compiler.CompileEnvironmentException;
import org.jetbrains.jet.compiler.FileNameTransformer;
import java.io.PrintStream; import java.io.PrintStream;
@@ -13,6 +15,14 @@ import java.io.PrintStream;
*/ */
@SuppressWarnings("UseOfSystemOutOrSystemErr") @SuppressWarnings("UseOfSystemOutOrSystemErr")
public class KotlinCompiler { public class KotlinCompiler {
private static final FileNameTransformer ANY_EXTENSION_TO_JAVA = new FileNameTransformer() {
@NotNull
@Override
public String transformFileName(@NotNull String fileName) {
return fileName.replaceFirst("\\.\\w+$", ".java");
}
};
private KotlinCompiler() { private KotlinCompiler() {
} }
@@ -40,6 +50,9 @@ public class KotlinCompiler {
@Argument(value = "ignoreErrors", description = "Emit byte code even if there are compilation errors (not recommended)") @Argument(value = "ignoreErrors", description = "Emit byte code even if there are compilation errors (not recommended)")
public boolean ignoreErrors; public boolean ignoreErrors;
@Argument(value = "transformNamesToJava", description = "Transform Kotlin file names to *.java. This option is needed for compiling kotlinized Java library headers")
public boolean transformNamesToJava;
} }
private static void usage(PrintStream target) { private static void usage(PrintStream target) {
@@ -79,7 +92,7 @@ public class KotlinCompiler {
return 0; return 0;
} }
CompileEnvironment environment = new CompileEnvironment(); CompileEnvironment environment = new CompileEnvironment(arguments.transformNamesToJava ? ANY_EXTENSION_TO_JAVA : FileNameTransformer.IDENTITY);
environment.setIgnoreErrors(arguments.ignoreErrors); environment.setIgnoreErrors(arguments.ignoreErrors);
if (arguments.stdlib != null) { if (arguments.stdlib != null) {
@@ -35,17 +35,23 @@ public class CompileEnvironment {
private JetCoreEnvironment myEnvironment; private JetCoreEnvironment myEnvironment;
private final Disposable myRootDisposable; private final Disposable myRootDisposable;
private PrintStream myErrorStream = System.out; private PrintStream myErrorStream = System.out;
private final FileNameTransformer myFileNameTransformer;
private URL myStdlib; private URL myStdlib;
private boolean ignoreErrors = false; private boolean ignoreErrors = false;
public CompileEnvironment() { public CompileEnvironment() {
this(FileNameTransformer.IDENTITY);
}
public CompileEnvironment(FileNameTransformer fileNameTransformer) {
myRootDisposable = new Disposable() { myRootDisposable = new Disposable() {
@Override @Override
public void dispose() { public void dispose() {
} }
}; };
myEnvironment = new JetCoreEnvironment(myRootDisposable); myEnvironment = new JetCoreEnvironment(myRootDisposable);
myFileNameTransformer = fileNameTransformer;
} }
public void setErrorStream(PrintStream errorStream) { public void setErrorStream(PrintStream errorStream) {
@@ -148,7 +154,7 @@ public class CompileEnvironment {
} }
public List<Module> loadModuleScript(String moduleFile) { public List<Module> loadModuleScript(String moduleFile) {
CompileSession scriptCompileSession = new CompileSession(myEnvironment); CompileSession scriptCompileSession = new CompileSession(myEnvironment, myFileNameTransformer);
scriptCompileSession.addSources(moduleFile); scriptCompileSession.addSources(moduleFile);
ensureRuntime(); ensureRuntime();
@@ -289,7 +295,7 @@ public class CompileEnvironment {
} }
public boolean compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime) { public boolean compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime) {
CompileSession session = new CompileSession(myEnvironment); CompileSession session = new CompileSession(myEnvironment, myFileNameTransformer);
session.addSources(sourceFileOrDir); session.addSources(sourceFileOrDir);
String mainClass = null; String mainClass = null;
@@ -29,6 +29,7 @@ public class CompileSession {
private final JetCoreEnvironment myEnvironment; private final JetCoreEnvironment myEnvironment;
private final List<JetFile> mySourceFiles = new ArrayList<JetFile>(); private final List<JetFile> mySourceFiles = new ArrayList<JetFile>();
private final List<JetFile> myLibrarySourceFiles = new ArrayList<JetFile>(); private final List<JetFile> myLibrarySourceFiles = new ArrayList<JetFile>();
private final FileNameTransformer myFileNameTransformer;
private List<String> myErrors = new ArrayList<String>(); private List<String> myErrors = new ArrayList<String>();
public BindingContext getMyBindingContext() { public BindingContext getMyBindingContext() {
@@ -38,7 +39,12 @@ public class CompileSession {
private BindingContext myBindingContext; private BindingContext myBindingContext;
public CompileSession(JetCoreEnvironment environment) { public CompileSession(JetCoreEnvironment environment) {
this(environment, FileNameTransformer.IDENTITY);
}
public CompileSession(JetCoreEnvironment environment, FileNameTransformer fileNameTransformer) {
myEnvironment = environment; myEnvironment = environment;
myFileNameTransformer = fileNameTransformer;
} }
public void addSources(String path) { public void addSources(String path) {
@@ -115,13 +121,13 @@ public class CompileSession {
@NotNull @NotNull
public ClassFileFactory generate() { public ClassFileFactory generate() {
GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.BINARIES); GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.BINARIES, myFileNameTransformer);
generationState.compileCorrectFiles(myBindingContext, mySourceFiles); generationState.compileCorrectFiles(myBindingContext, mySourceFiles);
return generationState.getFactory(); return generationState.getFactory();
} }
public String generateText() { public String generateText() {
GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.TEXT); GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.TEXT, myFileNameTransformer);
generationState.compileCorrectFiles(myBindingContext, mySourceFiles); generationState.compileCorrectFiles(myBindingContext, mySourceFiles);
return generationState.createText(); return generationState.createText();
} }