changeset 1514:7a528017b4c6

Don't initialize arrays of (arrays of...) void-initialized typedefs.
author Frits van Bommel <fvbommel wxs.nl>
date Wed, 24 Jun 2009 17:14:50 +0200
parents 8a5570ddad25
children 391fdb36f2aa
files gen/arrays.cpp
diffstat 1 files changed, 25 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/gen/arrays.cpp	Mon Jun 22 19:31:25 2009 +0200
+++ b/gen/arrays.cpp	Wed Jun 24 17:14:50 2009 +0200
@@ -398,6 +398,25 @@
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////
+static bool isInitialized(Type* et) {
+    // Strip array types from element type
+    Type* bt = et->toBasetype();
+    while (bt->ty == Tsarray || bt->ty == Tarray) {
+        et = bt->nextOf();
+        bt = et->toBasetype();
+    }
+    // If it's a typedef with "= void" initializer then don't initialize.
+    if (et->ty == Ttypedef) {
+        Logger::println("Typedef: %s", et->toChars());
+        TypedefDeclaration* tdd = ((TypeTypedef*)et)->sym;
+        if (tdd && tdd->init && tdd->init->isVoidInitializer())
+            return false;
+    }
+    // Otherwise, it's always initialized.
+    return true;
+}
+
+//////////////////////////////////////////////////////////////////////////////////////////
 DSliceValue* DtoNewDynArray(Loc& loc, Type* arrayType, DValue* dim, bool defaultInit)
 {
     Logger::println("DtoNewDynArray : %s", arrayType->toChars());
@@ -411,7 +430,10 @@
     LLValue* arrayLen = dim->getRVal();
 
     // get runtime function
-    bool zeroInit = arrayType->toBasetype()->nextOf()->isZeroInit();
+    Type* eltType = arrayType->toBasetype()->nextOf();
+    if (defaultInit && !isInitialized(eltType))
+        defaultInit = false;
+    bool zeroInit = eltType->isZeroInit();
     const char* fnname = defaultInit ? (zeroInit ? "_d_newarrayT" : "_d_newarrayiT") : "_d_newarrayvT";
     LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname);
 
@@ -445,6 +467,8 @@
 
     // get runtime function
     bool zeroInit = vtype->isZeroInit();
+    if (defaultInit && !isInitialized(vtype))
+        defaultInit = false;
     const char* fnname = defaultInit ? (zeroInit ? "_d_newarraymT" : "_d_newarraymiT") : "_d_newarraymvT";
     LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname);