diff options
-rw-r--r-- | CodeGen/CodeGenModule.cpp | 21 | ||||
-rw-r--r-- | test/CodeGen/globalinit.c | 4 |
2 files changed, 25 insertions, 0 deletions
diff --git a/CodeGen/CodeGenModule.cpp b/CodeGen/CodeGenModule.cpp index 6ef6206544..97ff705a39 100644 --- a/CodeGen/CodeGenModule.cpp +++ b/CodeGen/CodeGenModule.cpp @@ -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); } diff --git a/test/CodeGen/globalinit.c b/test/CodeGen/globalinit.c index e94027c360..dc7cdb9c06 100644 --- a/test/CodeGen/globalinit.c +++ b/test/CodeGen/globalinit.c @@ -8,3 +8,7 @@ void foo() { x[0] = 1; } int x[10]; void bar() { x[0] = 1; } + +extern int x[]; +void *g = x; + |