changeset 433:b5f55f471e0b

Move DeclarationExp code into a helper function so it can call itself for template mixin members.
author Christian Kamm <kamm incasoftware de>
date Wed, 30 Jul 2008 09:21:06 +0200
parents ecf70fe065b9
children 6df6433fdb25 74101be2a553
files gen/llvmhelpers.cpp gen/llvmhelpers.h gen/toir.cpp
diffstat 3 files changed, 130 insertions(+), 112 deletions(-) [+]
line wrap: on
line diff
--- a/gen/llvmhelpers.cpp	Tue Jul 29 21:52:25 2008 +0200
+++ b/gen/llvmhelpers.cpp	Wed Jul 30 09:21:06 2008 +0200
@@ -5,6 +5,7 @@
 #include "init.h"
 #include "id.h"
 #include "expression.h"
+#include "template.h"
 
 #include "gen/tollvm.h"
 #include "gen/llvmhelpers.h"
@@ -1207,6 +1208,131 @@
 
 /****************************************************************************************/
 /*////////////////////////////////////////////////////////////////////////////////////////
+//      DECLARATION EXP HELPER
+////////////////////////////////////////////////////////////////////////////////////////*/
+DValue* DtoDeclarationExp(Dsymbol* declaration)
+{
+    Logger::print("DtoDeclarationExp: %s\n", declaration->toChars());
+    LOG_SCOPE;
+
+    // variable declaration
+    if (VarDeclaration* vd = declaration->isVarDeclaration())
+    {
+        Logger::println("VarDeclaration");
+
+        // static
+        if (vd->isDataseg())
+        {
+            vd->toObjFile(0); // TODO: multiobj
+        }
+        else
+        {
+            if (global.params.llvmAnnotate)
+                DtoAnnotation(declaration->toChars());
+
+            Logger::println("vdtype = %s", vd->type->toChars());
+
+            // referenced by nested delegate?
+            if (vd->nestedref) {
+                Logger::println("has nestedref set");
+                assert(vd->ir.irLocal);
+                vd->ir.irLocal->value = gIR->func()->decl->ir.irFunc->nestedVar;
+                assert(vd->ir.irLocal->value);
+                assert(vd->ir.irLocal->nestedIndex >= 0);
+            }
+            // normal stack variable, allocate storage on the stack if it has not already been done
+            else if(!vd->ir.irLocal) {
+                const LLType* lltype = DtoType(vd->type);
+
+                llvm::Value* allocainst;
+                if(gTargetData->getTypeSizeInBits(lltype) == 0) 
+                    allocainst = llvm::ConstantPointerNull::get(getPtrToType(lltype));
+                else
+                    allocainst = new llvm::AllocaInst(lltype, vd->toChars(), gIR->topallocapoint());
+
+                //allocainst->setAlignment(vd->type->alignsize()); // TODO
+                vd->ir.irLocal = new IrLocal(vd);
+                vd->ir.irLocal->value = allocainst;
+
+                if (global.params.symdebug)
+                {
+                    DtoDwarfLocalVariable(allocainst, vd);
+                }
+            }
+
+            Logger::cout() << "llvm value for decl: " << *vd->ir.irLocal->value << '\n';
+            DValue* ie = DtoInitializer(vd->init);
+        }
+
+        return new DVarValue(vd, vd->ir.getIrValue(), true);
+    }
+    // struct declaration
+    else if (StructDeclaration* s = declaration->isStructDeclaration())
+    {
+        Logger::println("StructDeclaration");
+        DtoForceConstInitDsymbol(s);
+    }
+    // function declaration
+    else if (FuncDeclaration* f = declaration->isFuncDeclaration())
+    {
+        Logger::println("FuncDeclaration");
+        DtoForceDeclareDsymbol(f);
+    }
+    // alias declaration
+    else if (AliasDeclaration* a = declaration->isAliasDeclaration())
+    {
+        Logger::println("AliasDeclaration - no work");
+        // do nothing
+    }
+    // enum
+    else if (EnumDeclaration* e = declaration->isEnumDeclaration())
+    {
+        Logger::println("EnumDeclaration - no work");
+        // do nothing
+    }
+    // class
+    else if (ClassDeclaration* e = declaration->isClassDeclaration())
+    {
+        Logger::println("ClassDeclaration");
+        DtoForceConstInitDsymbol(e);
+    }
+    // typedef
+    else if (TypedefDeclaration* tdef = declaration->isTypedefDeclaration())
+    {
+        Logger::println("TypedefDeclaration");
+        DtoTypeInfoOf(tdef->type, false);
+    }
+    // attribute declaration
+    else if (AttribDeclaration* a = declaration->isAttribDeclaration())
+    {
+        Logger::println("AttribDeclaration");
+        for (int i=0; i < a->decl->dim; ++i)
+        {
+            DtoForceDeclareDsymbol((Dsymbol*)a->decl->data[i]);
+        }
+    }
+    // mixin declaration
+    else if (TemplateMixin* m = declaration->isTemplateMixin())
+    {
+        Logger::println("TemplateMixin");
+        for (int i=0; i < m->members->dim; ++i)
+        {
+            Dsymbol* mdsym = (Dsymbol*)m->members->data[i];
+            DtoDeclarationExp(mdsym);
+        }
+    }
+    // unsupported declaration
+    else
+    {
+        error(declaration->loc, "Unimplemented Declaration type for DeclarationExp. kind: %s", declaration->kind());
+        assert(0);
+    }
+    return NULL;
+}
+
+
+/****************************************************************************************/
+/*////////////////////////////////////////////////////////////////////////////////////////
 //      INITIALIZER HELPERS
 ////////////////////////////////////////////////////////////////////////////////////////*/
 
