OSDN Git Service

check that if/while/do-while condition is boolean or scalar
authorBrian <brian@yutani.localnet.net>
Wed, 28 Mar 2007 16:44:38 +0000 (10:44 -0600)
committerBrian <brian@yutani.localnet.net>
Wed, 28 Mar 2007 16:44:38 +0000 (10:44 -0600)
src/mesa/shader/slang/slang_codegen.c

index cf3569c..bd403b7 100644 (file)
@@ -1363,6 +1363,22 @@ _slang_is_constant_cond(const slang_operation *oper, GLboolean *value)
 }
 
 
+/**
+ * Test if an operation is a scalar or boolean.
+ */
+static GLboolean
+_slang_is_scalar_or_boolean(slang_assemble_ctx *A, slang_operation *oper)
+{
+   slang_typeinfo type;
+   GLint size;
+
+   slang_typeinfo_construct(&type);
+   _slang_typeof_operation(A, oper, &type);
+   size = _slang_sizeof_type_specifier(&type.spec);
+   slang_typeinfo_destruct(&type);
+   return size == 1;
+}
+
 
 /**
  * Generate loop code using high-level IR_LOOP instruction
@@ -1378,6 +1394,12 @@ _slang_gen_while(slang_assemble_ctx * A, const slang_operation *oper)
    slang_ir_node *prevLoop, *loop, *cond, *breakIf, *body;
    GLboolean isConst, constTrue;
 
+   /* type-check expression */
+   if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
+      slang_info_log_error(A->log, "scalar/boolean expression expected for 'while'");
+      return NULL;
+   }
+
    /* Check if loop condition is a constant */
    isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
 
@@ -1434,6 +1456,12 @@ _slang_gen_do(slang_assemble_ctx * A, const slang_operation *oper)
    slang_ir_node *prevLoop, *loop, *cond;
    GLboolean isConst, constTrue;
 
+   /* type-check expression */
+   if (!_slang_is_scalar_or_boolean(A, &oper->children[1])) {
+      slang_info_log_error(A->log, "scalar/boolean expression expected for 'do/while'");
+      return NULL;
+   }
+
    loop = new_loop(NULL);
 
    /* save old, push new loop */
@@ -1556,6 +1584,12 @@ _slang_gen_if(slang_assemble_ctx * A, const slang_operation *oper)
    slang_ir_node *ifNode, *cond, *ifBody, *elseBody;
    GLboolean isConst, constTrue;
 
+   /* type-check expression */
+   if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
+      slang_info_log_error(A->log, "scalar/boolean expression expected for 'if'");
+      return NULL;
+   }
+
    isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
    if (isConst) {
       if (constTrue) {