Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <cstdio> |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 25 | #include "glsl_symbol_table.h" |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 26 | #include "ast.h" |
| 27 | #include "glsl_types.h" |
| 28 | #include "ir.h" |
| 29 | |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 30 | static unsigned |
| 31 | process_parameters(exec_list *instructions, exec_list *actual_parameters, |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 32 | exec_list *parameters, |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 33 | struct _mesa_glsl_parse_state *state) |
| 34 | { |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 35 | unsigned count = 0; |
| 36 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 37 | foreach_list (n, parameters) { |
| 38 | ast_node *const ast = exec_node_data(ast_node, n, link); |
| 39 | ir_rvalue *const result = ast->hir(instructions, state); |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 40 | |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 41 | actual_parameters->push_tail(result); |
| 42 | count++; |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | return count; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | static ir_rvalue * |
| 50 | process_call(exec_list *instructions, ir_function *f, |
| 51 | YYLTYPE *loc, exec_list *actual_parameters, |
| 52 | struct _mesa_glsl_parse_state *state) |
| 53 | { |
| 54 | const ir_function_signature *sig = |
| 55 | f->matching_signature(actual_parameters); |
| 56 | |
| 57 | /* The instructions param will be used when the FINISHMEs below are done */ |
| 58 | (void) instructions; |
| 59 | |
| 60 | if (sig != NULL) { |
Ian Romanick | c35bb00 | 2010-04-02 15:51:02 -0700 | [diff] [blame] | 61 | /* Verify that 'out' and 'inout' actual parameters are lvalues. This |
| 62 | * isn't done in ir_function::matching_signature because that function |
| 63 | * cannot generate the necessary diagnostics. |
| 64 | */ |
| 65 | exec_list_iterator actual_iter = actual_parameters->iterator(); |
| 66 | exec_list_iterator formal_iter = sig->parameters.iterator(); |
| 67 | |
| 68 | while (actual_iter.has_next()) { |
Eric Anholt | f1ddca9 | 2010-04-07 12:35:34 -0700 | [diff] [blame] | 69 | ir_rvalue *actual = (ir_rvalue *) actual_iter.get(); |
| 70 | ir_variable *formal = (ir_variable *) formal_iter.get(); |
Ian Romanick | c35bb00 | 2010-04-02 15:51:02 -0700 | [diff] [blame] | 71 | |
| 72 | assert(actual != NULL); |
| 73 | assert(formal != NULL); |
| 74 | |
| 75 | if ((formal->mode == ir_var_out) |
| 76 | || (formal->mode == ir_var_inout)) { |
| 77 | if (! actual->is_lvalue()) { |
| 78 | /* FINISHME: Log a better diagnostic here. There is no way |
| 79 | * FINISHME: to tell the user which parameter is invalid. |
| 80 | */ |
| 81 | _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue", |
| 82 | (formal->mode == ir_var_out) ? "out" : "inout"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | actual_iter.next(); |
| 87 | formal_iter.next(); |
| 88 | } |
| 89 | |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 90 | /* FINISHME: The list of actual parameters needs to be modified to |
| 91 | * FINISHME: include any necessary conversions. |
| 92 | */ |
| 93 | return new ir_call(sig, actual_parameters); |
| 94 | } else { |
| 95 | /* FINISHME: Log a better error message here. G++ will show the types |
| 96 | * FINISHME: of the actual parameters and the set of candidate |
| 97 | * FINISHME: functions. A different error should also be logged when |
| 98 | * FINISHME: multiple functions match. |
| 99 | */ |
| 100 | _mesa_glsl_error(loc, state, "no matching function for call to `%s'", |
| 101 | f->name); |
| 102 | return ir_call::get_error_instruction(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 107 | static ir_rvalue * |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 108 | match_function_by_name(exec_list *instructions, const char *name, |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 109 | YYLTYPE *loc, exec_list *parameters, |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 110 | struct _mesa_glsl_parse_state *state) |
| 111 | { |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 112 | ir_function *f = state->symbols->get_function(name); |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 113 | |
| 114 | if (f == NULL) { |
| 115 | _mesa_glsl_error(loc, state, "function `%s' undeclared", name); |
| 116 | return ir_call::get_error_instruction(); |
| 117 | } |
| 118 | |
| 119 | /* Once we've determined that the function being called might exist, |
| 120 | * process the parameters. |
| 121 | */ |
| 122 | exec_list actual_parameters; |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 123 | process_parameters(instructions, &actual_parameters, parameters, state); |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 124 | |
| 125 | /* After processing the function's actual parameters, try to find an |
| 126 | * overload of the function that matches. |
| 127 | */ |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 128 | return process_call(instructions, f, loc, &actual_parameters, state); |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Perform automatic type conversion of constructor parameters |
| 134 | */ |
| 135 | static ir_rvalue * |
| 136 | convert_component(ir_rvalue *src, const glsl_type *desired_type) |
| 137 | { |
| 138 | const unsigned a = desired_type->base_type; |
| 139 | const unsigned b = src->type->base_type; |
| 140 | |
| 141 | if (src->type->is_error()) |
| 142 | return src; |
| 143 | |
| 144 | assert(a <= GLSL_TYPE_BOOL); |
| 145 | assert(b <= GLSL_TYPE_BOOL); |
| 146 | |
| 147 | if ((a == b) || (src->type->is_integer() && desired_type->is_integer())) |
| 148 | return src; |
| 149 | |
| 150 | switch (a) { |
| 151 | case GLSL_TYPE_UINT: |
| 152 | case GLSL_TYPE_INT: |
| 153 | if (b == GLSL_TYPE_FLOAT) |
| 154 | return new ir_expression(ir_unop_f2i, desired_type, src, NULL); |
| 155 | else { |
| 156 | assert(b == GLSL_TYPE_BOOL); |
Ian Romanick | 565185c | 2010-06-11 13:49:00 -0700 | [diff] [blame^] | 157 | return new ir_expression(ir_unop_b2i, desired_type, src, NULL); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 158 | } |
Ian Romanick | 565185c | 2010-06-11 13:49:00 -0700 | [diff] [blame^] | 159 | break; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 160 | case GLSL_TYPE_FLOAT: |
| 161 | switch (b) { |
| 162 | case GLSL_TYPE_UINT: |
| 163 | return new ir_expression(ir_unop_u2f, desired_type, src, NULL); |
| 164 | case GLSL_TYPE_INT: |
| 165 | return new ir_expression(ir_unop_i2f, desired_type, src, NULL); |
| 166 | case GLSL_TYPE_BOOL: |
Eric Anholt | dc58b3f | 2010-04-02 02:13:43 -1000 | [diff] [blame] | 167 | return new ir_expression(ir_unop_b2f, desired_type, src, NULL); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 168 | } |
| 169 | break; |
| 170 | case GLSL_TYPE_BOOL: { |
| 171 | int z = 0; |
| 172 | ir_constant *const zero = new ir_constant(src->type, &z); |
| 173 | |
| 174 | return new ir_expression(ir_binop_nequal, desired_type, src, zero); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | assert(!"Should not get here."); |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * Dereference a specific component from a scalar, vector, or matrix |
| 185 | */ |
| 186 | static ir_rvalue * |
| 187 | dereference_component(ir_rvalue *src, unsigned component) |
| 188 | { |
| 189 | assert(component < src->type->components()); |
| 190 | |
| 191 | if (src->type->is_scalar()) { |
| 192 | return src; |
| 193 | } else if (src->type->is_vector()) { |
| 194 | return new ir_swizzle(src, component, 0, 0, 0, 1); |
| 195 | } else { |
| 196 | assert(src->type->is_matrix()); |
| 197 | |
| 198 | /* Dereference a row of the matrix, then call this function again to get |
| 199 | * a specific element from that row. |
| 200 | */ |
| 201 | const int c = component / src->type->column_type()->vector_elements; |
| 202 | const int r = component % src->type->column_type()->vector_elements; |
| 203 | ir_constant *const col_index = new ir_constant(glsl_type::int_type, &c); |
Ian Romanick | 70fe8b6 | 2010-05-19 11:37:35 +0200 | [diff] [blame] | 204 | ir_dereference *const col = new ir_dereference_array(src, col_index); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 205 | |
| 206 | col->type = src->type->column_type(); |
| 207 | |
| 208 | return dereference_component(col, r); |
| 209 | } |
| 210 | |
| 211 | assert(!"Should not get here."); |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 216 | static ir_rvalue * |
| 217 | process_array_constructor(exec_list *instructions, |
| 218 | const glsl_type *constructor_type, |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 219 | YYLTYPE *loc, exec_list *parameters, |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 220 | struct _mesa_glsl_parse_state *state) |
| 221 | { |
| 222 | /* Array constructors come in two forms: sized and unsized. Sized array |
| 223 | * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4 |
| 224 | * variables. In this case the number of parameters must exactly match the |
| 225 | * specified size of the array. |
| 226 | * |
| 227 | * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b' |
| 228 | * are vec4 variables. In this case the size of the array being constructed |
| 229 | * is determined by the number of parameters. |
| 230 | * |
| 231 | * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec: |
| 232 | * |
| 233 | * "There must be exactly the same number of arguments as the size of |
| 234 | * the array being constructed. If no size is present in the |
| 235 | * constructor, then the array is explicitly sized to the number of |
| 236 | * arguments provided. The arguments are assigned in order, starting at |
| 237 | * element 0, to the elements of the constructed array. Each argument |
| 238 | * must be the same type as the element type of the array, or be a type |
| 239 | * that can be converted to the element type of the array according to |
| 240 | * Section 4.1.10 "Implicit Conversions."" |
| 241 | */ |
| 242 | exec_list actual_parameters; |
| 243 | const unsigned parameter_count = |
| 244 | process_parameters(instructions, &actual_parameters, parameters, state); |
| 245 | |
| 246 | if ((parameter_count == 0) |
| 247 | || ((constructor_type->length != 0) |
| 248 | && (constructor_type->length != parameter_count))) { |
| 249 | const unsigned min_param = (constructor_type->length == 0) |
| 250 | ? 1 : constructor_type->length; |
| 251 | |
| 252 | _mesa_glsl_error(loc, state, "array constructor must have %s %u " |
| 253 | "parameter%s", |
| 254 | (constructor_type->length != 0) ? "at least" : "exactly", |
| 255 | min_param, (min_param <= 1) ? "" : "s"); |
| 256 | return ir_call::get_error_instruction(); |
| 257 | } |
| 258 | |
| 259 | if (constructor_type->length == 0) { |
| 260 | constructor_type = |
Ian Romanick | cb9cba2 | 2010-04-02 16:08:44 -0700 | [diff] [blame] | 261 | glsl_type::get_array_instance(constructor_type->element_type(), |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 262 | parameter_count); |
| 263 | assert(constructor_type != NULL); |
| 264 | assert(constructor_type->length == parameter_count); |
| 265 | } |
| 266 | |
| 267 | ir_function *f = state->symbols->get_function(constructor_type->name); |
| 268 | |
| 269 | /* If the constructor for this type of array does not exist, generate the |
Ian Romanick | 82baaf4 | 2010-04-23 13:21:22 -0700 | [diff] [blame] | 270 | * prototype and add it to the symbol table. |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 271 | */ |
| 272 | if (f == NULL) { |
Ian Romanick | 82baaf4 | 2010-04-23 13:21:22 -0700 | [diff] [blame] | 273 | f = constructor_type->generate_constructor(state->symbols); |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | ir_rvalue *const r = |
| 277 | process_call(instructions, f, loc, &actual_parameters, state); |
| 278 | |
| 279 | assert(r != NULL); |
| 280 | assert(r->type->is_error() || (r->type == constructor_type)); |
| 281 | |
| 282 | return r; |
| 283 | } |
| 284 | |
| 285 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 286 | ir_rvalue * |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 287 | ast_function_expression::hir(exec_list *instructions, |
| 288 | struct _mesa_glsl_parse_state *state) |
| 289 | { |
| 290 | /* There are three sorts of function calls. |
| 291 | * |
| 292 | * 1. contstructors - The first subexpression is an ast_type_specifier. |
| 293 | * 2. methods - Only the .length() method of array types. |
| 294 | * 3. functions - Calls to regular old functions. |
| 295 | * |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 296 | * Method calls are actually detected when the ast_field_selection |
| 297 | * expression is handled. |
| 298 | */ |
| 299 | if (is_constructor()) { |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 300 | const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0]; |
| 301 | YYLTYPE loc = type->get_location(); |
Ian Romanick | 3e0ef5f | 2010-03-31 16:22:56 -0700 | [diff] [blame] | 302 | const char *name; |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 303 | |
Ian Romanick | 3e0ef5f | 2010-03-31 16:22:56 -0700 | [diff] [blame] | 304 | const glsl_type *const constructor_type = type->glsl_type(& name, state); |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 305 | |
| 306 | |
| 307 | /* Constructors for samplers are illegal. |
| 308 | */ |
| 309 | if (constructor_type->is_sampler()) { |
| 310 | _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'", |
| 311 | constructor_type->name); |
| 312 | return ir_call::get_error_instruction(); |
| 313 | } |
| 314 | |
Ian Romanick | b6326ab | 2010-03-31 16:25:21 -0700 | [diff] [blame] | 315 | if (constructor_type->is_array()) { |
| 316 | if (state->language_version <= 110) { |
| 317 | _mesa_glsl_error(& loc, state, |
| 318 | "array constructors forbidden in GLSL 1.10"); |
| 319 | return ir_call::get_error_instruction(); |
| 320 | } |
| 321 | |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 322 | return process_array_constructor(instructions, constructor_type, |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 323 | & loc, &this->expressions, state); |
Ian Romanick | b6326ab | 2010-03-31 16:25:21 -0700 | [diff] [blame] | 324 | } |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 325 | |
| 326 | /* There are two kinds of constructor call. Constructors for built-in |
| 327 | * language types, such as mat4 and vec2, are free form. The only |
| 328 | * requirement is that the parameters must provide enough values of the |
| 329 | * correct scalar type. Constructors for arrays and structures must |
| 330 | * have the exact number of parameters with matching types in the |
| 331 | * correct order. These constructors follow essentially the same type |
| 332 | * matching rules as functions. |
| 333 | */ |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 334 | if (constructor_type->is_numeric() || constructor_type->is_boolean()) { |
| 335 | /* Constructing a numeric type has a couple steps. First all values |
| 336 | * passed to the constructor are broken into individual parameters |
| 337 | * and type converted to the base type of the thing being constructed. |
| 338 | * |
| 339 | * At that point we have some number of values that match the base |
| 340 | * type of the thing being constructed. Now the constructor can be |
| 341 | * treated like a function call. Each numeric type has a small set |
| 342 | * of constructor functions. The set of new parameters will either |
| 343 | * match one of those functions or the original constructor is |
| 344 | * invalid. |
| 345 | */ |
| 346 | const glsl_type *const base_type = constructor_type->get_base_type(); |
| 347 | |
| 348 | /* Total number of components of the type being constructed. |
| 349 | */ |
| 350 | const unsigned type_components = constructor_type->components(); |
| 351 | |
| 352 | /* Number of components from parameters that have actually been |
| 353 | * consumed. This is used to perform several kinds of error checking. |
| 354 | */ |
| 355 | unsigned components_used = 0; |
| 356 | |
| 357 | unsigned matrix_parameters = 0; |
| 358 | unsigned nonmatrix_parameters = 0; |
| 359 | exec_list actual_parameters; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 360 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 361 | assert(!this->expressions.is_empty()); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 362 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 363 | foreach_list (n, &this->expressions) { |
| 364 | ast_node *ast = exec_node_data(ast_node, n, link); |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 365 | ir_rvalue *const result = |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 366 | ast->hir(instructions, state)->as_rvalue(); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 367 | |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 368 | /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec: |
| 369 | * |
| 370 | * "It is an error to provide extra arguments beyond this |
| 371 | * last used argument." |
| 372 | */ |
| 373 | if (components_used >= type_components) { |
| 374 | _mesa_glsl_error(& loc, state, "too many parameters to `%s' " |
| 375 | "constructor", |
| 376 | constructor_type->name); |
| 377 | return ir_call::get_error_instruction(); |
| 378 | } |
| 379 | |
| 380 | if (!result->type->is_numeric() && !result->type->is_boolean()) { |
| 381 | _mesa_glsl_error(& loc, state, "cannot construct `%s' from a " |
| 382 | "non-numeric data type", |
| 383 | constructor_type->name); |
| 384 | return ir_call::get_error_instruction(); |
| 385 | } |
| 386 | |
| 387 | /* Count the number of matrix and nonmatrix parameters. This |
| 388 | * is used below to enforce some of the constructor rules. |
| 389 | */ |
| 390 | if (result->type->is_matrix()) |
| 391 | matrix_parameters++; |
| 392 | else |
| 393 | nonmatrix_parameters++; |
| 394 | |
| 395 | |
| 396 | /* Process each of the components of the parameter. Dereference |
| 397 | * each component individually, perform any type conversions, and |
| 398 | * add it to the parameter list for the constructor. |
| 399 | */ |
| 400 | for (unsigned i = 0; i < result->type->components(); i++) { |
| 401 | if (components_used >= type_components) |
| 402 | break; |
| 403 | |
| 404 | ir_rvalue *const component = |
| 405 | convert_component(dereference_component(result, i), |
| 406 | base_type); |
| 407 | |
| 408 | /* All cases that could result in component->type being the |
| 409 | * error type should have already been caught above. |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 410 | */ |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 411 | assert(component->type == base_type); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 412 | |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 413 | /* Don't actually generate constructor calls for scalars. |
| 414 | * Instead, do the usual component selection and conversion, |
| 415 | * and return the single component. |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 416 | */ |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 417 | if (constructor_type->is_scalar()) |
| 418 | return component; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 419 | |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 420 | actual_parameters.push_tail(component); |
| 421 | components_used++; |
| 422 | } |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec: |
| 426 | * |
| 427 | * "It is an error to construct matrices from other matrices. This |
| 428 | * is reserved for future use." |
| 429 | */ |
| 430 | if ((state->language_version <= 110) && (matrix_parameters > 0) |
| 431 | && constructor_type->is_matrix()) { |
| 432 | _mesa_glsl_error(& loc, state, "cannot construct `%s' from a " |
| 433 | "matrix in GLSL 1.10", |
| 434 | constructor_type->name); |
| 435 | return ir_call::get_error_instruction(); |
| 436 | } |
| 437 | |
| 438 | /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec: |
| 439 | * |
| 440 | * "If a matrix argument is given to a matrix constructor, it is |
| 441 | * an error to have any other arguments." |
| 442 | */ |
| 443 | if ((matrix_parameters > 0) |
| 444 | && ((matrix_parameters + nonmatrix_parameters) > 1) |
| 445 | && constructor_type->is_matrix()) { |
| 446 | _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, " |
| 447 | "matrix must be only parameter", |
| 448 | constructor_type->name); |
| 449 | return ir_call::get_error_instruction(); |
| 450 | } |
| 451 | |
| 452 | /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec: |
| 453 | * |
| 454 | * "In these cases, there must be enough components provided in the |
| 455 | * arguments to provide an initializer for every component in the |
| 456 | * constructed value." |
| 457 | */ |
Ian Romanick | 8a24cd5 | 2010-03-29 15:36:02 -0700 | [diff] [blame] | 458 | if ((components_used < type_components) && (components_used != 1)) { |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 459 | _mesa_glsl_error(& loc, state, "too few components to construct " |
| 460 | "`%s'", |
| 461 | constructor_type->name); |
| 462 | return ir_call::get_error_instruction(); |
| 463 | } |
| 464 | |
| 465 | ir_function *f = state->symbols->get_function(constructor_type->name); |
| 466 | if (f == NULL) { |
| 467 | _mesa_glsl_error(& loc, state, "no constructor for type `%s'", |
| 468 | constructor_type->name); |
| 469 | return ir_call::get_error_instruction(); |
| 470 | } |
| 471 | |
| 472 | const ir_function_signature *sig = |
| 473 | f->matching_signature(& actual_parameters); |
| 474 | if (sig != NULL) { |
| 475 | return new ir_call(sig, & actual_parameters); |
| 476 | } else { |
| 477 | /* FINISHME: Log a better error message here. G++ will show the |
| 478 | * FINSIHME: types of the actual parameters and the set of |
| 479 | * FINSIHME: candidate functions. A different error should also be |
| 480 | * FINSIHME: logged when multiple functions match. |
| 481 | */ |
| 482 | _mesa_glsl_error(& loc, state, "no matching constructor for `%s'", |
| 483 | constructor_type->name); |
| 484 | return ir_call::get_error_instruction(); |
| 485 | } |
| 486 | } |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 487 | |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 488 | return ir_call::get_error_instruction(); |
| 489 | } else { |
| 490 | const ast_expression *id = subexpressions[0]; |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 491 | YYLTYPE loc = id->get_location(); |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 492 | |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 493 | return match_function_by_name(instructions, |
| 494 | id->primary_expression.identifier, & loc, |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 495 | &this->expressions, state); |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | return ir_call::get_error_instruction(); |
| 499 | } |