diff --git a/expr.cpp b/expr.cpp index b43f9e54..81680874 100644 --- a/expr.cpp +++ b/expr.cpp @@ -1258,6 +1258,11 @@ UnaryExpr::TypeCheck() { type->GetString().c_str()); return NULL; } + if (dynamic_cast(pt->GetBaseType())) { + Error(expr->pos, "Illegal to pre/post increment pointer to " + "undefined struct type \"%s\".", type->GetString().c_str()); + return NULL; + } return this; } @@ -2296,6 +2301,16 @@ BinaryExpr::TypeCheck() { "on \"%s\" type.", type1->GetString().c_str()); return NULL; } + if (dynamic_cast(pt0->GetBaseType())) { + Error(pos, "Illegal to perform pointer arithmetic " + "on undefined struct type \"%s\".", pt0->GetString().c_str()); + return NULL; + } + if (dynamic_cast(pt1->GetBaseType())) { + Error(pos, "Illegal to perform pointer arithmetic " + "on undefined struct type \"%s\".", pt1->GetString().c_str()); + return NULL; + } const Type *t = Type::MoreGeneralType(type0, type1, pos, "-"); if (t == NULL) @@ -2331,6 +2346,11 @@ BinaryExpr::TypeCheck() { "on \"%s\" type.", pt0->GetString().c_str()); return NULL; } + if (dynamic_cast(pt0->GetBaseType())) { + Error(pos, "Illegal to perform pointer arithmetic " + "on undefined struct type \"%s\".", pt0->GetString().c_str()); + return NULL; + } const Type *offsetType = g->target.is32Bit ? AtomicType::UniformInt32 : AtomicType::UniformInt64; @@ -7336,6 +7356,14 @@ SizeOfExpr::Print() const { Expr * SizeOfExpr::TypeCheck() { + // Can't compute the size of a struct without a definition + if (type != NULL && + dynamic_cast(type) != NULL) { + Error(pos, "Can't compute the size of declared but not defined " + "struct type \"%s\".", type->GetString().c_str()); + return NULL; + } + return this; } @@ -8105,9 +8133,20 @@ NewExpr::GetType() const { Expr * NewExpr::TypeCheck() { - // Here we only need to make sure that if we have an expression giving - // a number of elements to allocate that it can be converted to an - // integer of the appropriate variability. + // It's illegal to call new with an undefined struct type + if (allocType == NULL) { + Assert(m->errorCount > 0); + return NULL; + } + if (dynamic_cast(allocType) != NULL) { + Error(pos, "Can't dynamically allocate storage for declared " + "but not defined type \"%s\".", allocType->GetString().c_str()); + return NULL; + } + + // Otherwise we only need to make sure that if we have an expression + // giving a number of elements to allocate that it can be converted to + // an integer of the appropriate variability. if (countExpr == NULL) return this; diff --git a/tests_errors/undef-struct-new.ispc b/tests_errors/undef-struct-new.ispc new file mode 100644 index 00000000..3a9037c3 --- /dev/null +++ b/tests_errors/undef-struct-new.ispc @@ -0,0 +1,7 @@ +// Can't dynamically allocate storage for declared but not defined type + +struct Foo; + +Foo * uniform bar() { + return uniform new Foo; +} diff --git a/tests_errors/undef-struct-ptrmath-1.ispc b/tests_errors/undef-struct-ptrmath-1.ispc new file mode 100644 index 00000000..861c66fb --- /dev/null +++ b/tests_errors/undef-struct-ptrmath-1.ispc @@ -0,0 +1,7 @@ +// Illegal to perform pointer arithmetic on undefined struct type + +struct Foo; + +Foo * uniform bar(Foo * uniform f) { + return f + 1; +} diff --git a/tests_errors/undef-struct-ptrmath-2.ispc b/tests_errors/undef-struct-ptrmath-2.ispc new file mode 100644 index 00000000..dfaab13c --- /dev/null +++ b/tests_errors/undef-struct-ptrmath-2.ispc @@ -0,0 +1,7 @@ +// Illegal to perform pointer arithmetic on undefined struct type + +struct Foo; + +Foo * uniform bar(Foo * uniform f) { + return 1 + f; +} diff --git a/tests_errors/undef-struct-ptrmath-3.ispc b/tests_errors/undef-struct-ptrmath-3.ispc new file mode 100644 index 00000000..1fad2ac4 --- /dev/null +++ b/tests_errors/undef-struct-ptrmath-3.ispc @@ -0,0 +1,7 @@ +// Illegal to perform pointer arithmetic on undefined struct type + +struct Foo; + +Foo * uniform bar(Foo * uniform f) { + return f-1; +} diff --git a/tests_errors/undef-struct-ptrmath.ispc b/tests_errors/undef-struct-ptrmath.ispc new file mode 100644 index 00000000..39b19a4e --- /dev/null +++ b/tests_errors/undef-struct-ptrmath.ispc @@ -0,0 +1,7 @@ +// Illegal to pre/post increment pointer to undefined struct type + +struct Foo; + +Foo * uniform bar(Foo * uniform f) { + return ++f; +} diff --git a/tests_errors/undef-struct-sizeof.ispc b/tests_errors/undef-struct-sizeof.ispc new file mode 100644 index 00000000..d2a2219a --- /dev/null +++ b/tests_errors/undef-struct-sizeof.ispc @@ -0,0 +1,7 @@ +// Can't compute the size of declared but not defined struct type + +struct Foo; + +uniform int bar() { + return sizeof(Foo); +}