blob: f0c1f0409a07377dfe6eb4e537619f16fbfb47d9 [file] [log] [blame]
Ian Romanick548fa292010-03-15 13:04:13 -07001/*
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 Romanick8bde4ce2010-03-19 11:57:24 -070025#include "glsl_symbol_table.h"
Ian Romanick548fa292010-03-15 13:04:13 -070026#include "ast.h"
27#include "glsl_types.h"
28#include "ir.h"
29
Ian Romanick68515ee2010-03-31 16:28:51 -070030static unsigned
31process_parameters(exec_list *instructions, exec_list *actual_parameters,
Ian Romanick304ea902010-05-10 11:17:53 -070032 exec_list *parameters,
Ian Romanick68515ee2010-03-31 16:28:51 -070033 struct _mesa_glsl_parse_state *state)
34{
Ian Romanick68515ee2010-03-31 16:28:51 -070035 unsigned count = 0;
36
Ian Romanick304ea902010-05-10 11:17:53 -070037 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 Romanick68515ee2010-03-31 16:28:51 -070040
Ian Romanick3521f0b2010-05-10 10:47:14 -070041 actual_parameters->push_tail(result);
42 count++;
Ian Romanick68515ee2010-03-31 16:28:51 -070043 }
44
45 return count;
46}
47
48
49static ir_rvalue *
50process_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 Romanickc35bb002010-04-02 15:51:02 -070061 /* 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 Anholtf1ddca92010-04-07 12:35:34 -070069 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
70 ir_variable *formal = (ir_variable *) formal_iter.get();
Ian Romanickc35bb002010-04-02 15:51:02 -070071
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 Romanick68515ee2010-03-31 16:28:51 -070090 /* 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 Graunkefb9fb5f2010-03-26 00:25:36 -0700107static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -0700108match_function_by_name(exec_list *instructions, const char *name,
Ian Romanickc0771312010-06-09 17:23:26 -0700109 YYLTYPE *loc, exec_list *actual_parameters,
Ian Romanickf4749612010-03-15 13:26:02 -0700110 struct _mesa_glsl_parse_state *state)
111{
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700112 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -0700113
114 if (f == NULL) {
115 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
116 return ir_call::get_error_instruction();
117 }
118
Ian Romanickc0771312010-06-09 17:23:26 -0700119 /* Once we've determined that the function being called might exist, try
120 * to find an overload of the function that matches the parameters.
Ian Romanickf4749612010-03-15 13:26:02 -0700121 */
Ian Romanickc0771312010-06-09 17:23:26 -0700122 return process_call(instructions, f, loc, actual_parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700123}
124
125
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700126/**
127 * Perform automatic type conversion of constructor parameters
128 */
129static ir_rvalue *
130convert_component(ir_rvalue *src, const glsl_type *desired_type)
131{
132 const unsigned a = desired_type->base_type;
133 const unsigned b = src->type->base_type;
Ian Romanick00eb4662010-06-07 15:08:04 -0700134 ir_expression *result = NULL;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700135
136 if (src->type->is_error())
137 return src;
138
139 assert(a <= GLSL_TYPE_BOOL);
140 assert(b <= GLSL_TYPE_BOOL);
141
142 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
143 return src;
144
145 switch (a) {
146 case GLSL_TYPE_UINT:
147 case GLSL_TYPE_INT:
148 if (b == GLSL_TYPE_FLOAT)
Ian Romanick00eb4662010-06-07 15:08:04 -0700149 result = new ir_expression(ir_unop_f2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700150 else {
151 assert(b == GLSL_TYPE_BOOL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700152 result = new ir_expression(ir_unop_b2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700153 }
Ian Romanick565185c2010-06-11 13:49:00 -0700154 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700155 case GLSL_TYPE_FLOAT:
156 switch (b) {
157 case GLSL_TYPE_UINT:
Ian Romanick00eb4662010-06-07 15:08:04 -0700158 result = new ir_expression(ir_unop_u2f, desired_type, src, NULL);
159 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700160 case GLSL_TYPE_INT:
Ian Romanick00eb4662010-06-07 15:08:04 -0700161 result = new ir_expression(ir_unop_i2f, desired_type, src, NULL);
162 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700163 case GLSL_TYPE_BOOL:
Ian Romanick00eb4662010-06-07 15:08:04 -0700164 result = new ir_expression(ir_unop_b2f, desired_type, src, NULL);
165 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700166 }
167 break;
168 case GLSL_TYPE_BOOL: {
169 int z = 0;
170 ir_constant *const zero = new ir_constant(src->type, &z);
171
Ian Romanick00eb4662010-06-07 15:08:04 -0700172 result = new ir_expression(ir_binop_nequal, desired_type, src, zero);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700173 }
174 }
175
Ian Romanick00eb4662010-06-07 15:08:04 -0700176 assert(result != NULL);
177
178 ir_constant *const constant = result->constant_expression_value();
179 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700180}
181
182
183/**
184 * Dereference a specific component from a scalar, vector, or matrix
185 */
186static ir_rvalue *
187dereference_component(ir_rvalue *src, unsigned component)
188{
189 assert(component < src->type->components());
190
Ian Romanickc9cb1032010-06-04 16:20:35 -0700191 /* If the source is a constant, just create a new constant instead of a
192 * dereference of the existing constant.
193 */
194 ir_constant *constant = src->as_constant();
195 if (constant)
196 return new ir_constant(constant, component);
197
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700198 if (src->type->is_scalar()) {
199 return src;
200 } else if (src->type->is_vector()) {
201 return new ir_swizzle(src, component, 0, 0, 0, 1);
202 } else {
203 assert(src->type->is_matrix());
204
205 /* Dereference a row of the matrix, then call this function again to get
206 * a specific element from that row.
207 */
208 const int c = component / src->type->column_type()->vector_elements;
209 const int r = component % src->type->column_type()->vector_elements;
210 ir_constant *const col_index = new ir_constant(glsl_type::int_type, &c);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200211 ir_dereference *const col = new ir_dereference_array(src, col_index);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700212
213 col->type = src->type->column_type();
214
215 return dereference_component(col, r);
216 }
217
218 assert(!"Should not get here.");
219 return NULL;
220}
221
222
Ian Romanick00aa1732010-03-31 16:48:48 -0700223static ir_rvalue *
224process_array_constructor(exec_list *instructions,
225 const glsl_type *constructor_type,
Ian Romanick304ea902010-05-10 11:17:53 -0700226 YYLTYPE *loc, exec_list *parameters,
Ian Romanick00aa1732010-03-31 16:48:48 -0700227 struct _mesa_glsl_parse_state *state)
228{
229 /* Array constructors come in two forms: sized and unsized. Sized array
230 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
231 * variables. In this case the number of parameters must exactly match the
232 * specified size of the array.
233 *
234 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
235 * are vec4 variables. In this case the size of the array being constructed
236 * is determined by the number of parameters.
237 *
238 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
239 *
240 * "There must be exactly the same number of arguments as the size of
241 * the array being constructed. If no size is present in the
242 * constructor, then the array is explicitly sized to the number of
243 * arguments provided. The arguments are assigned in order, starting at
244 * element 0, to the elements of the constructed array. Each argument
245 * must be the same type as the element type of the array, or be a type
246 * that can be converted to the element type of the array according to
247 * Section 4.1.10 "Implicit Conversions.""
248 */
249 exec_list actual_parameters;
250 const unsigned parameter_count =
251 process_parameters(instructions, &actual_parameters, parameters, state);
252
253 if ((parameter_count == 0)
254 || ((constructor_type->length != 0)
255 && (constructor_type->length != parameter_count))) {
256 const unsigned min_param = (constructor_type->length == 0)
257 ? 1 : constructor_type->length;
258
259 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
260 "parameter%s",
261 (constructor_type->length != 0) ? "at least" : "exactly",
262 min_param, (min_param <= 1) ? "" : "s");
263 return ir_call::get_error_instruction();
264 }
265
266 if (constructor_type->length == 0) {
267 constructor_type =
Ian Romanickcb9cba22010-04-02 16:08:44 -0700268 glsl_type::get_array_instance(constructor_type->element_type(),
Ian Romanick00aa1732010-03-31 16:48:48 -0700269 parameter_count);
270 assert(constructor_type != NULL);
271 assert(constructor_type->length == parameter_count);
272 }
273
274 ir_function *f = state->symbols->get_function(constructor_type->name);
275
276 /* If the constructor for this type of array does not exist, generate the
Ian Romanick82baaf42010-04-23 13:21:22 -0700277 * prototype and add it to the symbol table.
Ian Romanick00aa1732010-03-31 16:48:48 -0700278 */
279 if (f == NULL) {
Ian Romanick82baaf42010-04-23 13:21:22 -0700280 f = constructor_type->generate_constructor(state->symbols);
Ian Romanick00aa1732010-03-31 16:48:48 -0700281 }
282
283 ir_rvalue *const r =
284 process_call(instructions, f, loc, &actual_parameters, state);
285
286 assert(r != NULL);
287 assert(r->type->is_error() || (r->type == constructor_type));
288
289 return r;
290}
291
292
Ian Romanickab92d0e2010-06-09 17:26:20 -0700293/**
294 * Try to convert a record constructor to a constant expression
295 */
296static ir_constant *
297constant_record_constructor(const glsl_type *constructor_type,
298 YYLTYPE *loc, exec_list *parameters,
299 struct _mesa_glsl_parse_state *state)
300{
301 bool all_parameters_are_constant = true;
302
303 exec_node *node = parameters->head;
304 for (unsigned i = 0; i < constructor_type->length; i++) {
305 ir_instruction *ir = (ir_instruction *) node;
306
307 if (node->is_tail_sentinal()) {
308 _mesa_glsl_error(loc, state,
309 "insufficient parameters to constructor for `%s'",
310 constructor_type->name);
311 return NULL;
312 }
313
314 if (ir->type != constructor_type->fields.structure[i].type) {
315 _mesa_glsl_error(loc, state,
316 "parameter type mismatch in constructor for `%s' "
317 " (%s vs %s)",
318 constructor_type->name,
319 ir->type->name,
320 constructor_type->fields.structure[i].type->name);
321 return NULL;
322 }
323
324 if (ir->as_constant() == NULL)
325 all_parameters_are_constant = false;
326
327 node = node->next;
328 }
329
330 if (!all_parameters_are_constant)
331 return NULL;
332
333 return new ir_constant(constructor_type, parameters);
334}
335
336
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700337ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700338ast_function_expression::hir(exec_list *instructions,
339 struct _mesa_glsl_parse_state *state)
340{
341 /* There are three sorts of function calls.
342 *
343 * 1. contstructors - The first subexpression is an ast_type_specifier.
344 * 2. methods - Only the .length() method of array types.
345 * 3. functions - Calls to regular old functions.
346 *
Ian Romanick548fa292010-03-15 13:04:13 -0700347 * Method calls are actually detected when the ast_field_selection
348 * expression is handled.
349 */
350 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700351 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
352 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700353 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700354
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700355 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700356
357
358 /* Constructors for samplers are illegal.
359 */
360 if (constructor_type->is_sampler()) {
361 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
362 constructor_type->name);
363 return ir_call::get_error_instruction();
364 }
365
Ian Romanickb6326ab2010-03-31 16:25:21 -0700366 if (constructor_type->is_array()) {
367 if (state->language_version <= 110) {
368 _mesa_glsl_error(& loc, state,
369 "array constructors forbidden in GLSL 1.10");
370 return ir_call::get_error_instruction();
371 }
372
Ian Romanick00aa1732010-03-31 16:48:48 -0700373 return process_array_constructor(instructions, constructor_type,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700374 & loc, &this->expressions, state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700375 }
Ian Romanickabef9552010-03-23 15:08:30 -0700376
377 /* There are two kinds of constructor call. Constructors for built-in
378 * language types, such as mat4 and vec2, are free form. The only
379 * requirement is that the parameters must provide enough values of the
380 * correct scalar type. Constructors for arrays and structures must
381 * have the exact number of parameters with matching types in the
382 * correct order. These constructors follow essentially the same type
383 * matching rules as functions.
384 */
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700385 if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
386 /* Constructing a numeric type has a couple steps. First all values
387 * passed to the constructor are broken into individual parameters
388 * and type converted to the base type of the thing being constructed.
389 *
390 * At that point we have some number of values that match the base
391 * type of the thing being constructed. Now the constructor can be
392 * treated like a function call. Each numeric type has a small set
393 * of constructor functions. The set of new parameters will either
394 * match one of those functions or the original constructor is
395 * invalid.
396 */
397 const glsl_type *const base_type = constructor_type->get_base_type();
398
399 /* Total number of components of the type being constructed.
400 */
401 const unsigned type_components = constructor_type->components();
402
403 /* Number of components from parameters that have actually been
404 * consumed. This is used to perform several kinds of error checking.
405 */
406 unsigned components_used = 0;
407
408 unsigned matrix_parameters = 0;
409 unsigned nonmatrix_parameters = 0;
410 exec_list actual_parameters;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700411
Ian Romanick9e08d012010-06-04 16:36:09 -0700412 bool all_parameters_are_constant = true;
413
Ian Romanick304ea902010-05-10 11:17:53 -0700414 assert(!this->expressions.is_empty());
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700415
Ian Romanick304ea902010-05-10 11:17:53 -0700416 foreach_list (n, &this->expressions) {
417 ast_node *ast = exec_node_data(ast_node, n, link);
Ian Romanick9e08d012010-06-04 16:36:09 -0700418 ir_rvalue *result =
Ian Romanick304ea902010-05-10 11:17:53 -0700419 ast->hir(instructions, state)->as_rvalue();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700420
Ian Romanick9e08d012010-06-04 16:36:09 -0700421 /* Attempt to convert the parameter to a constant valued expression.
422 * After doing so, track whether or not all the parameters to the
423 * constructor are trivially constant valued expressions.
424 */
425 ir_rvalue *const constant =
426 result->constant_expression_value();
427
428 if (constant != NULL)
429 result = constant;
430 else
431 all_parameters_are_constant = false;
432
Ian Romanick3521f0b2010-05-10 10:47:14 -0700433 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
434 *
435 * "It is an error to provide extra arguments beyond this
436 * last used argument."
437 */
438 if (components_used >= type_components) {
439 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
440 "constructor",
441 constructor_type->name);
442 return ir_call::get_error_instruction();
443 }
444
445 if (!result->type->is_numeric() && !result->type->is_boolean()) {
446 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
447 "non-numeric data type",
448 constructor_type->name);
449 return ir_call::get_error_instruction();
450 }
451
452 /* Count the number of matrix and nonmatrix parameters. This
453 * is used below to enforce some of the constructor rules.
454 */
455 if (result->type->is_matrix())
456 matrix_parameters++;
457 else
458 nonmatrix_parameters++;
459
460
461 /* Process each of the components of the parameter. Dereference
462 * each component individually, perform any type conversions, and
463 * add it to the parameter list for the constructor.
464 */
465 for (unsigned i = 0; i < result->type->components(); i++) {
466 if (components_used >= type_components)
467 break;
468
469 ir_rvalue *const component =
470 convert_component(dereference_component(result, i),
471 base_type);
472
473 /* All cases that could result in component->type being the
474 * error type should have already been caught above.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700475 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700476 assert(component->type == base_type);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700477
Ian Romanick9e08d012010-06-04 16:36:09 -0700478 if (component->as_constant() == NULL)
479 all_parameters_are_constant = false;
480
Ian Romanick3521f0b2010-05-10 10:47:14 -0700481 /* Don't actually generate constructor calls for scalars.
482 * Instead, do the usual component selection and conversion,
483 * and return the single component.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700484 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700485 if (constructor_type->is_scalar())
486 return component;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700487
Ian Romanick3521f0b2010-05-10 10:47:14 -0700488 actual_parameters.push_tail(component);
489 components_used++;
490 }
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700491 }
492
493 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
494 *
495 * "It is an error to construct matrices from other matrices. This
496 * is reserved for future use."
497 */
498 if ((state->language_version <= 110) && (matrix_parameters > 0)
499 && constructor_type->is_matrix()) {
500 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
501 "matrix in GLSL 1.10",
502 constructor_type->name);
503 return ir_call::get_error_instruction();
504 }
505
506 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
507 *
508 * "If a matrix argument is given to a matrix constructor, it is
509 * an error to have any other arguments."
510 */
511 if ((matrix_parameters > 0)
512 && ((matrix_parameters + nonmatrix_parameters) > 1)
513 && constructor_type->is_matrix()) {
514 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
515 "matrix must be only parameter",
516 constructor_type->name);
517 return ir_call::get_error_instruction();
518 }
519
520 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
521 *
522 * "In these cases, there must be enough components provided in the
523 * arguments to provide an initializer for every component in the
524 * constructed value."
525 */
Ian Romanick8a24cd52010-03-29 15:36:02 -0700526 if ((components_used < type_components) && (components_used != 1)) {
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700527 _mesa_glsl_error(& loc, state, "too few components to construct "
528 "`%s'",
529 constructor_type->name);
530 return ir_call::get_error_instruction();
531 }
532
533 ir_function *f = state->symbols->get_function(constructor_type->name);
534 if (f == NULL) {
535 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
536 constructor_type->name);
537 return ir_call::get_error_instruction();
538 }
539
540 const ir_function_signature *sig =
541 f->matching_signature(& actual_parameters);
542 if (sig != NULL) {
Ian Romanick9e08d012010-06-04 16:36:09 -0700543 /* If all of the parameters are trivially constant, create a
544 * constant representing the complete collection of parameters.
545 */
546 if (all_parameters_are_constant
547 && (sig->return_type->is_scalar()
548 || sig->return_type->is_vector()
549 || sig->return_type->is_matrix())
550 && (components_used >= type_components))
551 return new ir_constant(sig->return_type, & actual_parameters);
552 else
553 return new ir_call(sig, & actual_parameters);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700554 } else {
555 /* FINISHME: Log a better error message here. G++ will show the
556 * FINSIHME: types of the actual parameters and the set of
557 * FINSIHME: candidate functions. A different error should also be
558 * FINSIHME: logged when multiple functions match.
559 */
560 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
561 constructor_type->name);
562 return ir_call::get_error_instruction();
563 }
564 }
Ian Romanickabef9552010-03-23 15:08:30 -0700565
Ian Romanick548fa292010-03-15 13:04:13 -0700566 return ir_call::get_error_instruction();
567 } else {
568 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -0700569 YYLTYPE loc = id->get_location();
Ian Romanickc0771312010-06-09 17:23:26 -0700570 exec_list actual_parameters;
571
572 process_parameters(instructions, &actual_parameters, &this->expressions,
573 state);
Ian Romanick548fa292010-03-15 13:04:13 -0700574
Ian Romanickab92d0e2010-06-09 17:26:20 -0700575 const glsl_type *const type =
576 state->symbols->get_type(id->primary_expression.identifier);
577
578 if ((type != NULL) && type->is_record()) {
579 ir_constant *constant =
580 constant_record_constructor(type, &loc, &actual_parameters, state);
581
582 if (constant != NULL)
583 return constant;
584 }
585
Ian Romanickf4749612010-03-15 13:26:02 -0700586 return match_function_by_name(instructions,
587 id->primary_expression.identifier, & loc,
Ian Romanickc0771312010-06-09 17:23:26 -0700588 &actual_parameters, state);
Ian Romanick548fa292010-03-15 13:04:13 -0700589 }
590
591 return ir_call::get_error_instruction();
592}