ir_reader: rvalues are instructions too!
[mesa.git] / ast_function.cpp
index 340873ae4181984c83dc83cea25455dcffc82a13..3472b397cc1f0dcdcaea1495b163304c037f739f 100644 (file)
@@ -38,7 +38,7 @@ process_parameters(exec_list *instructions, exec_list *actual_parameters,
    if (first != NULL) {
       simple_node *ptr = first;
       do {
-        ir_instruction *const result =
+        ir_rvalue *const result =
            ((ast_node *) ptr)->hir(instructions, state);
         ptr = ptr->next;
 
@@ -63,6 +63,35 @@ process_call(exec_list *instructions, ir_function *f,
    (void) instructions;
 
    if (sig != NULL) {
+      /* Verify that 'out' and 'inout' actual parameters are lvalues.  This
+       * isn't done in ir_function::matching_signature because that function
+       * cannot generate the necessary diagnostics.
+       */
+      exec_list_iterator actual_iter = actual_parameters->iterator();
+      exec_list_iterator formal_iter = sig->parameters.iterator();
+
+      while (actual_iter.has_next()) {
+        ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
+        ir_variable *formal = (ir_variable *) formal_iter.get();
+
+        assert(actual != NULL);
+        assert(formal != NULL);
+
+        if ((formal->mode == ir_var_out)
+            || (formal->mode == ir_var_inout)) {
+           if (! actual->is_lvalue()) {
+              /* FINISHME: Log a better diagnostic here.  There is no way
+               * FINISHME: to tell the user which parameter is invalid.
+               */
+              _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
+                               (formal->mode == ir_var_out) ? "out" : "inout");
+           }
+        }
+
+        actual_iter.next();
+        formal_iter.next();
+      }
+
       /* FINISHME: The list of actual parameters needs to be modified to
        * FINISHME: include any necessary conversions.
        */
@@ -130,7 +159,7 @@ convert_component(ir_rvalue *src, const glsl_type *desired_type)
         return new ir_expression(ir_unop_f2i, desired_type, src, NULL);
       else {
         assert(b == GLSL_TYPE_BOOL);
-        assert(!"FINISHME: Convert bool to int / uint.");
+        return new ir_expression(ir_unop_f2b, desired_type, src, NULL);
       }
    case GLSL_TYPE_FLOAT:
       switch (b) {
@@ -139,7 +168,7 @@ convert_component(ir_rvalue *src, const glsl_type *desired_type)
       case GLSL_TYPE_INT:
         return new ir_expression(ir_unop_i2f, desired_type, src, NULL);
       case GLSL_TYPE_BOOL:
-        assert(!"FINISHME: Convert bool to float.");
+        return new ir_expression(ir_unop_b2f, desired_type, src, NULL);
       }
       break;
    case GLSL_TYPE_BOOL: {
@@ -188,6 +217,77 @@ dereference_component(ir_rvalue *src, unsigned component)
 }
 
 
+static ir_rvalue *
+process_array_constructor(exec_list *instructions,
+                         const glsl_type *constructor_type,
+                         YYLTYPE *loc, simple_node *parameters,
+                         struct _mesa_glsl_parse_state *state)
+{
+   /* Array constructors come in two forms: sized and unsized.  Sized array
+    * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
+    * variables.  In this case the number of parameters must exactly match the
+    * specified size of the array.
+    *
+    * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
+    * are vec4 variables.  In this case the size of the array being constructed
+    * is determined by the number of parameters.
+    *
+    * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
+    *
+    *    "There must be exactly the same number of arguments as the size of
+    *    the array being constructed. If no size is present in the
+    *    constructor, then the array is explicitly sized to the number of
+    *    arguments provided. The arguments are assigned in order, starting at
+    *    element 0, to the elements of the constructed array. Each argument
+    *    must be the same type as the element type of the array, or be a type
+    *    that can be converted to the element type of the array according to
+    *    Section 4.1.10 "Implicit Conversions.""
+    */
+   exec_list actual_parameters;
+   const unsigned parameter_count =
+      process_parameters(instructions, &actual_parameters, parameters, state);
+
+   if ((parameter_count == 0)
+       || ((constructor_type->length != 0)
+          && (constructor_type->length != parameter_count))) {
+      const unsigned min_param = (constructor_type->length == 0)
+        ? 1 : constructor_type->length;
+
+      _mesa_glsl_error(loc, state, "array constructor must have %s %u "
+                      "parameter%s",
+                      (constructor_type->length != 0) ? "at least" : "exactly",
+                      min_param, (min_param <= 1) ? "" : "s");
+      return ir_call::get_error_instruction();
+   }
+
+   if (constructor_type->length == 0) {
+      constructor_type =
+        glsl_type::get_array_instance(constructor_type->element_type(),
+                                      parameter_count);
+      assert(constructor_type != NULL);
+      assert(constructor_type->length == parameter_count);
+   }
+
+   ir_function *f = state->symbols->get_function(constructor_type->name);
+
+   /* If the constructor for this type of array does not exist, generate the
+    * prototype and add it to the symbol table.  The code will be generated
+    * later.
+    */
+   if (f == NULL) {
+      f = constructor_type->generate_constructor_prototype(state->symbols);
+   }
+
+   ir_rvalue *const r =
+      process_call(instructions, f, loc, &actual_parameters, state);
+
+   assert(r != NULL);
+   assert(r->type->is_error() || (r->type == constructor_type));
+
+   return r;
+}
+
+
 ir_rvalue *
 ast_function_expression::hir(exec_list *instructions,
                             struct _mesa_glsl_parse_state *state)
@@ -224,7 +324,8 @@ ast_function_expression::hir(exec_list *instructions,
            return ir_call::get_error_instruction();
         }
 
-        return ir_call::get_error_instruction();
+        return process_array_constructor(instructions, constructor_type,
+                                         & loc, subexpressions[1], state);
       }
 
       /* There are two kinds of constructor call.  Constructors for built-in