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)
}

llvm-svn: 134972
This commit is contained in:
Chris Lattner 2011-07-12 06:52:18 +00:00
parent d59d867ca5
commit 3f32d69699
3 changed files with 45 additions and 9 deletions

View File

@ -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);

View File

@ -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: {

View File

@ -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];
}
}