Commit acbc8021 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Some basic support for the two-arg raise statement

parent 2cff7121
......@@ -1801,20 +1801,19 @@ private:
void doRaise(AST_Raise* node, ExcInfo exc_info) {
std::vector<llvm::Value*> args;
llvm::Value* raise_func = g.funcs.raise0;
if (node->arg0) {
raise_func = g.funcs.raise1;
CompilerVariable* arg0 = evalExpr(node->arg0, exc_info);
ConcreteCompilerVariable* converted_arg0 = arg0->makeConverted(emitter, arg0->getBoxType());
arg0->decvref(emitter);
args.push_back(converted_arg0->getValue());
for (auto a : { node->arg0, node->arg1, node->arg2 }) {
if (a) {
CompilerVariable* v = evalExpr(a, exc_info);
ConcreteCompilerVariable* converted = v->makeConverted(emitter, v->getBoxType());
v->decvref(emitter);
args.push_back(converted->getValue());
} else {
args.push_back(embedConstantPtr(None, g.llvm_value_type_ptr));
}
}
RELEASE_ASSERT(node->arg1 == NULL, "");
RELEASE_ASSERT(node->arg2 == NULL, "");
emitter.createCall(exc_info, raise_func, args);
emitter.createCall(exc_info, g.funcs.raise3, args);
emitter.getBuilder()->CreateUnreachable();
endBlock(DEAD);
......
......@@ -223,8 +223,7 @@ void initGlobalFuncs(GlobalState& g) {
GET(__cxa_begin_catch);
g.funcs.__cxa_end_catch = addFunc((void*)__cxa_end_catch, g.void_);
GET(raise0);
GET(raise1);
GET(raise3);
g.funcs.div_i64_i64 = getFunc((void*)div_i64_i64, "div_i64_i64");
g.funcs.mod_i64_i64 = getFunc((void*)mod_i64_i64, "mod_i64_i64");
......
......@@ -45,7 +45,7 @@ struct GlobalFuncs {
llvm::Value* reoptCompiledFunc, *compilePartialFunc;
llvm::Value* __cxa_begin_catch, *__cxa_end_catch;
llvm::Value* raise0, *raise1;
llvm::Value* raise3;
llvm::Value* div_i64_i64, *mod_i64_i64, *pow_i64_i64;
llvm::Value* div_float_float, *mod_float_float, *pow_float_float;
......
......@@ -95,7 +95,7 @@ void force() {
FORCE(callattr);
FORCE(raise0);
FORCE(raise1);
FORCE(raise3);
FORCE(div_i64_i64);
FORCE(mod_i64_i64);
......
......@@ -30,9 +30,8 @@ class BoxedString;
// user-level raise functions that implement python-level semantics
extern "C" void raise0() __attribute__((__noreturn__));
extern "C" void raise1(Box*) __attribute__((__noreturn__));
extern "C" void raise2(Box*, Box*) __attribute__((__noreturn__));
extern "C" void raise3(Box*, Box*, Box*) __attribute__((__noreturn__));
// helper function for raising from the runtime:
void raiseExcHelper(BoxedClass*, const char* fmt, ...) __attribute__((__noreturn__));
......
......@@ -199,21 +199,30 @@ void raise0() {
raiseRaw(last_exc);
}
void raise1(Box* b) {
if (b->cls == type_cls) {
BoxedClass* c = static_cast<BoxedClass*>(b);
void raise3(Box* arg0, Box* arg1, Box* arg2) {
RELEASE_ASSERT(arg2 == None, "unsupported");
if (arg0->cls == type_cls) {
BoxedClass* c = static_cast<BoxedClass*>(arg0);
if (isSubclass(c, Exception)) {
auto exc_obj = exceptionNew1(c);
Box* exc_obj;
if (arg1 != None)
exc_obj = exceptionNew2(c, arg1);
else
exc_obj = exceptionNew1(c);
raiseExc(exc_obj);
} else {
raiseExcHelper(TypeError, "exceptions must be old-style classes or derived from BaseException, not %s",
getTypeName(b)->c_str());
getTypeName(arg0)->c_str());
}
}
if (arg1 != None)
raiseExcHelper(TypeError, "instance exception may not have a separate value");
// TODO: should only allow throwing of old-style classes or things derived
// from BaseException:
raiseExc(b);
raiseExc(arg0);
}
void raiseExcHelper(BoxedClass* cls, const char* msg, ...) {
......
......@@ -320,3 +320,19 @@ def f10():
with expected_exception(ZeroDivisionError):
raise
f10()
def f11():
print "f11"
# old style exception syntax"
try:
raise Exception, 12345
except Exception, e:
print e
try:
raise KeyError(), 12345
except TypeError, e:
print e
f11()
......@@ -51,3 +51,19 @@ def f():
except:
print True
f()
def f11():
print "f11"
# old style exception syntax"
try:
raise KeyError, 12345
except KeyError, e:
print e
try:
raise KeyError(), 12345
except TypeError, e:
print e
f11()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment