Allow structs with no members.

Issue #289.
This commit is contained in:
Matt Pharr
2012-06-21 16:07:31 -07:00
parent 50eb4bf53a
commit 5a2c8342eb
3 changed files with 43 additions and 14 deletions

View File

@@ -909,11 +909,21 @@ struct_or_union_specifier
} }
| struct_or_union '{' '}' | struct_or_union '{' '}'
{ {
Error(@1, "Empty struct definitions not allowed."); llvm::SmallVector<const Type *, 8> elementTypes;
llvm::SmallVector<std::string, 8> elementNames;
llvm::SmallVector<SourcePos, 8> elementPositions;
$$ = new StructType("", elementTypes, elementNames, elementPositions,
false, Variability::Unbound, @1);
} }
| struct_or_union struct_or_union_name '{' '}' | struct_or_union struct_or_union_name '{' '}'
{ {
Error(@1, "Empty struct definitions not allowed."); llvm::SmallVector<const Type *, 8> elementTypes;
llvm::SmallVector<std::string, 8> elementNames;
llvm::SmallVector<SourcePos, 8> elementPositions;
StructType *st = new StructType($2, elementTypes, elementNames, elementPositions,
false, Variability::Unbound, @1);
m->symbolTable->AddType($2, st, @2);
$$ = st;
} }
| struct_or_union struct_or_union_name | struct_or_union struct_or_union_name
{ {

13
tests/empty-struct.ispc Normal file
View File

@@ -0,0 +1,13 @@
export uniform int width() { return programCount; }
struct Foo { };
export void f_f(uniform float RET[], uniform float aFOO[]) {
uniform Foo f;
RET[programIndex] = sizeof(f);
}
export void result(uniform float RET[]) {
RET[programIndex] = 1;
}

View File

@@ -1828,19 +1828,25 @@ StructType::StructType(const std::string &n, const llvm::SmallVector<const Type
// Actually make the LLVM struct // Actually make the LLVM struct
std::vector<llvm::Type *> elementTypes; std::vector<llvm::Type *> elementTypes;
for (int i = 0; i < GetElementCount(); ++i) { int nElements = GetElementCount();
const Type *type = GetElementType(i); if (nElements == 0) {
if (type == NULL) { elementTypes.push_back(LLVMTypes::Int8Type);
Assert(m->errorCount > 0); }
return; else {
for (int i = 0; i < nElements; ++i) {
const Type *type = GetElementType(i);
if (type == NULL) {
Assert(m->errorCount > 0);
return;
}
else if (CastType<FunctionType>(type) != NULL) {
Error(elementPositions[i], "Method declarations are not "
"supported.");
return;
}
else
elementTypes.push_back(type->LLVMType(g->ctx));
} }
else if (CastType<FunctionType>(type) != NULL) {
Error(elementPositions[i], "Method declarations are not "
"supported.");
return;
}
else
elementTypes.push_back(type->LLVMType(g->ctx));
} }
if (lStructTypeMap.find(mname) == lStructTypeMap.end()) { if (lStructTypeMap.find(mname) == lStructTypeMap.end()) {