# HG changeset patch # User tomas@myhost # Date 1222628961 -7200 # Node ID 722630261d62c80d32a93a3564bc8785f46f2bca # Parent c9aa338280ed8e554abbb9a8b5311620895c450e Implemented constant pointer casts (like casting function pointer to void* as a constant global initializer) diff -r c9aa338280ed -r 722630261d62 dmd/expression.h --- a/dmd/expression.h Sun Sep 28 15:22:39 2008 +0200 +++ b/dmd/expression.h Sun Sep 28 21:09:21 2008 +0200 @@ -1003,6 +1003,9 @@ // For operator overloading Identifier *opId(); + + // LLVMDC + virtual llvm::Constant *toConstElem(IRState *irs); }; diff -r c9aa338280ed -r 722630261d62 gen/toir.cpp --- a/gen/toir.cpp Sun Sep 28 15:22:39 2008 +0200 +++ b/gen/toir.cpp Sun Sep 28 21:09:21 2008 +0200 @@ -819,6 +819,22 @@ ////////////////////////////////////////////////////////////////////////////////////////// +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 -r c9aa338280ed -r 722630261d62 tests/mini/const1.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/mini/const1.d Sun Sep 28 21:09:21 2008 +0200 @@ -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); +}