--- a/gen/llvmhelpers.h	Tue Jul 29 21:52:25 2008 +0200
+++ b/gen/llvmhelpers.h	Wed Jul 30 09:21:06 2008 +0200
@@ -71,6 +71,9 @@
 void DtoForceConstInitDsymbol(Dsymbol* dsym);
 void DtoForceDefineDsymbol(Dsymbol* dsym);
 
+// declaration inside a declarationexp
+DValue* DtoDeclarationExp(Dsymbol* declaration);
+
 // initializer helpers
 LLConstant* DtoConstInitializer(Type* type, Initializer* init);
 LLConstant* DtoConstFieldInitializer(Type* type, Initializer* init);
--- a/gen/toir.cpp	Tue Jul 29 21:52:25 2008 +0200
+++ b/gen/toir.cpp	Wed Jul 30 09:21:06 2008 +0200
@@ -44,118 +44,7 @@
     Logger::print("DeclarationExp::toElem: %s | T=%s\n", toChars(), type->toChars());
     LOG_SCOPE;
 
-    // variable declaration
-    if (VarDeclaration* vd = declaration->isVarDeclaration())
-    {
-        Logger::println("VarDeclaration");
-
-        // static
-        if (vd->isDataseg())
-        {
-            vd->toObjFile(0); // TODO: multiobj
-        }
-        else
-        {
-            if (global.params.llvmAnnotate)
-                DtoAnnotation(toChars());
-
-            Logger::println("vdtype = %s", vd->type->toChars());
-
-            // referenced by nested delegate?
-            if (vd->nestedref) {
-                Logger::println("has nestedref set");
-                assert(vd->ir.irLocal);
-                vd->ir.irLocal->value = p->func()->decl->ir.irFunc->nestedVar;
-                assert(vd->ir.irLocal->value);
-                assert(vd->ir.irLocal->nestedIndex >= 0);
-            }
-            // normal stack variable, allocate storage on the stack if it has not already been done
-            else if(!vd->ir.irLocal) {
-                const LLType* lltype = DtoType(vd->type);
-
-                llvm::Value* allocainst;
-                if(gTargetData->getTypeSizeInBits(lltype) == 0) 
-                    allocainst = llvm::ConstantPointerNull::get(getPtrToType(lltype));
-                else
-                    allocainst = new llvm::AllocaInst(lltype, vd->toChars(), p->topallocapoint());
-
-                //allocainst->setAlignment(vd->type->alignsize()); // TODO
-                vd->ir.irLocal = new IrLocal(vd);
-                vd->ir.irLocal->value = allocainst;
-
-                if (global.params.symdebug)
-                {
-                    DtoDwarfLocalVariable(allocainst, vd);
-                }
-            }
-
-            Logger::cout() << "llvm value for decl: " << *vd->ir.irLocal->value << '\n';
-            DValue* ie = DtoInitializer(vd->init);
-        }
-
-        return new DVarValue(vd, vd->ir.getIrValue(), true);
-    }
-    // struct declaration
-    else if (StructDeclaration* s = declaration->isStructDeclaration())
-    {
-        Logger::println("StructDeclaration");
-        DtoForceConstInitDsymbol(s);
-    }
-    // function declaration
-    else if (FuncDeclaration* f = declaration->isFuncDeclaration())
-    {
-        Logger::println("FuncDeclaration");
-        DtoForceDeclareDsymbol(f);
-    }
-    // alias declaration
-    else if (AliasDeclaration* a = declaration->isAliasDeclaration())
-    {
-        Logger::println("AliasDeclaration - no work");
-        // do nothing
-    }
-    // enum
-    else if (EnumDeclaration* e = declaration->isEnumDeclaration())
-    {
-        Logger::println("EnumDeclaration - no work");
-        // do nothing
-    }
-    // class
-    else if (ClassDeclaration* e = declaration->isClassDeclaration())
-    {
-        Logger::println("ClassDeclaration");
-        DtoForceConstInitDsymbol(e);
-    }
-    // typedef
-    else if (TypedefDeclaration* tdef = declaration->isTypedefDeclaration())
-    {
-        Logger::println("TypedefDeclaration");
-        DtoTypeInfoOf(tdef->type, false);
-    }
-    // attribute declaration
-    else if (AttribDeclaration* a = declaration->isAttribDeclaration())
-    {
-        Logger::println("AttribDeclaration");
-        for (int i=0; i < a->decl->dim; ++i)
-        {
-            DtoForceDeclareDsymbol((Dsymbol*)a->decl->data[i]);
-        }
-    }
-    // mixin declaration
-    else if (TemplateMixin* m = declaration->isTemplateMixin())
-    {
-        Logger::println("TemplateMixin");
-        for (int i=0; i < m->members->dim; ++i)
-        {
-            DtoForceDeclareDsymbol((Dsymbol*)m->members->data[i]);
-        }
-    }
-    // unsupported declaration
-    else
-    {
-        error("Unimplemented DeclarationExp type. kind: %s", declaration->kind());
-        assert(0);
-    }
-    return 0;
+    return DtoDeclarationExp(declaration);
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////