aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2011-07-12 06:52:18 +0000
committerChris Lattner <sabre@nondot.org>2011-07-12 06:52:18 +0000
commit3a2b657088de9413714a51bff153a59565adb3ef (patch)
treecb71c19b933c4b08606c3d721695e509c3caab26
parent811bf3669f4d82c57fe3cd3c49050fdbc95d0aff (diff)
Fix a problem Eli ran into where we now reject incomplete arrays of
uncompleted struct types. We now do what llvm-gcc does and compile them into [i8 x 0]. If the type is later completed, we make sure that it is appropriately cast. We compile the terrible example to something like this now: %struct.A = type { i32, i32, i32 } @g = external global [0 x i8] define void @_Z1fv() nounwind { entry: call void @_Z3fooP1A(%struct.A* bitcast ([0 x i8]* @g to %struct.A*)) ret void } declare void @_Z3fooP1A(%struct.A*) define %struct.A* @_Z2f2v() nounwind { entry: ret %struct.A* getelementptr inbounds ([0 x %struct.A]* bitcast ([0 x i8]* @g to [0 x %struct.A]*), i32 0, i64 1) } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@134972 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/CodeGen/CGExpr.cpp24
-rw-r--r--lib/CodeGen/CodeGenTypes.cpp11
-rw-r--r--test/CodeGenCXX/init-incomplete-type.cpp19
3 files changed, 45 insertions, 9 deletions
diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp
index e1d93095bd..6055fa759b 100644
--- a/lib/CodeGen/CGExpr.cpp
+++ b/lib/CodeGen/CGExpr.cpp
@@ -1274,6 +1274,14 @@ static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
}
}
+static llvm::Value *
+EmitBitCastOfLValueToProperType(llvm::IRBuilder<> &Builder,
+ llvm::Value *V, llvm::Type *IRType,
+ llvm::StringRef Name = llvm::StringRef()) {
+ unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
+ return Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
+}
+
static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
const Expr *E, const VarDecl *VD) {
assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
@@ -1282,8 +1290,11 @@ static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
if (VD->getType()->isReferenceType())
V = CGF.Builder.CreateLoad(V, "tmp");
- unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity();
+ V = EmitBitCastOfLValueToProperType(CGF.Builder, V,
+ CGF.getTypes().ConvertTypeForMem(E->getType()));
+
+ unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity();
LValue LV = CGF.MakeAddrLValue(V, E->getType(), Alignment);
setObjCGCLValueClass(CGF.getContext(), E, LV);
return LV;
@@ -1339,6 +1350,9 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
if (VD->getType()->isReferenceType())
V = Builder.CreateLoad(V, "tmp");
+ V = EmitBitCastOfLValueToProperType(Builder, V,
+ getTypes().ConvertTypeForMem(E->getType()));
+
LValue LV = MakeAddrLValue(V, E->getType(), Alignment);
if (NonGCable) {
LV.getQuals().removeObjCGCAttr();
@@ -1836,11 +1850,9 @@ LValue CodeGenFunction::EmitLValueForField(llvm::Value *baseAddr,
// for both unions and structs. A union needs a bitcast, a struct element
// will need a bitcast if the LLVM type laid out doesn't match the desired
// type.
- const llvm::Type *llvmType = CGM.getTypes().ConvertTypeForMem(type);
- unsigned AS = cast<llvm::PointerType>(baseAddr->getType())->getAddressSpace();
- addr = Builder.CreateBitCast(addr, llvmType->getPointerTo(AS),
- field->getName());
-
+ addr = EmitBitCastOfLValueToProperType(Builder, addr,
+ CGM.getTypes().ConvertTypeForMem(type),
+ field->getName());
unsigned alignment = getContext().getDeclAlign(field).getQuantity();
LValue LV = MakeAddrLValue(addr, type, alignment);
diff --git a/lib/CodeGen/CodeGenTypes.cpp b/lib/CodeGen/CodeGenTypes.cpp
index 64348ea7d1..8efe9e1eea 100644
--- a/lib/CodeGen/CodeGenTypes.cpp
+++ b/lib/CodeGen/CodeGenTypes.cpp
@@ -92,7 +92,6 @@ llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T){
// Otherwise, return an integer of the target-specified size.
return llvm::IntegerType::get(getLLVMContext(),
(unsigned)Context.getTypeSize(T));
-
}
/// isFuncTypeArgumentConvertible - Return true if the specified type in a
@@ -318,8 +317,14 @@ llvm::Type *CodeGenTypes::ConvertType(QualType T) {
const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
assert(A->getIndexTypeCVRQualifiers() == 0 &&
"FIXME: We only handle trivial array types so far!");
- // int X[] -> [0 x int]
- ResultType = llvm::ArrayType::get(ConvertTypeForMem(A->getElementType()),0);
+ // int X[] -> [0 x int], unless the element type is not sized. If it is
+ // unsized (e.g. an incomplete struct) just use [0 x i8].
+ ResultType = ConvertTypeForMem(A->getElementType());
+ if (!ResultType->isSized()) {
+ SkippedLayout = true;
+ ResultType = llvm::Type::getInt8Ty(getLLVMContext());
+ }
+ ResultType = llvm::ArrayType::get(ResultType, 0);
break;
}
case Type::ConstantArray: {
diff --git a/test/CodeGenCXX/init-incomplete-type.cpp b/test/CodeGenCXX/init-incomplete-type.cpp
index 3312d3e04b..1755dfb7be 100644
--- a/test/CodeGenCXX/init-incomplete-type.cpp
+++ b/test/CodeGenCXX/init-incomplete-type.cpp
@@ -10,3 +10,22 @@ static struct Bar<int> bar[1] = {
{ 0 }
};
+
+
+namespace incomplete_type_refs {
+ struct A;
+ extern A g[];
+ void foo(A*);
+ void f(void) {
+ foo(g); // Reference to array with unknown element type.
+ }
+
+ struct A { // define the element type.
+ int a,b,c;
+ };
+
+ A *f2() {
+ return &g[1];
+ }
+
+} \ No newline at end of file