glsl: Apply implicit conversions to structure constructor parameters.
[mesa.git] / src / glsl / ir_reader.cpp
index 03dce0d6849541a9ee9eb2bf0fc9b1404a98af5f..e57e03c3078c0b50c3e1a4ae50cd1cce69fc89e0 100644 (file)
@@ -68,7 +68,7 @@ static ir_dereference *read_record_ref(_mesa_glsl_parse_state *, s_list *);
 
 void
 _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
-                  const char *src)
+                  const char *src, bool scan_for_protos)
 {
    s_expression *expr = s_expression::read_expression(state, src);
    if (expr == NULL) {
@@ -76,9 +76,11 @@ _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
       return;
    }
    
-   scan_for_prototypes(state, instructions, expr);
-   if (state->error)
-      return;
+   if (scan_for_protos) {
+      scan_for_prototypes(state, instructions, expr);
+      if (state->error)
+        return;
+   }
 
    read_instructions(state, instructions, expr, NULL);
    talloc_free(expr);
@@ -138,7 +140,7 @@ read_type(_mesa_glsl_parse_state *st, s_expression *expr)
            return NULL;
         }
 
-        return glsl_type::get_array_instance(st, base_type, size->value());
+        return glsl_type::get_array_instance(base_type, size->value());
       } else if (strcmp(type_sym->value(), "struct") == 0) {
         assert(false); // FINISHME
       } else {
@@ -191,7 +193,8 @@ scan_for_prototypes(_mesa_glsl_parse_state *st, exec_list *instructions,
 static ir_function *
 read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
+   bool added = false;
    if (list->length() < 3) {
       ir_read_error(st, list, "Expected (function <name> (signature ...) ...)");
       return NULL;
@@ -206,7 +209,8 @@ read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
    ir_function *f = st->symbols->get_function(name->value());
    if (f == NULL) {
       f = new(ctx) ir_function(name->value());
-      bool added = st->symbols->add_function(name->value(), f);
+      f->is_builtin = true;
+      added = st->symbols->add_function(f->name, f);
       assert(added);
    }
 
@@ -228,14 +232,14 @@ read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
 
       read_function_sig(st, f, siglist, skip_body);
    }
-   return f;
+   return added ? f : NULL;
 }
 
 static void
 read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
                  bool skip_body)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 4) {
       ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
                              "(<instruction> ...))");
@@ -275,7 +279,11 @@ read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
    }
 
    ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
