Fix logic for ordering of struct declarations in generated header files.

When a struct had an array of another struct type as a member, we weren't
detecting that the struct type in the array needed to be declared before the
enclosing struct type.

Fixes issue #408.
This commit is contained in:
Matt Pharr
2012-12-06 11:39:22 -05:00
parent 23e5877509
commit 9892c8bf9a

View File

@@ -1075,6 +1075,23 @@ Module::writeObjectFileOrAssembly(llvm::TargetMachine *targetMachine,
} }
/** Given a pointer to an element of a structure, see if it is a struct
type or an array of a struct type. If so, return a pointer to the
underlying struct type. */
static const StructType *
lGetElementStructType(const Type *t) {
const StructType *st = CastType<StructType>(t);
if (st != NULL)
return st;
const ArrayType *at = CastType<ArrayType>(t);
if (at != NULL)
return lGetElementStructType(at->GetElementType());
return NULL;
}
/** Emits a declaration for the given struct to the given file. This /** Emits a declaration for the given struct to the given file. This
function first makes sure that declarations for any structs that are function first makes sure that declarations for any structs that are
(recursively) members of this struct are emitted first. (recursively) members of this struct are emitted first.
@@ -1092,7 +1109,7 @@ lEmitStructDecl(const StructType *st, std::vector<const StructType *> *emittedSt
// Otherwise first make sure any contained structs have been declared. // Otherwise first make sure any contained structs have been declared.
for (int i = 0; i < st->GetElementCount(); ++i) { for (int i = 0; i < st->GetElementCount(); ++i) {
const StructType *elementStructType = const StructType *elementStructType =
CastType<StructType>(st->GetElementType(i)); lGetElementStructType(st->GetElementType(i));
if (elementStructType != NULL) if (elementStructType != NULL)
lEmitStructDecl(elementStructType, emittedStructs, file); lEmitStructDecl(elementStructType, emittedStructs, file);
} }