diff --git a/dmd/expression.h b/dmd/expression.h index a7d74b3e..28bc78c2 100644 --- a/dmd/expression.h +++ b/dmd/expression.h @@ -1003,6 +1003,9 @@ struct CastExp : UnaExp // For operator overloading Identifier *opId(); + + // LLVMDC + virtual llvm::Constant *toConstElem(IRState *irs); }; diff --git a/gen/toir.cpp b/gen/toir.cpp index 785e95c1..c9ede4c4 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -819,6 +819,22 @@ DValue* CastExp::toElem(IRState* p) ////////////////////////////////////////////////////////////////////////////////////////// +LLConstant* CastExp::toConstElem(IRState* p) +{ + Logger::print("CastExp::toConstElem: %s | %s\n", toChars(), type->toChars()); + LOG_SCOPE; + + LLConstant* c = e1->toConstElem(p); + assert(isaPointer(c->getType())); + + const LLType* lltype = DtoType(type); + assert(isaPointer(lltype)); + + return llvm::ConstantExpr::getBitCast(c, lltype); +} + +////////////////////////////////////////////////////////////////////////////////////////// + DValue* SymOffExp::toElem(IRState* p) { Logger::print("SymOffExp::toElem: %s | %s\n", toChars(), type->toChars()); diff --git a/tests/mini/const1.d b/tests/mini/const1.d new file mode 100644 index 00000000..b37052be --- /dev/null +++ b/tests/mini/const1.d @@ -0,0 +1,15 @@ +module mini.const1; + +void* g = cast(void*)&foobar; + +int foobar() +{ + return 42; +} + +void main() +{ + auto fn = cast(int function())g; + int i = fn(); + assert(i == 42); +}