-   if (sig != NULL) {
+   if (sig == NULL && skip_body) {
+      /* If scanning for prototypes, generate a new signature. */
+      sig = new(ctx) ir_function_signature(return_type);
+      f->add_signature(sig);
+   } else if (sig != NULL) {
       const char *badvar = sig->qualifiers_match(&hir_parameters);
       if (badvar != NULL) {
         ir_read_error(st, list, "function `%s' parameter `%s' qualifiers "
@@ -289,13 +297,15 @@ read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
         return;
       }
    } else {
-      sig = new(ctx) ir_function_signature(return_type);
-      f->add_signature(sig);
+      /* No prototype for this body exists - skip it. */
+      st->symbols->pop_scope();
+      return;
    }
+   assert(sig != NULL);
 
    sig->replace_parameters(&hir_parameters);
 
-   if (!skip_body) {
+   if (!skip_body && !body_list->subexpressions.is_empty()) {
       if (sig->is_defined) {
         ir_read_error(st, list, "function %s redefined", f->name);
         return;
@@ -321,11 +331,8 @@ read_instructions(_mesa_glsl_parse_state *st, exec_list *instructions,
    foreach_iter(exec_list_iterator, it, list->subexpressions) {
       s_expression *sub = (s_expression*) it.get();
       ir_instruction *ir = read_instruction(st, sub, loop_ctx);
-      if (ir == NULL) {
-        ir_read_error(st, sub, "Invalid instruction.\n");
-        return;
-      }
-      instructions->push_tail(ir);
+      if (ir != NULL)
+        instructions->push_tail(ir);
    }
 }
 
@@ -334,7 +341,7 @@ static ir_instruction *
 read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
                 ir_loop *loop_ctx)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    s_symbol *symbol = SX_AS_SYMBOL(expr);
    if (symbol != NULL) {
       if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
@@ -344,8 +351,10 @@ read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
    }
 
    s_list *list = SX_AS_LIST(expr);
-   if (list == NULL || list->subexpressions.is_empty())
+   if (list == NULL || list->subexpressions.is_empty()) {
+      ir_read_error(st, expr, "Invalid instruction.\n");
       return NULL;
+   }
 
    s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
    if (tag == NULL) {
@@ -356,6 +365,8 @@ read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
    ir_instruction *inst = NULL;
    if (strcmp(tag->value(), "declare") == 0) {
       inst = read_declaration(st, list);
+   } else if (strcmp(tag->value(), "assign") == 0) {
+      inst = read_assignment(st, list);
    } else if (strcmp(tag->value(), "if") == 0) {
       inst = read_if(st, list, loop_ctx);
    } else if (strcmp(tag->value(), "loop") == 0) {
@@ -376,7 +387,7 @@ read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
 static ir_variable *
 read_declaration(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 4) {
       ir_read_error(st, list, "expected (declare (<qualifiers>) <type> "
                              "<name>)");
@@ -400,7 +411,8 @@ read_declaration(_mesa_glsl_parse_state *st, s_list *list)
       return NULL;
    }
 
-   ir_variable *var = new(ctx) ir_variable(type, var_name->value());
+   ir_variable *var = new(ctx) ir_variable(type, var_name->value(),
+                                          ir_var_auto);
 
    foreach_iter(exec_list_iterator, it, quals->subexpressions) {
       s_symbol *qualifier = SX_AS_SYMBOL(it.get());
@@ -439,7 +451,7 @@ read_declaration(_mesa_glsl_parse_state *st, s_list *list)
    }
 
    // Add the variable to the symbol table
-   st->symbols->add_variable(var_name->value(), var);
+   st->symbols->add_variable(var->name, var);
 
    return var;
 }
@@ -448,7 +460,7 @@ read_declaration(_mesa_glsl_parse_state *st, s_list *list)
 static ir_if *
 read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 4) {
       ir_read_error(st, list, "expected (if <condition> (<then> ...) "
                           "(<else> ...))");
@@ -480,7 +492,7 @@ read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
 static ir_loop *
 read_loop(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 6) {
       ir_read_error(st, list, "expected (loop <counter> <from> <to> "
                              "<increment> <body>)");
@@ -508,7 +520,7 @@ read_loop(_mesa_glsl_parse_state *st, s_list *list)
 static ir_return *
 read_return(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 2) {
       ir_read_error(st, list, "expected (return <rvalue>)");
       return NULL;
@@ -544,8 +556,6 @@ read_rvalue(_mesa_glsl_parse_state *st, s_expression *expr)
       return rvalue;
    else if (strcmp(tag->value(), "swiz") == 0) {
       rvalue = read_swizzle(st, list);
-   } else if (strcmp(tag->value(), "assign") == 0) {
-      rvalue = read_assignment(st, list);
    } else if (strcmp(tag->value(), "expression") == 0) {
       rvalue = read_expression(st, list);
    } else if (strcmp(tag->value(), "call") == 0) {
@@ -564,7 +574,7 @@ read_rvalue(_mesa_glsl_parse_state *st, s_expression *expr)
 static ir_assignment *
 read_assignment(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 4) {
       ir_read_error(st, list, "expected (assign <condition> <lhs> <rhs>)");
       return NULL;
@@ -599,7 +609,7 @@ read_assignment(_mesa_glsl_parse_state *st, s_list *list)
 static ir_call *
 read_call(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 3) {
       ir_read_error(st, list, "expected (call <name> (<param> ...))");
       return NULL;
@@ -631,7 +641,7 @@ read_call(_mesa_glsl_parse_state *st, s_list *list)
       return NULL;
    }
 
-   const ir_function_signature *callee = f->matching_signature(&parameters);
+   ir_function_signature *callee = f->matching_signature(&parameters);
    if (callee == NULL) {
       ir_read_error(st, list, "couldn't find matching signature for function "
                     "%s", name->value());
@@ -644,7 +654,7 @@ read_call(_mesa_glsl_parse_state *st, s_list *list)
 static ir_expression *
 read_expression(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    const unsigned list_length = list->length();
    if (list_length < 4) {
       ir_read_error(st, list, "expected (expression <type> <operator> "
@@ -749,9 +759,9 @@ read_swizzle(_mesa_glsl_parse_state *st, s_list *list)
 static ir_constant *
 read_constant(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 3) {
-      ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
+      ir_read_error(st, list, "expected (constant <type> (...))");
       return NULL;
    }
 
@@ -762,13 +772,38 @@ read_constant(_mesa_glsl_parse_state *st, s_list *list)
 
    s_list *values = SX_AS_LIST(type_expr->next);
    if (values == NULL) {
-      ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
+      ir_read_error(st, list, "expected (constant <type> (...))");
       return NULL;
    }
 
+   if (type->is_array()) {
+      const unsigned elements_supplied = values->length();
+      if (elements_supplied != type->length) {
+        ir_read_error(st, values, "expected exactly %u array elements, "
+                      "given %u", type->length, elements_supplied);
+        return NULL;
+      }
+
+      exec_list elements;
+      foreach_iter(exec_list_iterator, it, values->subexpressions) {
+        s_expression *expr = (s_expression *) it.get();
+        s_list *elt = SX_AS_LIST(expr);
+        if (elt == NULL) {
+           ir_read_error(st, expr, "expected (constant ...) array element");
+           return NULL;
+        }
+
+        ir_constant *ir_elt = read_constant(st, elt);
+        if (ir_elt == NULL)
+           return NULL;
+        elements.push_tail(ir_elt);
+      }
+      return new(ctx) ir_constant(type, &elements);
+   }
+
    const glsl_type *const base_type = type->get_base_type();
 
-   ir_constant_data data;
+   ir_constant_data data = { { 0 } };
 
    // Read in list of values (at most 16).
    int k = 0;
@@ -840,7 +875,7 @@ read_dereference(_mesa_glsl_parse_state *st, s_expression *expr)
 static ir_dereference *
 read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 2) {
       ir_read_error(st, list, "expected (var_ref <variable name>)");
       return NULL;
@@ -863,7 +898,7 @@ read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
 static ir_dereference *
 read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 3) {
       ir_read_error(st, list, "expected (array_ref <rvalue> <index>)");
       return NULL;
@@ -884,7 +919,7 @@ read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
 static ir_dereference *
 read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    if (list->length() != 3) {
       ir_read_error(st, list, "expected (record_ref <rvalue> <field>)");
       return NULL;
@@ -920,7 +955,7 @@ valid_texture_list_length(ir_texture_opcode op, s_list *list)
 static ir_texture *
 read_texture(_mesa_glsl_parse_state *st, s_list *list)
 {
-   void *ctx = talloc_parent(st);
+   void *ctx = st;
    s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
    assert(tag != NULL);