More code simplifications from using CollectionType. Finishes Issue #37

This commit is contained in:
Matt Pharr
2011-06-29 09:32:31 +01:00
parent 6b153566f3
commit 28a68e3c1f
2 changed files with 40 additions and 68 deletions

View File

@@ -2494,7 +2494,7 @@ ExprList::GetConstant(const Type *type) const {
if (collectionType == NULL) if (collectionType == NULL)
return NULL; return NULL;
std::string name = NULL; std::string name;
if (dynamic_cast<const StructType *>(type) != NULL) if (dynamic_cast<const StructType *>(type) != NULL)
name = "struct"; name = "struct";
else if (dynamic_cast<const ArrayType *>(type) != NULL) else if (dynamic_cast<const ArrayType *>(type) != NULL)
@@ -3888,7 +3888,8 @@ lUniformValueToVarying(FunctionEmitContext *ctx, llvm::Value *value,
// for structs/arrays/vectors, just recursively make their elements // for structs/arrays/vectors, just recursively make their elements
// varying (if needed) and populate the return value. // varying (if needed) and populate the return value.
const CollectionType *collectionType = dynamic_cast<const StructType *>(type); const CollectionType *collectionType =
dynamic_cast<const CollectionType *>(type);
if (collectionType != NULL) { if (collectionType != NULL) {
for (int i = 0; i < collectionType->GetElementCount(); ++i) { for (int i = 0; i < collectionType->GetElementCount(); ++i) {
llvm::Value *v = ctx->ExtractInst(value, i, "get_element"); llvm::Value *v = ctx->ExtractInst(value, i, "get_element");

103
stmt.cpp
View File

@@ -178,88 +178,59 @@ lInitSymbol(llvm::Value *lvalue, const char *symName, const Type *type,
return; return;
} }
// There are two cases for initializing arrays and vectors; either a single // There are two cases for initializing structs, arrays and vectors;
// initializer may be provided (float foo[3] = 0;), in which case all // either a single initializer may be provided (float foo[3] = 0;), in
// of the array elements are initialized to the given value, or an // which case all of the elements are initialized to the given value,
// initializer list may be provided (float foo[3] = { 1,2,3 }), in // or an initializer list may be provided (float foo[3] = { 1,2,3 }),
// which case the array elements are initialized with the corresponding // in which case the elements are initialized with the corresponding
// values. // values.
const SequentialType *seqType = dynamic_cast<const SequentialType *>(type); const CollectionType *collectionType =
if (seqType != NULL) { dynamic_cast<const CollectionType *>(type);
ExprList *exprList = dynamic_cast<ExprList *>(initExpr); if (collectionType != NULL) {
if (exprList == NULL) { std::string name;
// We have single expression; loop over the elements of the if (dynamic_cast<const StructType *>(type) != NULL)
// array/vector and initialize each of them with it name = "struct";
// individually. else if (dynamic_cast<const ArrayType *>(type) != NULL)
for (int i = 0; i < seqType->GetElementCount(); ++i) { name = "array";
llvm::Value *ptr = ctx->GetElementPtrInst(lvalue, 0, i, "offset"); else if (dynamic_cast<const VectorType *>(type) != NULL)
lInitSymbol(ptr, symName, seqType->GetElementType(), initExpr, name = "vector";
ctx, pos); else
} FATAL("Unexpected CollectionType in lInitSymbol()");
}
else {
// Otherwise make sure that we have the same number of elements
// in the { } initializer expression as we have in the
// array/vector
int nInits = exprList->exprs.size();
if (nInits != seqType->GetElementCount()) {
const char *actualType = dynamic_cast<const ArrayType *>(type) ?
"Array" : "Vector";
Error(initExpr->pos, "%s initializer for variable \"%s\" requires "
"%d values; %d provided.", actualType, symName,
seqType->GetElementCount(), nInits);
}
else {
// And initialize each of the array/vector elements with
// the corresponding expression from the ExprList
for (int i = 0; i < nInits; ++i) {
llvm::Value *ptr = ctx->GetElementPtrInst(lvalue, 0, i, "offset");
lInitSymbol(ptr, symName, seqType->GetElementType(),
exprList->exprs[i], ctx, pos);
}
}
}
return;
}
// Structs can similarly be initialized in one of two ways; either with
// a list of expressions in braces, one expression per struct member,
// or with a single expression that is used to initialize all struct
// members.
const StructType *st = dynamic_cast<const StructType *>(type);
if (st) {
ExprList *exprList = dynamic_cast<ExprList *>(initExpr); ExprList *exprList = dynamic_cast<ExprList *>(initExpr);
if (exprList != NULL) { if (exprList != NULL) {
// The { ... } case; make sure we have the same number of // The { ... } case; make sure we have the same number of
// expressions in the ExprList as we have struct members // expressions in the ExprList as we have struct members
int nInits = exprList->exprs.size(); int nInits = exprList->exprs.size();
if (nInits != st->GetElementCount()) if (nInits != collectionType->GetElementCount()) {
Error(initExpr->pos, Error(initExpr->pos, "Initializer for %s \"%s\" requires "
"Initializer for struct \"%s\" requires %d values; %d provided.", "%d values; %d provided.", name.c_str(), symName,
symName, st->GetElementCount(), nInits); collectionType->GetElementCount(), nInits);
else { return;
// Initialize each struct member with the corresponding }
// value from the ExprList
for (int i = 0; i < nInits; ++i) { // Initialize each element with the corresponding value from
llvm::Value *ep = ctx->GetElementPtrInst(lvalue, 0, i, "structelement"); // the ExprList
lInitSymbol(ep, symName, st->GetElementType(i), exprList->exprs[i], for (int i = 0; i < nInits; ++i) {
ctx, pos); llvm::Value *ep = ctx->GetElementPtrInst(lvalue, 0, i, "element");
} lInitSymbol(ep, symName, collectionType->GetElementType(i),
exprList->exprs[i], ctx, pos);
} }
} }
else if (initExpr->GetType()->IsNumericType() || else if (initExpr->GetType()->IsNumericType() ||
initExpr->GetType()->IsBoolType()) { initExpr->GetType()->IsBoolType()) {
// Otherwise initialize all of the struct elements in turn with // Otherwise initialize all of the elements in turn with the
// the initExpr. // initExpr.
for (int i = 0; i < st->GetElementCount(); ++i) { for (int i = 0; i < collectionType->GetElementCount(); ++i) {
llvm::Value *ep = ctx->GetElementPtrInst(lvalue, 0, i, "structelement"); llvm::Value *ep = ctx->GetElementPtrInst(lvalue, 0, i, "element");
lInitSymbol(ep, symName, st->GetElementType(i), initExpr, ctx, pos); lInitSymbol(ep, symName, collectionType->GetElementType(i),
initExpr, ctx, pos);
} }
} }
else { else {
Error(initExpr->pos, "Can't assign type \"%s\" to \"%s\".", Error(initExpr->pos, "Can't assign type \"%s\" to \"%s\".",
initExpr->GetType()->GetString().c_str(), initExpr->GetType()->GetString().c_str(),
st->GetString().c_str()); collectionType->GetString().c_str());
} }
return; return;
} }