Fix bug with taking references of temporaries.

Previously, the compiler would crash if e.g. the program passed a
temporary value to a function taking a const reference.  This change
fixes ReferenceExpr::GetValue() to handle this case and allocate
temporary storage for the temporary so that the pointer to that
storage can be used for the reference value.
This commit is contained in:
Matt Pharr
2012-04-12 06:08:19 -07:00
parent 491fa239bd
commit ca7cc4744e
2 changed files with 42 additions and 1 deletions

View File

@@ -6861,7 +6861,34 @@ ReferenceExpr::ReferenceExpr(Expr *e, SourcePos p)
llvm::Value *
ReferenceExpr::GetValue(FunctionEmitContext *ctx) const {
ctx->SetDebugPos(pos);
return expr ? expr->GetLValue(ctx) : NULL;
if (expr == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
llvm::Value *value = expr->GetLValue(ctx);
if (value != NULL)
return value;
// value is NULL if the expression is a temporary; in this case, we'll
// allocate storage for it so that we can return the pointer to that...
const Type *type;
LLVM_TYPE_CONST llvm::Type *llvmType;
if ((type = expr->GetType()) == NULL ||
(llvmType = type->LLVMType(g->ctx)) == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
value = expr->GetValue(ctx);
if (value == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
llvm::Value *ptr = ctx->AllocaInst(llvmType);
ctx->StoreInst(value, ptr);
return ptr;
}

View File

@@ -0,0 +1,14 @@
export uniform int width() { return programCount; }
int func(const int &a) { return a+1; }
int bar() { return 0; }
export void f_f(uniform float RET[], uniform float aFOO[]) {
RET[programIndex] = func(bar());
}
export void result(uniform float RET[]) {
RET[programIndex] = 1;
}