glsl: Set ir_instruction::ir_type in the base class constructor
authorIan Romanick <ian.d.romanick@intel.com>
Wed, 28 May 2014 02:49:04 +0000 (19:49 -0700)
committerMatt Turner <mattst88@gmail.com>
Wed, 4 Jun 2014 00:58:34 +0000 (17:58 -0700)
This has the added perk that if you forget to set ir_type in the
constructor of a new subclass (or a new constructor of an existing
subclass) the compiler will tell you... instead of relying on
ir_validate or similar run-time detection.

Reviewed-by: Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
src/glsl/ir.cpp
src/glsl/ir.h

index ba8a8394f59eb5b2c55e7bacabc968a8a607a333..8fed768a24512fc46b321eab7994e43f22d14ad7 100644 (file)
@@ -26,7 +26,8 @@
 #include "ir_visitor.h"
 #include "glsl_types.h"
 
-ir_rvalue::ir_rvalue()
+ir_rvalue::ir_rvalue(enum ir_node_type t)
+   : ir_instruction(t)
 {
    this->type = glsl_type::error_type;
 }
@@ -153,8 +154,8 @@ ir_assignment::whole_variable_written()
 
 ir_assignment::ir_assignment(ir_dereference *lhs, ir_rvalue *rhs,
                             ir_rvalue *condition, unsigned write_mask)
