]> granicus.if.org Git - clang/commitdiff
add codegen support for global inits that require array decay.
authorChris Lattner <sabre@nondot.org>
Sun, 2 Dec 2007 07:30:13 +0000 (07:30 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 2 Dec 2007 07:30:13 +0000 (07:30 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44511 91177308-0d34-0410-b5e6-96231b3b80d8

CodeGen/CodeGenModule.cpp
test/CodeGen/globalinit.c

index 6ef620654429377004fcd47eea79966327a0f243..97ff705a39fcdddd742b8139db69762eaca59642 100644 (file)
@@ -368,6 +368,27 @@ static llvm::Constant *GenerateConstantExpr(const Expr *Expression,
 
   case Stmt::ImplicitCastExprClass: {
     const ImplicitCastExpr *ICExpr = cast<ImplicitCastExpr>(Expression);
+    
+    // If this is due to array->pointer conversion, emit the array expression as
+    // an l-value.
+    if (ICExpr->getSubExpr()->getType()->isArrayType()) {
+      // FIXME: For now we assume that all source arrays map to LLVM arrays.
+      // This will not true when we add support for VLAs.
+      // The only thing that can have array type like this is a
+      // DeclRefExpr(FileVarDecl)?
+      const DeclRefExpr *DRE = cast<DeclRefExpr>(ICExpr->getSubExpr());
+      const FileVarDecl *FVD = cast<FileVarDecl>(DRE->getDecl());
+      llvm::Constant *C = CGM.GetAddrOfFileVarDecl(FVD, false);
+      assert(isa<llvm::PointerType>(C->getType()) &&
+             isa<llvm::ArrayType>(cast<llvm::PointerType>(C->getType())
+                                  ->getElementType()) &&
+             "Doesn't support VLAs yet!");
+      llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
+      
+      llvm::Constant *Ops[] = {Idx0, Idx0};
+      return llvm::ConstantExpr::getGetElementPtr(C, Ops, 2);
+    }
+    
     return GenerateConstantCast(ICExpr->getSubExpr(), type, CGM);
   }
 
index e94027c360922194e1bbd3c265c65ea62e4a1f51..dc7cdb9c063920595d09a0bc3f1f6db2bf833198 100644 (file)
@@ -8,3 +8,7 @@ void foo() { x[0] = 1; }
 int x[10];
 void bar() { x[0] = 1; }
 
+
+extern int x[];
+void *g = x;
+