ErrorHandler introduced

This commit is contained in:
Andrey Breslav
2011-03-01 14:43:17 +03:00
parent 8c77e38c67
commit d06233eed6
9 changed files with 49 additions and 19 deletions
@@ -0,0 +1,10 @@
package org.jetbrains.jet.lang;
/**
* @author abreslav
*/
public class ErrorHandler {
public static final ErrorHandler DO_NOTHING = new ErrorHandler();
public static final ErrorHandler THROW_EXCEPTION = new ErrorHandler();
}
@@ -10,16 +10,23 @@ import org.jetbrains.jet.lang.types.JetTypeInferrer;
* @author abreslav * @author abreslav
*/ */
public class JetSemanticServices { public class JetSemanticServices {
private final JetStandardLibrary standardLibrary; public static JetSemanticServices createSemanticServices(JetStandardLibrary standardLibrary, ErrorHandler errorHandler) {
private final JetTypeInferrer typeInferrer; return new JetSemanticServices(standardLibrary, errorHandler);
public JetSemanticServices(JetStandardLibrary standardLibrary) {
this.standardLibrary = standardLibrary;
this.typeInferrer = new JetTypeInferrer(BindingTrace.DUMMY, this);
} }
public JetSemanticServices(Project project) { public static JetSemanticServices createSemanticServices(Project project, ErrorHandler errorHandler) {
this(new JetStandardLibrary(project)); return new JetSemanticServices(new JetStandardLibrary(project), errorHandler);
}
private final JetTypeInferrer typeInferrer;
private final JetStandardLibrary standardLibrary;
private final ErrorHandler errorHandler;
private JetSemanticServices(JetStandardLibrary standardLibrary, ErrorHandler errorHandler) {
this.standardLibrary = standardLibrary;
this.errorHandler = errorHandler;
this.typeInferrer = new JetTypeInferrer(BindingTrace.DUMMY, this);
} }
public JetStandardLibrary getStandardLibrary() { public JetStandardLibrary getStandardLibrary() {
@@ -5,6 +5,7 @@ import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.ErrorHandler;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
@@ -20,7 +21,9 @@ public class JetPsiChecker implements Annotator {
public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolder holder) { public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolder holder) {
if (element instanceof JetFile) { if (element instanceof JetFile) {
JetFile file = (JetFile) element; JetFile file = (JetFile) element;
JetSemanticServices semanticServices = new JetSemanticServices(element.getProject()); JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(element.getProject(), new ErrorHandler() {
});
try { try {
final BindingContext bindingContext = new TopDownAnalyzer(semanticServices).process(semanticServices.getStandardLibrary().getLibraryScope(), file.getRootNamespace().getDeclarations()); final BindingContext bindingContext = new TopDownAnalyzer(semanticServices).process(semanticServices.getStandardLibrary().getLibraryScope(), file.getRootNamespace().getDeclarations());
file.getRootNamespace().accept(new JetVisitor() { file.getRootNamespace().accept(new JetVisitor() {
@@ -8,6 +8,7 @@ import com.intellij.psi.tree.TokenSet;
import com.intellij.util.IncorrectOperationException; import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.ErrorHandler;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.TopDownAnalyzer; import org.jetbrains.jet.lang.resolve.TopDownAnalyzer;
@@ -66,7 +67,7 @@ public class JetReferenceExpression extends JetExpression {
element = element.getParent(); element = element.getParent();
} }
JetFile file = (JetFile) element; JetFile file = (JetFile) element;
JetSemanticServices semanticServices = new JetSemanticServices(element.getProject()); JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(element.getProject(), ErrorHandler.DO_NOTHING);
BindingContext bindingContext = new TopDownAnalyzer(semanticServices).process(semanticServices.getStandardLibrary().getLibraryScope(), file.getRootNamespace().getDeclarations()); BindingContext bindingContext = new TopDownAnalyzer(semanticServices).process(semanticServices.getStandardLibrary().getLibraryScope(), file.getRootNamespace().getDeclarations());
return bindingContext.resolveToDeclarationPsiElement(JetReferenceExpression.this); return bindingContext.resolveToDeclarationPsiElement(JetReferenceExpression.this);
} }
@@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiFileFactory; import com.intellij.psi.PsiFileFactory;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.ErrorHandler;
import org.jetbrains.jet.lang.JetFileType; import org.jetbrains.jet.lang.JetFileType;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
@@ -50,7 +51,7 @@ public class JetStandardLibrary {
JetFile file = (JetFile) PsiFileFactory.getInstance(project).createFileFromText("Library.jet", JetFile file = (JetFile) PsiFileFactory.getInstance(project).createFileFromText("Library.jet",
JetFileType.INSTANCE, FileUtil.loadTextAndClose(new InputStreamReader(stream))); JetFileType.INSTANCE, FileUtil.loadTextAndClose(new InputStreamReader(stream)));
JetSemanticServices bootstrappingSemanticServices = new JetSemanticServices(this); JetSemanticServices bootstrappingSemanticServices = JetSemanticServices.createSemanticServices(this, ErrorHandler.DO_NOTHING);
TopDownAnalyzer bootstrappingTDA = new TopDownAnalyzer(bootstrappingSemanticServices); TopDownAnalyzer bootstrappingTDA = new TopDownAnalyzer(bootstrappingSemanticServices);
BindingContext bindingContext = bootstrappingTDA.process(JetStandardClasses.STANDARD_CLASSES, file.getRootNamespace().getDeclarations()); BindingContext bindingContext = bootstrappingTDA.process(JetStandardClasses.STANDARD_CLASSES, file.getRootNamespace().getDeclarations());
this.libraryScope = bindingContext.getTopLevelScope(); this.libraryScope = bindingContext.getTopLevelScope();
@@ -338,7 +338,9 @@ public class JetTypeInferrer {
throw new IllegalArgumentException("Unsupported element: " + elem); throw new IllegalArgumentException("Unsupported element: " + elem);
} }
}); });
trace.recordExpressionType(expression, result[0]); if (result[0] != null) {
trace.recordExpressionType(expression, result[0]);
}
return result[0]; return result[0];
} }
@@ -396,14 +398,18 @@ public class JetTypeInferrer {
@Override @Override
public FunctionDescriptor getFunctionDescriptorForNamedArguments(@NotNull List<Type> typeArguments, @NotNull Map<String, Type> valueArgumentTypes, @Nullable Type functionLiteralArgumentType) { public FunctionDescriptor getFunctionDescriptorForNamedArguments(@NotNull List<Type> typeArguments, @NotNull Map<String, Type> valueArgumentTypes, @Nullable Type functionLiteralArgumentType) {
FunctionDescriptor descriptor = result[0].getFunctionDescriptorForNamedArguments(typeArguments, valueArgumentTypes, functionLiteralArgumentType); FunctionDescriptor descriptor = result[0].getFunctionDescriptorForNamedArguments(typeArguments, valueArgumentTypes, functionLiteralArgumentType);
trace.recordReferenceResolution(reference[0], descriptor); if (descriptor != null) {
trace.recordReferenceResolution(reference[0], descriptor);
}
return descriptor; return descriptor;
} }
@Override @Override
public FunctionDescriptor getFunctionDescriptorForPositionedArguments(@NotNull List<Type> typeArguments, @NotNull List<Type> positionedValueArgumentTypes) { public FunctionDescriptor getFunctionDescriptorForPositionedArguments(@NotNull List<Type> typeArguments, @NotNull List<Type> positionedValueArgumentTypes) {
FunctionDescriptor descriptor = result[0].getFunctionDescriptorForPositionedArguments(typeArguments, positionedValueArgumentTypes); FunctionDescriptor descriptor = result[0].getFunctionDescriptorForPositionedArguments(typeArguments, positionedValueArgumentTypes);
trace.recordReferenceResolution(reference[0], descriptor); if (descriptor != null) {
trace.recordReferenceResolution(reference[0], descriptor);
}
return descriptor; return descriptor;
} }
}; };
@@ -1,10 +1,10 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.jetbrains.jet.lang.ErrorHandler;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespace; import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.types.JetStandardLibrary;
import org.jetbrains.jet.parsing.JetParsingTest; import org.jetbrains.jet.parsing.JetParsingTest;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.util.TraceClassVisitor; import org.objectweb.asm.util.TraceClassVisitor;
@@ -62,7 +62,7 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
JetFile jetFile = (JetFile) myFixture.getFile(); JetFile jetFile = (JetFile) myFixture.getFile();
JetNamespace namespace = jetFile.getRootNamespace(); JetNamespace namespace = jetFile.getRootNamespace();
codegen.generate(namespace, new TraceClassVisitor(new PrintWriter(writer)), new JetSemanticServices(new JetStandardLibrary(getProject()))); codegen.generate(namespace, new TraceClassVisitor(new PrintWriter(writer)), JetSemanticServices.createSemanticServices(getProject(), ErrorHandler.THROW_EXCEPTION));
return writer.toString(); return writer.toString();
} }
@@ -70,7 +70,7 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
JetFile jetFile = (JetFile) myFixture.getFile(); JetFile jetFile = (JetFile) myFixture.getFile();
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
final JetNamespace namespace = jetFile.getRootNamespace(); final JetNamespace namespace = jetFile.getRootNamespace();
codegen.generate(namespace, writer, new JetSemanticServices(new JetStandardLibrary(getProject()))); codegen.generate(namespace, writer, JetSemanticServices.createSemanticServices(getProject(), ErrorHandler.THROW_EXCEPTION));
final byte[] data = writer.toByteArray(); final byte[] data = writer.toByteArray();
MyClassLoader classLoader = new MyClassLoader(NamespaceGenTest.class.getClassLoader()); MyClassLoader classLoader = new MyClassLoader(NamespaceGenTest.class.getClassLoader());
final Class aClass = classLoader.doDefineClass(NamespaceCodegen.getJVMClassName(namespace).replace("/", "."), data); final Class aClass = classLoader.doDefineClass(NamespaceCodegen.getJVMClassName(namespace).replace("/", "."), data);
@@ -3,6 +3,7 @@ package org.jetbrains.jet.resolve;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase; import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import com.intellij.openapi.application.PathManager; import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.jet.lang.ErrorHandler;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.*; import org.jetbrains.jet.lang.resolve.*;
@@ -39,7 +40,7 @@ public class JetResolveTest extends LightDaemonAnalyzerTestCase {
public void testBasic() throws Exception { public void testBasic() throws Exception {
JetFile jetFile = JetChangeUtil.createFile(getProject(), FileUtil.loadTextAndClose(new FileReader(getTestDataPath() + "/resolve/Basic.jet"))); JetFile jetFile = JetChangeUtil.createFile(getProject(), FileUtil.loadTextAndClose(new FileReader(getTestDataPath() + "/resolve/Basic.jet")));
List<JetDeclaration> declarations = jetFile.getRootNamespace().getDeclarations(); List<JetDeclaration> declarations = jetFile.getRootNamespace().getDeclarations();
BindingContext bindingContext = new TopDownAnalyzer(new JetSemanticServices(library)).process(library.getLibraryScope(), declarations); BindingContext bindingContext = new TopDownAnalyzer(JetSemanticServices.createSemanticServices(library, ErrorHandler.THROW_EXCEPTION)).process(library.getLibraryScope(), declarations);
JetClass classADecl = (JetClass) declarations.get(0); JetClass classADecl = (JetClass) declarations.get(0);
ClassDescriptor classA = bindingContext.getClassDescriptor(classADecl); ClassDescriptor classA = bindingContext.getClassDescriptor(classADecl);
@@ -4,6 +4,7 @@ import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import com.intellij.openapi.application.PathManager; import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.ErrorHandler;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.psi.JetChangeUtil; import org.jetbrains.jet.lang.psi.JetChangeUtil;
import org.jetbrains.jet.lang.psi.JetClass; import org.jetbrains.jet.lang.psi.JetClass;
@@ -32,7 +33,7 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
library = new JetStandardLibrary(getProject()); library = new JetStandardLibrary(getProject());
semanticServices = new JetSemanticServices(library); semanticServices = JetSemanticServices.createSemanticServices(library, ErrorHandler.THROW_EXCEPTION);
classDefinitions = new ClassDefinitions(); classDefinitions = new ClassDefinitions();
classDescriptorResolver = semanticServices.getClassDescriptorResolver(BindingTrace.DUMMY); classDescriptorResolver = semanticServices.getClassDescriptorResolver(BindingTrace.DUMMY);
} }