better error reporting

This commit is contained in:
Alex Tkachman
2011-12-20 09:17:16 +02:00
parent 50400fd707
commit 78a2845604
11 changed files with 101 additions and 60 deletions
@@ -126,9 +126,7 @@ public abstract class ClassBodyCodegen {
}
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
FunctionCodegen.endVisit(v, "static initializer", myClass);
}
}
}
@@ -147,8 +147,7 @@ public class ClosureCodegen extends ObjectOrClosureCodegen {
mv.visitLabel(ret);
mv.visitInsn(ARETURN);
mv.visitMaxs(0,0);
mv.visitEnd();
FunctionCodegen.endVisit(mv, "$getInstance", fun);
}
}
@@ -205,8 +204,7 @@ public class ClosureCodegen extends ObjectOrClosureCodegen {
iv.areturn(JetTypeMapper.TYPE_OBJECT);
mv.visitMaxs(0, 0);
mv.visitEnd();
FunctionCodegen.endVisit(mv, "bridge", fun);
}
}
@@ -286,8 +284,7 @@ public class ClosureCodegen extends ObjectOrClosureCodegen {
iv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
FunctionCodegen.endVisit(iv, "constructor", fun);
}
return constructor;
}
@@ -0,0 +1,20 @@
package org.jetbrains.jet.codegen;
import com.intellij.psi.PsiElement;
import org.jetbrains.jet.lang.psi.JetElement;
/**
* @author alex.tkachman
*/
public class CompilationException extends RuntimeException {
private PsiElement element;
CompilationException(String message, Throwable cause, PsiElement element) {
super(message, cause);
this.element = element;
}
public PsiElement getElement() {
return element;
}
}
@@ -130,7 +130,15 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
public StackValue genQualified(StackValue receiver, JetElement selector) {
markLineNumber(selector);
return selector.accept(this, receiver);
try {
return selector.accept(this, receiver);
}
catch(CompilationException e) {
throw e;
}
catch (Throwable error) {
throw new CompilationException(error.getMessage(), error, selector);
}
}
public StackValue gen(JetElement expr) {
@@ -204,7 +212,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
JetExpression elseExpression = expression.getElse();
if (thenExpression == null && elseExpression == null) {
throw new CompilationException();
throw new CompilationException("Both brunches of if/else are null", null, expression);
}
if (isEmptyExpression(thenExpression)) {
@@ -1126,7 +1134,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
ResolvedCall<? extends CallableDescriptor> resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, callee);
if(resolvedCall == null) {
throw new CompilationException("Cannot resolve: " + callee.getText());
throw new CompilationException("Cannot resolve: " + callee.getText(), null, expression);
}
receiver = StackValue.receiver(resolvedCall, receiver, this, null);
@@ -2074,7 +2082,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
ResolvedCall<? extends CallableDescriptor> resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, callee);
if(resolvedCall == null) {
assert callee != null;
throw new CompilationException("Cannot resolve: " + callee.getText());
throw new CompilationException("Cannot resolve: " + callee.getText(), null, expression);
}
FunctionDescriptor descriptor = (FunctionDescriptor) resolvedCall.getResultingDescriptor();
@@ -2098,7 +2106,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
}
else {
if (args.size() != 1) {
throw new CompilationException("primitive array constructor requires one argument");
throw new CompilationException("primitive array constructor requires one argument", null, expression);
}
}
@@ -2784,15 +2792,6 @@ If finally block is present, its last expression is the value of try expression.
v.athrow();
}
private static class CompilationException extends RuntimeException {
private CompilationException() {
}
private CompilationException(String message) {
super(message);
}
}
@Override
public String toString() {
return context.getContextDescriptor().toString();
@@ -223,7 +223,7 @@ public class FunctionCodegen {
k += type.getSize();
}
mv.visitMaxs(0, 0);
endVisit(mv, null, fun);
mv.visitEnd();
generateBridgeIfNeeded(owner, state, v, jvmSignature.getAsmMethod(), functionDescriptor, kind);
@@ -233,6 +233,16 @@ public class FunctionCodegen {
generateDefaultIfNeeded(context, state, v, jvmSignature.getAsmMethod(), functionDescriptor, kind);
}
public static void endVisit(MethodVisitor mv, String description, PsiElement method) {
try {
mv.visitMaxs(0, 0);
}
catch (Throwable t) {
throw new CompilationException("wrong code generated" + (description != null ? " for " + description : "") + t.getClass().getName() + " " + t.getMessage(), t, method);
}
mv.visitEnd();
}
static void generateBridgeIfNeeded(CodegenContext owner, GenerationState state, ClassBuilder v, Method jvmSignature, FunctionDescriptor functionDescriptor, OwnerKind kind) {
Set<? extends FunctionDescriptor> overriddenFunctions = functionDescriptor.getOverriddenDescriptors();
if(kind != OwnerKind.TRAIT_IMPL) {
@@ -390,7 +400,7 @@ public class FunctionCodegen {
iv.areturn(jvmSignature.getReturnType());
mv.visitMaxs(0, 0);
endVisit(mv, "default method", state.getBindingContext().get(BindingContext.DESCRIPTOR_TO_DECLARATION, functionDescriptor));
mv.visitEnd();
}
}
@@ -426,7 +436,7 @@ public class FunctionCodegen {
if(jvmSignature.getReturnType() == Type.VOID_TYPE)
iv.aconst(null);
iv.areturn(overriden.getReturnType());
mv.visitMaxs(0, 0);
endVisit(mv, "bridge method", state.getBindingContext().get(BindingContext.DESCRIPTOR_TO_DECLARATION, functionDescriptor));
mv.visitEnd();
}
}
@@ -198,8 +198,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
iv.invokespecial(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION), originalMethod.getName(), originalMethod.getDescriptor());
iv.areturn(method.getReturnType());
mv.visitMaxs(0,0);
mv.visitEnd();
FunctionCodegen.endVisit(iv, "accessor", null);
}
}
else if(entry.getValue() instanceof PropertyDescriptor) {
@@ -222,8 +221,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
iv.invokespecial(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION), originalMethod.getName(), originalMethod.getDescriptor());
iv.areturn(method.getReturnType());
mv.visitMaxs(0,0);
mv.visitEnd();
FunctionCodegen.endVisit(iv, "accessor", null);
}
method = typeMapper.mapSetterSignature(bridge, OwnerKind.IMPLEMENTATION).getAsmMethod();
@@ -248,8 +246,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
iv.invokespecial(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION), originalMethod.getName(), originalMethod.getDescriptor());
iv.areturn(method.getReturnType());
mv.visitMaxs(0,0);
mv.visitEnd();
FunctionCodegen.endVisit(iv, "accessor", null);
}
}
else {
@@ -465,8 +462,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
outer.visitVarInsn(Opcodes.ALOAD, 0);
outer.visitFieldInsn(Opcodes.GETFIELD, classname, "this$0", outerType.getDescriptor());
outer.visitInsn(Opcodes.ARETURN);
outer.visitMaxs(0, 0);
outer.visitEnd();
FunctionCodegen.endVisit(outer, "getOuterObject", myClass);
}
if (CodegenUtil.requireTypeInfoConstructorArg(descriptor.getDefaultType()) && kind == OwnerKind.IMPLEMENTATION) {
@@ -518,8 +514,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
generateTraitMethods(codegen);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
FunctionCodegen.endVisit(mv, "constructor", myClass);
FunctionCodegen.generateDefaultIfNeeded(constructorContext, state, v, constructorMethod, constructorDescriptor, OwnerKind.IMPLEMENTATION);
}
@@ -572,8 +567,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
iv.checkcast(function.getReturnType());
}
iv.areturn(function.getReturnType());
mv.visitMaxs(0, 0);
mv.visitEnd();
FunctionCodegen.endVisit(iv, "trait method", bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, fun));
}
FunctionCodegen.generateBridgeIfNeeded(context, state, v, function, fun, kind);
@@ -708,8 +702,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
FunctionCodegen.endVisit(mv, "constructor", null);
}
}
@@ -821,8 +814,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
iv.load(0, JetTypeMapper.TYPE_OBJECT);
iv.getfield(owner, "$typeInfo", "Ljet/typeinfo/TypeInfo;");
iv.areturn(JetTypeMapper.TYPE_TYPEINFO);
mv.visitMaxs(0, 0);
mv.visitEnd();
FunctionCodegen.endVisit(iv, "getTypeInfo", myClass);
}
mv = v.newMethod(myClass, Opcodes.ACC_PROTECTED | Opcodes.ACC_FINAL, "$setTypeInfo", "(Ljet/typeinfo/TypeInfo;)V", null, null);
@@ -834,8 +826,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
iv.load(1, JetTypeMapper.TYPE_OBJECT);
iv.putfield(owner, "$typeInfo", "Ljet/typeinfo/TypeInfo;");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
FunctionCodegen.endVisit(iv, "$setTypeInfo", myClass);
}
}
}
@@ -853,8 +844,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
String owner = typeMapper.mapType(descriptor.getDefaultType(), OwnerKind.IMPLEMENTATION).getInternalName();
v.getstatic(owner, "$staticTypeInfo", "Ljet/typeinfo/TypeInfo;");
v.areturn(JetTypeMapper.TYPE_TYPEINFO);
mv.visitMaxs(0, 0);
mv.visitEnd();
FunctionCodegen.endVisit(v, "getTypeInfo", myClass);
}
}
@@ -62,8 +62,11 @@ public class NamespaceCodegen {
else if (declaration instanceof JetNamedFunction) {
try {
functionCodegen.gen((JetNamedFunction) declaration);
} catch (Exception e) {
throw new RuntimeException("Failed to generate function " + declaration.getName(), e);
} catch (CompilationException e) {
throw e;
}
catch (Exception e) {
throw new CompilationException("Failed to generate function " + declaration.getName(), e, declaration);
}
}
else if (declaration instanceof JetClassOrObject) {
@@ -107,7 +110,7 @@ public class NamespaceCodegen {
}
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
FunctionCodegen.endVisit(mv, "static initializer for namespace", namespace);
mv.visitEnd();
}
}
@@ -134,8 +137,7 @@ public class NamespaceCodegen {
v.visitFieldInsn(PUTSTATIC, jvmClassName, fieldName, "Ljet/typeinfo/TypeInfo;");
v.visitLabel(end);
v.visitInsn(ARETURN);
v.visitMaxs(0, 0);
v.visitEnd();
FunctionCodegen.endVisit(v, "type info method", namespace);
}
}
}
@@ -154,8 +154,7 @@ public class PropertyCodegen {
type.getDescriptor());
}
iv.areturn(type);
mv.visitMaxs(0, 0);
mv.visitEnd();
FunctionCodegen.endVisit(mv, "getter", origin);
}
}
@@ -207,7 +206,7 @@ public class PropertyCodegen {
}
iv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
FunctionCodegen.endVisit(mv, "setter", origin);
mv.visitEnd();
}
}