add codegen support for global inits that require array decay.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44511 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/CodeGen/CodeGenModule.cpp b/CodeGen/CodeGenModule.cpp
index 6ef6206..97ff705 100644
--- a/CodeGen/CodeGenModule.cpp
+++ b/CodeGen/CodeGenModule.cpp
@@ -368,6 +368,27 @@
 
   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);
   }