JetNamespace eliminated

This commit is contained in:
Andrey Breslav
2011-12-27 20:23:58 +04:00
parent a5d8ff4669
commit 6aafaa1851
157 changed files with 31254 additions and 31833 deletions
@@ -1,6 +1,6 @@
package org.jetbrains.jet.codegen;
import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import java.util.*;
@@ -30,13 +30,13 @@ public class ClassFileFactory {
return newVisitor(className + ".class");
}
NamespaceCodegen forNamespace(JetNamespace namespace) {
NamespaceCodegen forNamespace(JetFile file) {
assert !isDone : "Already done!";
String fqName = JetPsiUtil.getFQName(namespace);
String fqName = JetPsiUtil.getFQName(file);
NamespaceCodegen codegen = ns2codegen.get(fqName);
if (codegen == null) {
final ClassBuilder builder = newVisitor(NamespaceCodegen.getJVMClassName(fqName) + ".class");
codegen = new NamespaceCodegen(builder, fqName, state, namespace.getContainingFile());
codegen = new NamespaceCodegen(builder, fqName, state, file.getContainingFile());
ns2codegen.put(fqName, codegen);
}
@@ -6,6 +6,7 @@ package org.jetbrains.jet.codegen;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiElement;
import com.intellij.util.containers.Stack;
import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethods;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
@@ -78,15 +79,14 @@ public class GenerationState {
return Pair.create(className, factory.forAnonymousSubclass(className));
}
public NamespaceCodegen forNamespace(JetNamespace namespace) {
public NamespaceCodegen forNamespace(JetFile namespace) {
return factory.forNamespace(namespace);
}
public BindingContext compile(JetFile psiFile) {
final JetNamespace namespace = psiFile.getRootNamespace();
final BindingContext bindingContext = AnalyzerFacade.analyzeOneNamespaceWithJavaIntegration(namespace, JetControlFlowDataTraceFactory.EMPTY);
public BindingContext compile(JetFile file) {
final BindingContext bindingContext = AnalyzerFacade.analyzeOneFileWithJavaIntegration(file, JetControlFlowDataTraceFactory.EMPTY);
AnalyzingUtils.throwExceptionOnErrors(bindingContext);
compileCorrectNamespaces(bindingContext, Collections.singletonList(namespace));
compileCorrectFiles(bindingContext, Collections.singletonList(file));
return bindingContext;
// NamespaceCodegen codegen = forNamespace(namespace);
// bindingContexts.push(bindingContext);
@@ -102,15 +102,15 @@ public class GenerationState {
// }
}
public void compileCorrectNamespaces(BindingContext bindingContext, List<JetNamespace> namespaces) {
compileCorrectNamespaces(bindingContext, namespaces, CompilationErrorHandler.THROW_EXCEPTION);
public void compileCorrectFiles(BindingContext bindingContext, List<JetFile> files) {
compileCorrectFiles(bindingContext, files, CompilationErrorHandler.THROW_EXCEPTION);
}
public void compileCorrectNamespaces(BindingContext bindingContext, List<JetNamespace> namespaces, CompilationErrorHandler errorHandler) {
public void compileCorrectFiles(BindingContext bindingContext, List<JetFile> files, CompilationErrorHandler errorHandler) {
typeMapper = new JetTypeMapper(standardLibrary, bindingContext);
bindingContexts.push(bindingContext);
try {
for (JetNamespace namespace : namespaces) {
for (JetFile namespace : files) {
try {
generateNamespace(namespace);
}
@@ -129,7 +129,7 @@ public class GenerationState {
}
}
protected void generateNamespace(JetNamespace namespace) {
protected void generateNamespace(JetFile namespace) {
NamespaceCodegen codegen = forNamespace(namespace);
codegen.generate(namespace);
}
@@ -149,8 +149,8 @@ public class GenerationState {
return new GeneratedAnonymousClassDescriptor(nameAndVisitor.first, callableMethod.getSignature().getAsmMethod(), objectContext.outerWasUsed, null);
}
public static void prepareAnonymousClasses(JetElement aClass, final JetTypeMapper typeMapper) {
aClass.acceptChildren(new JetVisitorVoid() {
public static void prepareAnonymousClasses(PsiElement element, final JetTypeMapper typeMapper) {
element.acceptChildren(new JetVisitorVoid() {
@Override
public void visitJetElement(JetElement element) {
super.visitJetElement(element);
@@ -753,11 +753,12 @@ public class JetTypeMapper {
return name;
}
@SuppressWarnings("unchecked") JetNamedDeclaration container = PsiTreeUtil.getParentOfType(expression, JetNamespace.class, JetClass.class, JetObjectDeclaration.class);
@SuppressWarnings("unchecked")
PsiElement container = PsiTreeUtil.getParentOfType(expression, JetFile.class, JetClass.class, JetObjectDeclaration.class);
String baseName;
if (container instanceof JetNamespace) {
baseName = NamespaceCodegen.getJVMClassName(JetPsiUtil.getFQName(((JetNamespace) container)));
if (container instanceof JetFile) {
baseName = NamespaceCodegen.getJVMClassName(JetPsiUtil.getFQName(((JetFile) container)));
}
else {
ClassDescriptor aClass = bindingContext.get(BindingContext.CLASS, container);
@@ -43,8 +43,8 @@ public class NamespaceCodegen {
v.visitSource(sourceFile.getName(), null);
}
public void generate(JetNamespace namespace) {
NamespaceDescriptor descriptor = state.getBindingContext().get(BindingContext.NAMESPACE, namespace);
public void generate(JetFile file) {
NamespaceDescriptor descriptor = state.getBindingContext().get(BindingContext.NAMESPACE, file);
final CodegenContext context = CodegenContext.STATIC.intoNamespace(descriptor);
final FunctionCodegen functionCodegen = new FunctionCodegen(context, v, state);
@@ -52,10 +52,10 @@ public class NamespaceCodegen {
final ClassCodegen classCodegen = state.forClass();
if (v.generateCode()) {
GenerationState.prepareAnonymousClasses(namespace, state.getTypeMapper());
GenerationState.prepareAnonymousClasses(file, state.getTypeMapper());
}
for (JetDeclaration declaration : namespace.getDeclarations()) {
for (JetDeclaration declaration : file.getDeclarations()) {
if (declaration instanceof JetProperty) {
propertyCodegen.gen((JetProperty) declaration);
}
@@ -72,20 +72,20 @@ public class NamespaceCodegen {
else if (declaration instanceof JetClassOrObject) {
classCodegen.generate(context, (JetClassOrObject) declaration);
}
else if (declaration instanceof JetNamespace) {
JetNamespace childNamespace = (JetNamespace) declaration;
state.forNamespace(childNamespace).generate(childNamespace);
}
// else if (declaration instanceof JetFile) {
// JetFile childNamespace = (JetFile) declaration;
// state.forNamespace(childNamespace).generate(childNamespace);
// }
}
if (hasNonConstantPropertyInitializers(namespace)) {
generateStaticInitializers(namespace);
if (hasNonConstantPropertyInitializers(file)) {
generateStaticInitializers(file);
}
generateTypeInfoFields(namespace, context);
generateTypeInfoFields(file, context);
}
private void generateStaticInitializers(JetNamespace namespace) {
private void generateStaticInitializers(JetFile namespace) {
MethodVisitor mv = v.newMethod(namespace, ACC_PUBLIC | ACC_STATIC,
"<clinit>", "()V", null, null);
if (v.generateCode()) {
@@ -115,9 +115,9 @@ public class NamespaceCodegen {
}
}
private void generateTypeInfoFields(JetNamespace namespace, CodegenContext context) {
private void generateTypeInfoFields(JetFile file, CodegenContext context) {
if(context.typeInfoConstants != null) {
String jvmClassName = getJVMClassName(namespace.getName());
String jvmClassName = getJVMClassName(file.getNamespaceHeader().getName());
for(int index = 0; index != context.typeInfoConstantsCount; index++) {
JetType type = context.reverseTypeInfoConstants.get(index);
String fieldName = "$typeInfoCache$" + index;
@@ -137,7 +137,7 @@ public class NamespaceCodegen {
v.visitFieldInsn(PUTSTATIC, jvmClassName, fieldName, "Ljet/TypeInfo;");
v.visitLabel(end);
v.visitInsn(ARETURN);
FunctionCodegen.endVisit(v, "type info method", namespace);
FunctionCodegen.endVisit(v, "type info method", file);
}
}
}
@@ -187,7 +187,7 @@ public class NamespaceCodegen {
}
}
private static boolean hasNonConstantPropertyInitializers(JetNamespace namespace) {
private static boolean hasNonConstantPropertyInitializers(JetFile namespace) {
for (JetDeclaration declaration : namespace.getDeclarations()) {
if (declaration instanceof JetProperty) {
final JetExpression initializer = ((JetProperty) declaration).getInitializer();
@@ -8,21 +8,18 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Function;
import com.intellij.util.Processor;
import jet.ExtensionFunction0;
import jet.ExtensionFunction10;
import jet.Function1;
import jet.modules.IModuleBuilder;
import jet.modules.IModuleSetBuilder;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetCoreEnvironment;
import org.jetbrains.jet.codegen.ClassFileFactory;
import org.jetbrains.jet.codegen.GeneratedClassLoader;
import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.plugin.JetMainDetector;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
@@ -337,9 +334,9 @@ public class CompileEnvironment {
session.addStdLibSources();
String mainClass = null;
for (JetNamespace namespace : session.getSourceFileNamespaces()) {
if (JetMainDetector.hasMain(namespace.getDeclarations())) {
mainClass = JetPsiUtil.getFQName(namespace) + ".namespace";
for (JetFile file : session.getSourceFileNamespaces()) {
if (JetMainDetector.hasMain(file.getDeclarations())) {
mainClass = JetPsiUtil.getFQName(file) + ".namespace";
break;
}
}
@@ -10,7 +10,6 @@ import org.jetbrains.jet.codegen.ClassFileFactory;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
import org.jetbrains.jet.plugin.JetFileType;
@@ -27,8 +26,8 @@ import java.util.List;
*/
public class CompileSession {
private final JetCoreEnvironment myEnvironment;
private final List<JetNamespace> mySourceFileNamespaces = new ArrayList<JetNamespace>();
private final List<JetNamespace> myLibrarySourceFileNamespaces = new ArrayList<JetNamespace>();
private final List<JetFile> mySourceFiles = new ArrayList<JetFile>();
private final List<JetFile> myLibrarySourceFiles = new ArrayList<JetFile>();
private List<String> myErrors = new ArrayList<String>();
private BindingContext myBindingContext;
@@ -63,7 +62,7 @@ public class CompileSession {
VirtualFile fileByPath = myEnvironment.getLocalFileSystem().findFileByPath(file.getAbsolutePath());
PsiFile psiFile = PsiManager.getInstance(myEnvironment.getProject()).findFile(fileByPath);
if(psiFile instanceof JetFile) {
mySourceFileNamespaces.add(((JetFile) psiFile).getRootNamespace());
mySourceFiles.add((JetFile) psiFile);
}
}
}
@@ -78,14 +77,14 @@ public class CompileSession {
if (vFile.getFileType() == JetFileType.INSTANCE) {
PsiFile psiFile = PsiManager.getInstance(myEnvironment.getProject()).findFile(vFile);
if (psiFile instanceof JetFile) {
mySourceFileNamespaces.add(((JetFile) psiFile).getRootNamespace());
mySourceFiles.add((JetFile) psiFile);
}
}
}
}
public List<JetNamespace> getSourceFileNamespaces() {
return mySourceFileNamespaces;
public List<JetFile> getSourceFileNamespaces() {
return mySourceFiles;
}
public boolean analyze(final PrintStream out) {
@@ -95,9 +94,9 @@ public class CompileSession {
}
return false;
}
List<JetNamespace> allNamespaces = new ArrayList<JetNamespace>(mySourceFileNamespaces);
allNamespaces.addAll(myLibrarySourceFileNamespaces);
myBindingContext = AnalyzerFacade.analyzeNamespacesWithJavaIntegration(
List<JetFile> allNamespaces = new ArrayList<JetFile>(mySourceFiles);
allNamespaces.addAll(myLibrarySourceFiles);
myBindingContext = AnalyzerFacade.analyzeFilesWithJavaIntegration(
myEnvironment.getProject(), allNamespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
ErrorCollector errorCollector = new ErrorCollector(myBindingContext);
errorCollector.report(out);
@@ -106,13 +105,13 @@ public class CompileSession {
public ClassFileFactory generate() {
GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.BINARIES);
generationState.compileCorrectNamespaces(myBindingContext, mySourceFileNamespaces);
generationState.compileCorrectFiles(myBindingContext, mySourceFiles);
return generationState.getFactory();
}
public String generateText() {
GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.TEXT);
generationState.compileCorrectNamespaces(myBindingContext, mySourceFileNamespaces);
generationState.compileCorrectFiles(myBindingContext, mySourceFiles);
return generationState.createText();
}