Implemented constant pointer casts (like casting function pointer to void* as a constant global initializer)

This commit is contained in:
Tomas Lindquist Olsen
2008-09-28 21:09:21 +02:00
parent 04f7c18f95
commit 7b18b7a633
3 changed files with 34 additions and 0 deletions

View File

@@ -1003,6 +1003,9 @@ struct CastExp : UnaExp
// For operator overloading
Identifier *opId();
// LLVMDC
virtual llvm::Constant *toConstElem(IRState *irs);
};

View File

@@ -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());

15
tests/mini/const1.d Normal file
View File

@@ -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);
}