changeset 619:722630261d62

Implemented constant pointer casts (like casting function pointer to void* as a constant global initializer)
author tomas@myhost
date Sun, 28 Sep 2008 21:09:21 +0200
parents c9aa338280ed
children 0ecdb11ca85e 26fce59fe80a
files dmd/expression.h gen/toir.cpp tests/mini/const1.d
diffstat 3 files changed, 34 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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);
 };
 
 
--- 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());
--- /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);
+}