+   : ir_instruction(ir_type_assignment)
 {
-   this->ir_type = ir_type_assignment;
    this->condition = condition;
    this->rhs = rhs;
    this->lhs = lhs;
@@ -173,8 +174,8 @@ ir_assignment::ir_assignment(ir_dereference *lhs, ir_rvalue *rhs,
 
 ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs,
                             ir_rvalue *condition)
+   : ir_instruction(ir_type_assignment)
 {
-   this->ir_type = ir_type_assignment;
    this->condition = condition;
    this->rhs = rhs;
 
@@ -198,8 +199,8 @@ ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs,
 ir_expression::ir_expression(int op, const struct glsl_type *type,
                             ir_rvalue *op0, ir_rvalue *op1,
                             ir_rvalue *op2, ir_rvalue *op3)
+   : ir_rvalue(ir_type_expression)
 {
-   this->ir_type = ir_type_expression;
    this->type = type;
    this->operation = ir_expression_operation(op);
    this->operands[0] = op0;
@@ -215,9 +216,8 @@ ir_expression::ir_expression(int op, const struct glsl_type *type,
 }
 
 ir_expression::ir_expression(int op, ir_rvalue *op0)
+   : ir_rvalue(ir_type_expression)
 {
-   this->ir_type = ir_type_expression;
-
    this->operation = ir_expression_operation(op);
    this->operands[0] = op0;
    this->operands[1] = NULL;
@@ -324,9 +324,8 @@ ir_expression::ir_expression(int op, ir_rvalue *op0)
 }
 
 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1)
+   : ir_rvalue(ir_type_expression)
 {
-   this->ir_type = ir_type_expression;
-
    this->operation = ir_expression_operation(op);
    this->operands[0] = op0;
    this->operands[1] = op1;
@@ -420,9 +419,8 @@ ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1)
 
 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1,
                              ir_rvalue *op2)
+   : ir_rvalue(ir_type_expression)
 {
-   this->ir_type = ir_type_expression;
-
    this->operation = ir_expression_operation(op);
    this->operands[0] = op0;
    this->operands[1] = op1;
@@ -610,25 +608,25 @@ ir_expression::get_operator(const char *str)
 }
 
 ir_constant::ir_constant()
+   : ir_rvalue(ir_type_constant)
 {
-   this->ir_type = ir_type_constant;
 }
 
 ir_constant::ir_constant(const struct glsl_type *type,
                         const ir_constant_data *data)
+   : ir_rvalue(ir_type_constant)
 {
    assert((type->base_type >= GLSL_TYPE_UINT)
          && (type->base_type <= GLSL_TYPE_BOOL));
 
-   this->ir_type = ir_type_constant;
    this->type = type;
    memcpy(& this->value, data, sizeof(this->value));
 }
 
 ir_constant::ir_constant(float f, unsigned vector_elements)
+   : ir_rvalue(ir_type_constant)
 {
    assert(vector_elements <= 4);
-   this->ir_type = ir_type_constant;
    this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT, vector_elements, 1);
    for (unsigned i = 0; i < vector_elements; i++) {
       this->value.f[i] = f;
@@ -639,9 +637,9 @@ ir_constant::ir_constant(float f, unsigned vector_elements)
 }
 
 ir_constant::ir_constant(unsigned int u, unsigned vector_elements)
+   : ir_rvalue(ir_type_constant)
 {
    assert(vector_elements <= 4);
-   this->ir_type = ir_type_constant;
    this->type = glsl_type::get_instance(GLSL_TYPE_UINT, vector_elements, 1);
    for (unsigned i = 0; i < vector_elements; i++) {
       this->value.u[i] = u;
@@ -652,9 +650,9 @@ ir_constant::ir_constant(unsigned int u, unsigned vector_elements)
 }
 
 ir_constant::ir_constant(int integer, unsigned vector_elements)
+   : ir_rvalue(ir_type_constant)
 {
    assert(vector_elements <= 4);
-   this->ir_type = ir_type_constant;
    this->type = glsl_type::get_instance(GLSL_TYPE_INT, vector_elements, 1);
    for (unsigned i = 0; i < vector_elements; i++) {
       this->value.i[i] = integer;
@@ -665,9 +663,9 @@ ir_constant::ir_constant(int integer, unsigned vector_elements)
 }
 
 ir_constant::ir_constant(bool b, unsigned vector_elements)
+   : ir_rvalue(ir_type_constant)
 {
    assert(vector_elements <= 4);
-   this->ir_type = ir_type_constant;
    this->type = glsl_type::get_instance(GLSL_TYPE_BOOL, vector_elements, 1);
    for (unsigned i = 0; i < vector_elements; i++) {
       this->value.b[i] = b;
@@ -678,8 +676,8 @@ ir_constant::ir_constant(bool b, unsigned vector_elements)
 }
 
 ir_constant::ir_constant(const ir_constant *c, unsigned i)
+   : ir_rvalue(ir_type_constant)
 {
-   this->ir_type = ir_type_constant;
    this->type = c->type->get_base_type();
 
    switch (this->type->base_type) {
@@ -692,8 +690,8 @@ ir_constant::ir_constant(const ir_constant *c, unsigned i)
 }
 
 ir_constant::ir_constant(const struct glsl_type *type, exec_list *value_list)
+   : ir_rvalue(ir_type_constant)
 {
-   this->ir_type = ir_type_constant;
    this->type = type;
 
    assert(type->is_scalar() || type->is_vector() || type->is_matrix()
@@ -1233,16 +1231,16 @@ ir_constant::is_uint16_constant() const
 }
 
 ir_loop::ir_loop()
+   : ir_instruction(ir_type_loop)
 {
-   this->ir_type = ir_type_loop;
 }
 
 
 ir_dereference_variable::ir_dereference_variable(ir_variable *var)
+   : ir_dereference(ir_type_dereference_variable)
 {
    assert(var != NULL);
 
-   this->ir_type = ir_type_dereference_variable;
    this->var = var;
    this->type = var->type;
 }
@@ -1250,8 +1248,8 @@ ir_dereference_variable::ir_dereference_variable(ir_variable *var)
 
 ir_dereference_array::ir_dereference_array(ir_rvalue *value,
                                           ir_rvalue *array_index)
+   : ir_dereference(ir_type_dereference_array)
 {
-   this->ir_type = ir_type_dereference_array;
    this->array_index = array_index;
    this->set_array(value);
 }
@@ -1259,10 +1257,10 @@ ir_dereference_array::ir_dereference_array(ir_rvalue *value,
 
 ir_dereference_array::ir_dereference_array(ir_variable *var,
                                           ir_rvalue *array_index)
+   : ir_dereference(ir_type_dereference_array)
 {
    void *ctx = ralloc_parent(var);
 
-   this->ir_type = ir_type_dereference_array;
    this->array_index = array_index;
    this->set_array(new(ctx) ir_dereference_variable(var));
 }
@@ -1289,10 +1287,10 @@ ir_dereference_array::set_array(ir_rvalue *value)
 
 ir_dereference_record::ir_dereference_record(ir_rvalue *value,
                                             const char *field)
+   : ir_dereference(ir_type_dereference_record)
 {
    assert(value != NULL);
 
-   this->ir_type = ir_type_dereference_record;
    this->record = value;
    this->field = ralloc_strdup(this, field);
    this->type = this->record->type->field_type(field);
@@ -1301,10 +1299,10 @@ ir_dereference_record::ir_dereference_record(ir_rvalue *value,
 
 ir_dereference_record::ir_dereference_record(ir_variable *var,
                                             const char *field)
+   : ir_dereference(ir_type_dereference_record)
 {
    void *ctx = ralloc_parent(var);
 
-   this->ir_type = ir_type_dereference_record;
    this->record = new(ctx) ir_dereference_variable(var);
    this->field = ralloc_strdup(this, field);
    this->type = this->record->type->field_type(field);
@@ -1421,24 +1419,22 @@ ir_swizzle::init_mask(const unsigned *comp, unsigned count)
 
 ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
                       unsigned w, unsigned count)
-   : val(val)
+   : ir_rvalue(ir_type_swizzle), val(val)
 {
    const unsigned components[4] = { x, y, z, w };
-   this->ir_type = ir_type_swizzle;
    this->init_mask(components, count);
 }
 
 ir_swizzle::ir_swizzle(ir_rvalue *val, const unsigned *comp,
                       unsigned count)
-   : val(val)
+   : ir_rvalue(ir_type_swizzle), val(val)
 {
-   this->ir_type = ir_type_swizzle;
    this->init_mask(comp, count);
 }
 
 ir_swizzle::ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
+   : ir_rvalue(ir_type_swizzle)
 {
-   this->ir_type = ir_type_swizzle;
    this->val = val;
    this->mask = mask;
    this->type = glsl_type::get_instance(val->type->base_type,
@@ -1537,9 +1533,8 @@ ir_swizzle::variable_referenced() const
 
 ir_variable::ir_variable(const struct glsl_type *type, const char *name,
                         ir_variable_mode mode)
-   : max_ifc_array_access(NULL)
+   : ir_instruction(ir_type_variable), max_ifc_array_access(NULL)
 {
-   this->ir_type = ir_type_variable;
    this->type = type;
    this->name = ralloc_strdup(this, name);
    this->data.explicit_location = false;
@@ -1613,10 +1608,10 @@ ir_variable::determine_interpolation_mode(bool flat_shade)
 
 ir_function_signature::ir_function_signature(const glsl_type *return_type,
                                              builtin_available_predicate b)
-   : return_type(return_type), is_defined(false), is_intrinsic(false),
+   : ir_instruction(ir_type_function_signature),
+     return_type(return_type), is_defined(false), is_intrinsic(false),
      builtin_avail(b), _function(NULL)
 {
-   this->ir_type = ir_type_function_signature;
    this->origin = NULL;
 }
 
@@ -1699,8 +1694,8 @@ ir_function_signature::replace_parameters(exec_list *new_params)
 
 
 ir_function::ir_function(const char *name)
+   : ir_instruction(ir_type_function)
 {
-   this->ir_type = ir_type_function;
    this->name = ralloc_strdup(this, name);
 }
 
@@ -1720,7 +1715,7 @@ ir_function::has_user_signature()
 ir_rvalue *
 ir_rvalue::error_value(void *mem_ctx)
 {
-   ir_rvalue *v = new(mem_ctx) ir_rvalue;
+   ir_rvalue *v = new(mem_ctx) ir_rvalue(ir_type_unset);
 
    v->type = glsl_type::error_type;
    return v;
index 80609829ebc6d1f16e9a7b7db3294ed1d1fd1d08..02784e1f369c83d8a4e404e23b0d337ea603291a 100644 (file)
@@ -152,9 +152,15 @@ public:
    virtual bool equals(ir_instruction *ir, enum ir_node_type ignore = ir_type_unset);
 
 protected:
+   ir_instruction(enum ir_node_type t)
+      : ir_type(t)
+   {
+   }
+
+private:
    ir_instruction()
    {
-      ir_type = ir_type_unset;
+      assert(!"Should not get here.");
    }
 };
 
@@ -281,7 +287,7 @@ public:
    static ir_rvalue *error_value(void *mem_ctx);
 
 protected:
-   ir_rvalue();
+   ir_rvalue(enum ir_node_type t);
 };
 
 
@@ -984,9 +990,8 @@ inline const char *ir_function_signature::function_name() const
 class ir_if : public ir_instruction {
 public:
    ir_if(ir_rvalue *condition)
-      : condition(condition)
+      : ir_instruction(ir_type_if), condition(condition)
    {
-      ir_type = ir_type_if;
    }
 
    virtual ir_if *clone(void *mem_ctx, struct hash_table *ht) const;
@@ -1514,9 +1519,8 @@ public:
    ir_call(ir_function_signature *callee,
           ir_dereference_variable *return_deref,
           exec_list *actual_parameters)
-      : return_deref(return_deref), callee(callee)
+      : ir_instruction(ir_type_call), return_deref(return_deref), callee(callee)
    {
-      ir_type = ir_type_call;
       assert(callee->return_type != NULL);
       actual_parameters->move_nodes_to(& this->actual_parameters);
       this->use_builtin = callee->is_builtin();
@@ -1579,9 +1583,9 @@ public:
 /*@{*/
 class ir_jump : public ir_instruction {
 protected:
-   ir_jump()
+   ir_jump(enum ir_node_type t)
+      : ir_instruction(t)
    {
-      ir_type = ir_type_unset;
    }
 
 public:
@@ -1594,15 +1598,13 @@ public:
 class ir_return : public ir_jump {
 public:
    ir_return()
-      : value(NULL)
+      : ir_jump(ir_type_return), value(NULL)
    {
-      this->ir_type = ir_type_return;
    }
 
    ir_return(ir_rvalue *value)
-      : value(value)
+      : ir_jump(ir_type_return), value(value)
    {
-      this->ir_type = ir_type_return;
    }
 
    virtual ir_return *clone(void *mem_ctx, struct hash_table *) const;
@@ -1644,8 +1646,8 @@ public:
    };
 
    ir_loop_jump(jump_mode mode)
+      : ir_jump(ir_type_loop_jump)
    {
-      this->ir_type = ir_type_loop_jump;
       this->mode = mode;
    }
 
@@ -1678,14 +1680,14 @@ public:
 class ir_discard : public ir_jump {
 public:
    ir_discard()
+      : ir_jump(ir_type_discard)
    {
-      this->ir_type = ir_type_discard;
       this->condition = NULL;
    }
 
    ir_discard(ir_rvalue *cond)
+      : ir_jump(ir_type_discard)
    {
-      this->ir_type = ir_type_discard;
       this->condition = cond;
    }
 
@@ -1752,10 +1754,10 @@ enum ir_texture_opcode {
 class ir_texture : public ir_rvalue {
 public:
    ir_texture(enum ir_texture_opcode op)
-      : op(op), sampler(NULL), coordinate(NULL), projector(NULL),
+      : ir_rvalue(ir_type_texture),
+        op(op), sampler(NULL), coordinate(NULL), projector(NULL),
         shadow_comparitor(NULL), offset(NULL)
    {
-      this->ir_type = ir_type_texture;
       memset(&lod_info, 0, sizeof(lod_info));
    }
 
@@ -1921,6 +1923,12 @@ public:
     * Get the variable that is ultimately referenced by an r-value
     */
    virtual ir_variable *variable_referenced() const = 0;
+
+protected:
+   ir_dereference(enum ir_node_type t)
+      : ir_rvalue(t)
+   {
+   }
 };
 
 
@@ -2207,8 +2215,8 @@ private:
 class ir_emit_vertex : public ir_instruction {
 public:
    ir_emit_vertex()
+      : ir_instruction(ir_type_emit_vertex)
    {
-      ir_type = ir_type_emit_vertex;
    }
 
    virtual void accept(ir_visitor *v)
@@ -2231,8 +2239,8 @@ public:
 class ir_end_primitive : public ir_instruction {
 public:
    ir_end_primitive()
+      : ir_instruction(ir_type_end_primitive)
    {
-      ir_type = ir_type_end_primitive;
    }
 
    virtual void accept(ir_visitor *v)