Allow 'return exp;' for void main().

It's unfortunate that for main the llvm type and D type don't match up...
This commit is contained in:
Christian Kamm 2009-02-17 18:53:15 +01:00
parent d6a264e74b
commit f62df176d6
2 changed files with 14 additions and 2 deletions

View file

@ -90,10 +90,15 @@ void ReturnStatement::toIR(IRState* p)
if (Logger::enabled())
Logger::cout() << "return value is '" <<*v << "'\n";
// can happen for classes
// can happen for classes and void main
if (v->getType() != p->topfunc()->getReturnType())
{
v = gIR->ir->CreateBitCast(v, p->topfunc()->getReturnType(), "tmp");
// for main and a void expression: return 0 instead, else bitcast
if (p->topfunc() == p->mainFunc && v->getType() == LLType::VoidTy)
v = llvm::Constant::getNullValue(p->mainFunc->getReturnType());
else
v = gIR->ir->CreateBitCast(v, p->topfunc()->getReturnType(), "tmp");
if (Logger::enabled())
Logger::cout() << "return value after cast: " << *v << '\n';
}