Allow the last member of a struct to be an unsized/zero-length array.

This enables the C truck of allocating a dynamic amount of storage for
the struct in order to extend out the array to the desired length.
This commit is contained in:
Matt Pharr
2012-03-25 13:10:12 -07:00
parent ca3100874f
commit 1da2834b1e
3 changed files with 35 additions and 10 deletions

View File

@@ -739,15 +739,7 @@ GetStructTypesNamesPositions(const std::vector<StructDeclaration *> &sd,
if (Type::Equal(sym->type, AtomicType::Void))
Error(d->pos, "\"void\" type illegal for struct member.");
const ArrayType *arrayType =
dynamic_cast<const ArrayType *>(sym->type);
if (arrayType != NULL && arrayType->GetElementCount() == 0) {
Error(d->pos, "Unsized arrays aren't allowed in struct "
"definitions.");
elementTypes->push_back(NULL);
}
else
elementTypes->push_back(sym->type);
elementTypes->push_back(sym->type);
if (seenNames.find(sym->name) != seenNames.end())
Error(d->pos, "Struct member \"%s\" has same name as a "
@@ -759,4 +751,13 @@ GetStructTypesNamesPositions(const std::vector<StructDeclaration *> &sd,
elementPositions->push_back(sym->pos);
}
}
for (int i = 0; i < (int)elementTypes->size() - 1; ++i) {
const ArrayType *arrayType =
dynamic_cast<const ArrayType *>((*elementTypes)[i]);
if (arrayType != NULL && arrayType->GetElementCount() == 0)
Error((*elementPositions)[i], "Unsized arrays aren't allowed except "
"for the last member in a struct definition.");
}
}

View File

@@ -0,0 +1,24 @@
struct Foo {
float x;
float a[0];
};
export uniform int width() { return programCount; }
export void f_f(uniform float RET[], uniform float aFOO[]) {
uniform int nFloats = 3+programCount;
varying Foo * uniform ptr = (varying Foo * uniform)(uniform new varying int32[nFloats]);
memset(ptr, 0, nFloats*sizeof(int32));
for (uniform int i = 0; i < nFloats-1; ++i)
ptr->a[i] = i;
ptr->x = aFOO[programIndex];
RET[programIndex] = ptr->a[1+programIndex];
}
export void result(uniform float RET[]) {
RET[programIndex] = 1 + programIndex;
}

View File

@@ -1,4 +1,4 @@
// Unsized arrays aren't allowed in struct definitions
// Unsized arrays aren't allowed except for the last member in a struct definition.
struct Foo {
float a[];