glcpp: Add glcpp_warning for printing warnings to the info log.
[mesa.git] / ast_function.cpp
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>
25 #include "glsl_symbol_table.h"
26 #include "ast.h"
27 #include "glsl_types.h"
28 #include "ir.h"
29
30 static unsigned
31 process_parameters(exec_list *instructions, exec_list *actual_parameters,
32 exec_list *parameters,
33 struct _mesa_glsl_parse_state *state)
34 {
35 unsigned count = 0;
36
37 foreach_list (n, parameters) {
38 ast_node *const ast = exec_node_data(ast_node, n, link);
39 ir_rvalue *result = ast->hir(instructions, state);
40
41 ir_constant *const constant = result->constant_expression_value();
42 if (constant != NULL)
43 result = constant;
44
45 actual_parameters->push_tail(result);
46 count++;
47 }
48
49 return count;
50 }
51
52
53 static ir_rvalue *
54 process_call(exec_list *instructions, ir_function *f,
55 YYLTYPE *loc, exec_list *actual_parameters,
56 struct _mesa_glsl_parse_state *state)
57 {
58 const ir_function_signature *sig =
59 f->matching_signature(actual_parameters);
60
61 /* The instructions param will be used when the FINISHMEs below are done */
62 (void) instructions;
63
64 if (sig != NULL) {
65 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
66 * isn't done in ir_function::matching_signature because that function
67 * cannot generate the necessary diagnostics.
68 */
69 exec_list_iterator actual_iter = actual_parameters->iterator();
70 exec_list_iterator formal_iter = sig->parameters.iterator();
71
72 while (actual_iter.has_next()) {
73 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
74 ir_variable *formal = (ir_variable *) formal_iter.get();
75
76 assert(actual != NULL);
77 assert(formal != NULL);
78
79 if ((formal->mode == ir_var_out)
80 || (formal->mode == ir_var_inout)) {
81 if (! actual->is_lvalue()) {
82 /* FINISHME: Log a better diagnostic here. There is no way
83 * FINISHME: to tell the user which parameter is invalid.
84 */
85 _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
86 (formal->mode == ir_var_out) ? "out" : "inout");
87 }
88 }
89
90 actual_iter.next();
91 formal_iter.next();
92 }
93
94 /* FINISHME: The list of actual parameters needs to be modified to
95 * FINISHME: include any necessary conversions.
96 */
97 return new ir_call(sig, actual_parameters);
98 } else {
99 /* FINISHME: Log a better error message here. G++ will show the types
100 * FINISHME: of the actual parameters and the set of candidate
101 * FINISHME: functions. A different error should also be logged when
102 * FINISHME: multiple functions match.
103 */
104 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
105 f->name);
106 return ir_call::get_error_instruction();
107 }
108 }
109
110
111 static ir_rvalue *
112 match_function_by_name(exec_list *instructions, const char *name,
113 YYLTYPE *loc, exec_list *actual_parameters,
114 struct _mesa_glsl_parse_state *state)
115 {
116 ir_function *f = state->symbols->get_function(name);
117
118 if (f == NULL) {
119 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
120 return ir_call::get_error_instruction();
121 }
122
123 /* Once we've determined that the function being called might exist, try
124 * to find an overload of the function that matches the parameters.
125 */
126 return process_call(instructions, f, loc, actual_parameters, state);
127 }
128
129
130 /**
131 * Perform automatic type conversion of constructor parameters
132 */
133 static ir_rvalue *
134 convert_component(ir_rvalue *src, const glsl_type *desired_type)
135 {
136 const unsigned a = desired_type->base_type;
137 const unsigned b = src->type->base_type;
138 ir_expression *result = NULL;
139
140 if (src->type->is_error())
141 return src;
142
143 assert(a <= GLSL_TYPE_BOOL);
144 assert(b <= GLSL_TYPE_BOOL);
145
146 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
147 return src;
148
149 switch (a) {
150 case GLSL_TYPE_UINT:
151 case GLSL_TYPE_INT:
152 if (b == GLSL_TYPE_FLOAT)
153 result = new ir_expression(ir_unop_f2i, desired_type, src, NULL);
154 else {
155 assert(b == GLSL_TYPE_BOOL);
156 result = new ir_expression(ir_unop_b2i, desired_type, src, NULL);
157 }
158 break;
159 case GLSL_TYPE_FLOAT:
160 switch (b) {
161 case GLSL_TYPE_UINT:
162 result = new ir_expression(ir_unop_u2f, desired_type, src, NULL);
163 break;
164 case GLSL_TYPE_INT:
165 result = new ir_expression(ir_unop_i2f, desired_type, src, NULL);
166 break;
167 case GLSL_TYPE_BOOL:
168 result = new ir_expression(ir_unop_b2f, desired_type, src, NULL);
169 break;
170 }
171 break;
172 case GLSL_TYPE_BOOL: {
173 ir_constant *zero = NULL;
174
175 switch (b) {
176 case GLSL_TYPE_UINT: zero = new ir_constant(unsigned(0)); break;
177 case GLSL_TYPE_INT: zero = new ir_constant(int(0)); break;
178 case GLSL_TYPE_FLOAT: zero = new ir_constant(0.0f); break;
179 }
180
181 result = new ir_expression(ir_binop_nequal, desired_type, src, zero);
182 }
183 }
184
185 assert(result != NULL);
186
187 ir_constant *const constant = result->constant_expression_value();
188 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
189 }
190
191
192 /**
193 * Dereference a specific component from a scalar, vector, or matrix
194 */
195 static ir_rvalue *
196 dereference_component(ir_rvalue *src, unsigned component)
197 {
198 assert(component < src->type->components());
199
200 /* If the source is a constant, just create a new constant instead of a
201 * dereference of the existing constant.
202 */
203 ir_constant *constant = src->as_constant();
204 if (constant)
205 return new ir_constant(constant, component);
206
207 if (src->type->is_scalar()) {
208 return src;
209 } else if (src->type->is_vector()) {
210 return new ir_swizzle(src, component, 0, 0, 0, 1);
211 } else {
212 assert(src->type->is_matrix());
213
214 /* Dereference a row of the matrix, then call this function again to get
215 * a specific element from that row.
216 */
217 const int c = component / src->type->column_type()->vector_elements;
218 const int r = component % src->type->column_type()->vector_elements;
219 ir_constant *const col_index = new ir_constant(c);
220 ir_dereference *const col = new ir_dereference_array(src, col_index);
221
222 col->type = src->type->column_type();
223
224 return dereference_component(col, r);
225 }
226
227 assert(!"Should not get here.");
228 return NULL;
229 }
230
231
232 static ir_rvalue *
233 process_array_constructor(exec_list *instructions,
234 const glsl_type *constructor_type,
235 YYLTYPE *loc, exec_list *parameters,
236 struct _mesa_glsl_parse_state *state)
237 {
238 /* Array constructors come in two forms: sized and unsized. Sized array
239 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
240 * variables. In this case the number of parameters must exactly match the
241 * specified size of the array.
242 *
243 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
244 * are vec4 variables. In this case the size of the array being constructed
245 * is determined by the number of parameters.
246 *
247 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
248 *
249 * "There must be exactly the same number of arguments as the size of
250 * the array being constructed. If no size is present in the
251 * constructor, then the array is explicitly sized to the number of
252 * arguments provided. The arguments are assigned in order, starting at
253 * element 0, to the elements of the constructed array. Each argument
254 * must be the same type as the element type of the array, or be a type
255 * that can be converted to the element type of the array according to
256 * Section 4.1.10 "Implicit Conversions.""
257 */
258 exec_list actual_parameters;
259 const unsigned parameter_count =
260 process_parameters(instructions, &actual_parameters, parameters, state);
261
262 if ((parameter_count == 0)
263 || ((constructor_type->length != 0)
264 && (constructor_type->length != parameter_count))) {
265 const unsigned min_param = (constructor_type->length == 0)
266 ? 1 : constructor_type->length;
267
268 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
269 "parameter%s",
270 (constructor_type->length != 0) ? "at least" : "exactly",
271 min_param, (min_param <= 1) ? "" : "s");
272 return ir_call::get_error_instruction();
273 }
274
275 if (constructor_type->length == 0) {
276 constructor_type =
277 glsl_type::get_array_instance(constructor_type->element_type(),
278 parameter_count);
279 assert(constructor_type != NULL);
280 assert(constructor_type->length == parameter_count);
281 }
282
283 ir_function *f = state->symbols->get_function(constructor_type->name);
284
285 /* If the constructor for this type of array does not exist, generate the
286 * prototype and add it to the symbol table.
287 */
288 if (f == NULL) {
289 f = constructor_type->generate_constructor(state->symbols);
290 }
291
292 ir_rvalue *const r =
293 process_call(instructions, f, loc, &actual_parameters, state);
294
295 assert(r != NULL);
296 assert(r->type->is_error() || (r->type == constructor_type));
297
298 return r;
299 }
300
301
302 /**
303 * Try to convert a record constructor to a constant expression
304 */
305 static ir_constant *
306 constant_record_constructor(const glsl_type *constructor_type,
307 YYLTYPE *loc, exec_list *parameters,
308 struct _mesa_glsl_parse_state *state)
309 {
310 bool all_parameters_are_constant = true;
311
312 exec_node *node = parameters->head;
313 for (unsigned i = 0; i < constructor_type->length; i++) {
314 ir_instruction *ir = (ir_instruction *) node;
315
316 if (node->is_tail_sentinal()) {
317 _mesa_glsl_error(loc, state,
318 "insufficient parameters to constructor for `%s'",
319 constructor_type->name);
320 return NULL;
321 }
322
323 if (ir->type != constructor_type->fields.structure[i].type) {
324 _mesa_glsl_error(loc, state,
325 "parameter type mismatch in constructor for `%s' "
326 " (%s vs %s)",
327 constructor_type->name,
328 ir->type->name,
329 constructor_type->fields.structure[i].type->name);
330 return NULL;
331 }
332
333 if (ir->as_constant() == NULL)
334 all_parameters_are_constant = false;
335
336 node = node->next;
337 }
338
339 if (!all_parameters_are_constant)
340 return NULL;
341
342 return new ir_constant(constructor_type, parameters);
343 }
344
345
346 /**
347 * Generate data for a constant matrix constructor w/a single scalar parameter
348 *
349 * Matrix constructors in GLSL can be passed a single scalar of the
350 * approriate type. In these cases, the resulting matrix is the identity
351 * matrix multipled by the specified scalar. This function generates data for
352 * that matrix.
353 *
354 * \param type Type of the desired matrix.
355 * \param initializer Scalar value used to initialize the matrix diagonal.
356 * \param data Location to store the resulting matrix.
357 */
358 void
359 generate_constructor_matrix(const glsl_type *type, ir_constant *initializer,
360 ir_constant_data *data)
361 {
362 switch (type->base_type) {
363 case GLSL_TYPE_UINT:
364 case GLSL_TYPE_INT:
365 for (unsigned i = 0; i < type->components(); i++)
366 data->u[i] = 0;
367
368 for (unsigned i = 0; i < type->matrix_columns; i++) {
369 /* The array offset of the ith row and column of the matrix.
370 */
371 const unsigned idx = (i * type->vector_elements) + i;
372
373 data->u[idx] = initializer->value.u[0];
374 }
375 break;
376
377 case GLSL_TYPE_FLOAT:
378 for (unsigned i = 0; i < type->components(); i++)
379 data->f[i] = 0;
380
381 for (unsigned i = 0; i < type->matrix_columns; i++) {
382 /* The array offset of the ith row and column of the matrix.
383 */
384 const unsigned idx = (i * type->vector_elements) + i;
385
386 data->f[idx] = initializer->value.f[0];
387 }
388
389 break;
390
391 default:
392 assert(!"Should not get here.");
393 break;
394 }
395 }
396
397
398 /**
399 * Generate data for a constant vector constructor w/a single scalar parameter
400 *
401 * Vector constructors in GLSL can be passed a single scalar of the
402 * approriate type. In these cases, the resulting vector contains the specified
403 * value in all components. This function generates data for that vector.
404 *
405 * \param type Type of the desired vector.
406 * \param initializer Scalar value used to initialize the vector.
407 * \param data Location to store the resulting vector data.
408 */
409 void
410 generate_constructor_vector(const glsl_type *type, ir_constant *initializer,
411 ir_constant_data *data)
412 {
413 switch (type->base_type) {
414 case GLSL_TYPE_UINT:
415 case GLSL_TYPE_INT:
416 for (unsigned i = 0; i < type->components(); i++)
417 data->u[i] = initializer->value.u[0];
418
419 break;
420
421 case GLSL_TYPE_FLOAT:
422 for (unsigned i = 0; i < type->components(); i++)
423 data->f[i] = initializer->value.f[0];
424
425 break;
426
427 case GLSL_TYPE_BOOL:
428 for (unsigned i = 0; i < type->components(); i++)
429 data->b[i] = initializer->value.b[0];
430
431 break;
432
433 default:
434 assert(!"Should not get here.");
435 break;
436 }
437 }
438
439
440 ir_rvalue *
441 ast_function_expression::hir(exec_list *instructions,
442 struct _mesa_glsl_parse_state *state)
443 {
444 /* There are three sorts of function calls.
445 *
446 * 1. contstructors - The first subexpression is an ast_type_specifier.
447 * 2. methods - Only the .length() method of array types.
448 * 3. functions - Calls to regular old functions.
449 *
450 * Method calls are actually detected when the ast_field_selection
451 * expression is handled.
452 */
453 if (is_constructor()) {
454 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
455 YYLTYPE loc = type->get_location();
456 const char *name;
457
458 const glsl_type *const constructor_type = type->glsl_type(& name, state);
459
460
461 /* Constructors for samplers are illegal.
462 */
463 if (constructor_type->is_sampler()) {
464 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
465 constructor_type->name);
466 return ir_call::get_error_instruction();
467 }
468
469 if (constructor_type->is_array()) {
470 if (state->language_version <= 110) {
471 _mesa_glsl_error(& loc, state,
472 "array constructors forbidden in GLSL 1.10");
473 return ir_call::get_error_instruction();
474 }
475
476 return process_array_constructor(instructions, constructor_type,
477 & loc, &this->expressions, state);
478 }
479
480 /* There are two kinds of constructor call. Constructors for built-in
481 * language types, such as mat4 and vec2, are free form. The only
482 * requirement is that the parameters must provide enough values of the
483 * correct scalar type. Constructors for arrays and structures must
484 * have the exact number of parameters with matching types in the
485 * correct order. These constructors follow essentially the same type
486 * matching rules as functions.
487 */
488 if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
489 /* Constructing a numeric type has a couple steps. First all values
490 * passed to the constructor are broken into individual parameters
491 * and type converted to the base type of the thing being constructed.
492 *
493 * At that point we have some number of values that match the base
494 * type of the thing being constructed. Now the constructor can be
495 * treated like a function call. Each numeric type has a small set
496 * of constructor functions. The set of new parameters will either
497 * match one of those functions or the original constructor is
498 * invalid.
499 */
500 const glsl_type *const base_type = constructor_type->get_base_type();
501
502 /* Total number of components of the type being constructed.
503 */
504 const unsigned type_components = constructor_type->components();
505
506 /* Number of components from parameters that have actually been
507 * consumed. This is used to perform several kinds of error checking.
508 */
509 unsigned components_used = 0;
510
511 unsigned matrix_parameters = 0;
512 unsigned nonmatrix_parameters = 0;
513 exec_list actual_parameters;
514
515 bool all_parameters_are_constant = true;
516
517 assert(!this->expressions.is_empty());
518
519 foreach_list (n, &this->expressions) {
520 ast_node *ast = exec_node_data(ast_node, n, link);
521 ir_rvalue *result =
522 ast->hir(instructions, state)->as_rvalue();
523
524 /* Attempt to convert the parameter to a constant valued expression.
525 * After doing so, track whether or not all the parameters to the
526 * constructor are trivially constant valued expressions.
527 */
528 ir_rvalue *const constant =
529 result->constant_expression_value();
530
531 if (constant != NULL)
532 result = constant;
533 else
534 all_parameters_are_constant = false;
535
536 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
537 *
538 * "It is an error to provide extra arguments beyond this
539 * last used argument."
540 */
541 if (components_used >= type_components) {
542 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
543 "constructor",
544 constructor_type->name);
545 return ir_call::get_error_instruction();
546 }
547
548 if (!result->type->is_numeric() && !result->type->is_boolean()) {
549 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
550 "non-numeric data type",
551 constructor_type->name);
552 return ir_call::get_error_instruction();
553 }
554
555 /* Count the number of matrix and nonmatrix parameters. This
556 * is used below to enforce some of the constructor rules.
557 */
558 if (result->type->is_matrix())
559 matrix_parameters++;
560 else
561 nonmatrix_parameters++;
562
563
564 /* Process each of the components of the parameter. Dereference
565 * each component individually, perform any type conversions, and
566 * add it to the parameter list for the constructor.
567 */
568 for (unsigned i = 0; i < result->type->components(); i++) {
569 if (components_used >= type_components)
570 break;
571
572 ir_rvalue *const component =
573 convert_component(dereference_component(result, i),
574 base_type);
575
576 /* All cases that could result in component->type being the
577 * error type should have already been caught above.
578 */
579 assert(component->type == base_type);
580
581 if (component->as_constant() == NULL)
582 all_parameters_are_constant = false;
583
584 /* Don't actually generate constructor calls for scalars.
585 * Instead, do the usual component selection and conversion,
586 * and return the single component.
587 */
588 if (constructor_type->is_scalar())
589 return component;
590
591 actual_parameters.push_tail(component);
592 components_used++;
593 }
594 }
595
596 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
597 *
598 * "It is an error to construct matrices from other matrices. This
599 * is reserved for future use."
600 */
601 if ((state->language_version <= 110) && (matrix_parameters > 0)
602 && constructor_type->is_matrix()) {
603 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
604 "matrix in GLSL 1.10",
605 constructor_type->name);
606 return ir_call::get_error_instruction();
607 }
608
609 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
610 *
611 * "If a matrix argument is given to a matrix constructor, it is
612 * an error to have any other arguments."
613 */
614 if ((matrix_parameters > 0)
615 && ((matrix_parameters + nonmatrix_parameters) > 1)
616 && constructor_type->is_matrix()) {
617 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
618 "matrix must be only parameter",
619 constructor_type->name);
620 return ir_call::get_error_instruction();
621 }
622
623 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
624 *
625 * "In these cases, there must be enough components provided in the
626 * arguments to provide an initializer for every component in the
627 * constructed value."
628 */
629 if ((components_used < type_components) && (components_used != 1)) {
630 _mesa_glsl_error(& loc, state, "too few components to construct "
631 "`%s'",
632 constructor_type->name);
633 return ir_call::get_error_instruction();
634 }
635
636 ir_function *f = state->symbols->get_function(constructor_type->name);
637 if (f == NULL) {
638 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
639 constructor_type->name);
640 return ir_call::get_error_instruction();
641 }
642
643 const ir_function_signature *sig =
644 f->matching_signature(& actual_parameters);
645 if (sig != NULL) {
646 /* If all of the parameters are trivially constant, create a
647 * constant representing the complete collection of parameters.
648 */
649 if (all_parameters_are_constant) {
650 if (components_used >= type_components)
651 return new ir_constant(sig->return_type, & actual_parameters);
652
653 assert(sig->return_type->is_vector()
654 || sig->return_type->is_matrix());
655
656 /* Constructors with exactly one component are special for
657 * vectors and matrices. For vectors it causes all elements of
658 * the vector to be filled with the value. For matrices it
659 * causes the matrix to be filled with 0 and the diagonal to be
660 * filled with the value.
661 */
662 ir_constant_data data;
663 ir_constant *const initializer =
664 (ir_constant *) actual_parameters.head;
665 if (sig->return_type->is_matrix())
666 generate_constructor_matrix(sig->return_type, initializer,
667 &data);
668 else
669 generate_constructor_vector(sig->return_type, initializer,
670 &data);
671
672 return new ir_constant(sig->return_type, &data);
673 } else
674 return new ir_call(sig, & actual_parameters);
675 } else {
676 /* FINISHME: Log a better error message here. G++ will show the
677 * FINSIHME: types of the actual parameters and the set of
678 * FINSIHME: candidate functions. A different error should also be
679 * FINSIHME: logged when multiple functions match.
680 */
681 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
682 constructor_type->name);
683 return ir_call::get_error_instruction();
684 }
685 }
686
687 return ir_call::get_error_instruction();
688 } else {
689 const ast_expression *id = subexpressions[0];
690 YYLTYPE loc = id->get_location();
691 exec_list actual_parameters;
692
693 process_parameters(instructions, &actual_parameters, &this->expressions,
694 state);
695
696 const glsl_type *const type =
697 state->symbols->get_type(id->primary_expression.identifier);
698
699 if ((type != NULL) && type->is_record()) {
700 ir_constant *constant =
701 constant_record_constructor(type, &loc, &actual_parameters, state);
702
703 if (constant != NULL)
704 return constant;
705 }
706
707 return match_function_by_name(instructions,
708 id->primary_expression.identifier, & loc,
709 &actual_parameters, state);
710 }
711
712 return ir_call::get_error_instruction();
713 }