mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-02-28 01:23:14 +01:00
Implemented constant pointer casts (like casting function pointer to void* as a constant global initializer)
This commit is contained in:
@@ -1003,6 +1003,9 @@ struct CastExp : UnaExp
|
||||
|
||||
// For operator overloading
|
||||
Identifier *opId();
|
||||
|
||||
// LLVMDC
|
||||
virtual llvm::Constant *toConstElem(IRState *irs);
|
||||
};
|
||||
|
||||
|
||||
|
||||
16
gen/toir.cpp
16
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());
|
||||
|
||||
15
tests/mini/const1.d
Normal file
15
tests/mini/const1.d
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user