Merge remote branch 'origin/master' into glsl2
[mesa.git] / src / glsl / glsl_parser_extras.cpp
1 /*
2 * Copyright © 2008, 2009 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 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <assert.h>
27
28 extern "C" {
29 #include <talloc.h>
30 #include "main/mtypes.h"
31 }
32
33 #include "ast.h"
34 #include "glsl_parser_extras.h"
35 #include "glsl_parser.h"
36
37 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct __GLcontextRec *ctx,
38 GLenum target, void *mem_ctx)
39 {
40 switch (target) {
41 case GL_VERTEX_SHADER: this->target = vertex_shader; break;
42 case GL_FRAGMENT_SHADER: this->target = fragment_shader; break;
43 case GL_GEOMETRY_SHADER: this->target = geometry_shader; break;
44 }
45
46 this->scanner = NULL;
47 this->translation_unit.make_empty();
48 this->symbols = new(mem_ctx) glsl_symbol_table;
49 this->info_log = talloc_strdup(mem_ctx, "");
50 this->error = false;
51 this->loop_or_switch_nesting = NULL;
52 this->ARB_texture_rectangle_enable = true;
53
54 if (ctx != NULL) {
55 this->extensions = &ctx->Extensions;
56
57 this->Const.MaxLights = ctx->Const.MaxLights;
58 this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;
59 this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;
60 this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;
61 this->Const.MaxVertexAttribs = ctx->Const.VertexProgram.MaxAttribs;
62 this->Const.MaxVertexUniformComponents = ctx->Const.VertexProgram.MaxUniformComponents;
63 this->Const.MaxVaryingFloats = ctx->Const.MaxVarying * 4;
64 this->Const.MaxVertexTextureImageUnits = ctx->Const.MaxVertexTextureImageUnits;
65 this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;
66 this->Const.MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
67 this->Const.MaxFragmentUniformComponents = ctx->Const.FragmentProgram.MaxUniformComponents;
68
69 this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
70 } else {
71 /* If there is no GL context (standalone compiler), fill in constants
72 * with the minimum required values.
73 */
74 static struct gl_extensions null_extensions;
75
76 memset(&null_extensions, 0, sizeof(null_extensions));
77 this->extensions = &null_extensions;
78
79 /* 1.10 minimums. */
80 this->Const.MaxLights = 8;
81 this->Const.MaxClipPlanes = 8;
82 this->Const.MaxTextureUnits = 2;
83
84 /* More than the 1.10 minimum to appease parser tests taken from
85 * apps that (hopefully) already checked the number of coords.
86 */
87 this->Const.MaxTextureCoords = 4;
88
89 this->Const.MaxVertexAttribs = 16;
90 this->Const.MaxVertexUniformComponents = 512;
91 this->Const.MaxVaryingFloats = 32;
92 this->Const.MaxVertexTextureImageUnits = 0;
93 this->Const.MaxCombinedTextureImageUnits = 2;
94 this->Const.MaxTextureImageUnits = 2;
95 this->Const.MaxFragmentUniformComponents = 64;
96
97 this->Const.MaxDrawBuffers = 2;
98 }
99 }
100
101 const char *
102 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
103 {
104 switch (target) {
105 case vertex_shader: return "vertex";
106 case fragment_shader: return "fragment";
107 case geometry_shader: return "geometry";
108 case ir_shader: break;
109 }
110
111 assert(!"Should not get here.");
112 return "unknown";
113 }
114
115
116 void
117 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
118 const char *fmt, ...)
119 {
120 va_list ap;
121
122 state->error = true;
123
124 assert(state->info_log != NULL);
125 state->info_log = talloc_asprintf_append(state->info_log,
126 "%u:%u(%u): error: ",
127 locp->source,
128 locp->first_line,
129 locp->first_column);
130 va_start(ap, fmt);
131 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
132 va_end(ap);
133 state->info_log = talloc_strdup_append(state->info_log, "\n");
134 }
135
136
137 void
138 _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
139 const char *fmt, ...)
140 {
141 va_list ap;
142
143 assert(state->info_log != NULL);
144 state->info_log = talloc_asprintf_append(state->info_log,
145 "%u:%u(%u): warning: ",
146 locp->source,
147 locp->first_line,
148 locp->first_column);
149 va_start(ap, fmt);
150 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
151 va_end(ap);
152 state->info_log = talloc_strdup_append(state->info_log, "\n");
153 }
154
155
156 bool
157 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
158 const char *behavior, YYLTYPE *behavior_locp,
159 _mesa_glsl_parse_state *state)
160 {
161 enum {
162 extension_disable,
163 extension_enable,
164 extension_require,
165 extension_warn
166 } ext_mode;
167
168 if (strcmp(behavior, "warn") == 0) {
169 ext_mode = extension_warn;
170 } else if (strcmp(behavior, "require") == 0) {
171 ext_mode = extension_require;
172 } else if (strcmp(behavior, "enable") == 0) {
173 ext_mode = extension_enable;
174 } else if (strcmp(behavior, "disable") == 0) {
175 ext_mode = extension_disable;
176 } else {
177 _mesa_glsl_error(behavior_locp, state,
178 "Unknown extension behavior `%s'",
179 behavior);
180 return false;
181 }
182
183 bool unsupported = false;
184
185 if (strcmp(name, "all") == 0) {
186 if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
187 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
188 (ext_mode == extension_enable)
189 ? "enable" : "require");
190 return false;
191 }
192 } else if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
193 /* This extension is only supported in fragment shaders.
194 */
195 if (state->target != fragment_shader) {
196 unsupported = true;
197 } else {
198 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
199 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
200 }
201 } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
202 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
203 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
204 } else if (strcmp(name, "GL_EXT_texture_array") == 0) {
205 state->EXT_texture_array_enable = (ext_mode != extension_disable);
206 state->EXT_texture_array_warn = (ext_mode == extension_warn);
207
208 unsupported = !state->extensions->EXT_texture_array;
209 } else {
210 unsupported = true;
211 }
212
213 if (unsupported) {
214 static const char *const fmt = "extension `%s' unsupported in %s shader";
215
216 if (ext_mode == extension_require) {
217 _mesa_glsl_error(name_locp, state, fmt,
218 name, _mesa_glsl_shader_target_name(state->target));
219 return false;
220 } else {
221 _mesa_glsl_warning(name_locp, state, fmt,
222 name, _mesa_glsl_shader_target_name(state->target));
223 }
224 }
225
226 return true;
227 }
228
229 void
230 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
231 {
232 if (q->constant)
233 printf("const ");
234
235 if (q->invariant)
236 printf("invariant ");
237
238 if (q->attribute)
239 printf("attribute ");
240
241 if (q->varying)
242 printf("varying ");
243
244 if (q->in && q->out)
245 printf("inout ");
246 else {
247 if (q->in)
248 printf("in ");
249
250 if (q->out)
251 printf("out ");
252 }
253
254 if (q->centroid)
255 printf("centroid ");
256 if (q->uniform)
257 printf("uniform ");
258 if (q->smooth)
259 printf("smooth ");
260 if (q->flat)
261 printf("flat ");
262 if (q->noperspective)
263 printf("noperspective ");
264 }
265
266
267 void
268 ast_node::print(void) const
269 {
270 printf("unhandled node ");
271 }
272
273
274 ast_node::ast_node(void)
275 {
276 /* empty */
277 }
278
279
280 static void
281 ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
282 {
283 if (is_array) {
284 printf("[ ");
285
286 if (array_size)
287 array_size->print();
288
289 printf("] ");
290 }
291 }
292
293
294 void
295 ast_compound_statement::print(void) const
296 {
297 printf("{\n");
298
299 foreach_list_const(n, &this->statements) {
300 ast_node *ast = exec_node_data(ast_node, n, link);
301 ast->print();
302 }
303
304 printf("}\n");
305 }
306
307
308 ast_compound_statement::ast_compound_statement(int new_scope,
309 ast_node *statements)
310 {
311 this->new_scope = new_scope;
312
313 if (statements != NULL) {
314 this->statements.push_degenerate_list_at_head(&statements->link);
315 }
316 }
317
318
319 void
320 ast_expression::print(void) const
321 {
322 switch (oper) {
323 case ast_assign:
324 case ast_mul_assign:
325 case ast_div_assign:
326 case ast_mod_assign:
327 case ast_add_assign:
328 case ast_sub_assign:
329 case ast_ls_assign:
330 case ast_rs_assign:
331 case ast_and_assign:
332 case ast_xor_assign:
333 case ast_or_assign:
334 subexpressions[0]->print();
335 printf("%s ", operator_string(oper));
336 subexpressions[1]->print();
337 break;
338
339 case ast_field_selection:
340 subexpressions[0]->print();
341 printf(". %s ", primary_expression.identifier);
342 break;
343
344 case ast_plus:
345 case ast_neg:
346 case ast_bit_not:
347 case ast_logic_not:
348 case ast_pre_inc:
349 case ast_pre_dec:
350 printf("%s ", operator_string(oper));
351 subexpressions[0]->print();
352 break;
353
354 case ast_post_inc:
355 case ast_post_dec:
356 subexpressions[0]->print();
357 printf("%s ", operator_string(oper));
358 break;
359
360 case ast_conditional:
361 subexpressions[0]->print();
362 printf("? ");
363 subexpressions[1]->print();
364 printf(": ");
365 subexpressions[1]->print();
366 break;
367
368 case ast_array_index:
369 subexpressions[0]->print();
370 printf("[ ");
371 subexpressions[1]->print();
372 printf("] ");
373 break;
374
375 case ast_function_call: {
376 subexpressions[0]->print();
377 printf("( ");
378
379 foreach_list_const (n, &this->expressions) {
380 if (n != this->expressions.get_head())
381 printf(", ");
382
383 ast_node *ast = exec_node_data(ast_node, n, link);
384 ast->print();
385 }
386
387 printf(") ");
388 break;
389 }
390
391 case ast_identifier:
392 printf("%s ", primary_expression.identifier);
393 break;
394
395 case ast_int_constant:
396 printf("%d ", primary_expression.int_constant);
397 break;
398
399 case ast_uint_constant:
400 printf("%u ", primary_expression.uint_constant);
401 break;
402
403 case ast_float_constant:
404 printf("%f ", primary_expression.float_constant);
405 break;
406
407 case ast_bool_constant:
408 printf("%s ",
409 primary_expression.bool_constant
410 ? "true" : "false");
411 break;
412
413 case ast_sequence: {
414 printf("( ");
415 foreach_list_const(n, & this->expressions) {
416 if (n != this->expressions.get_head())
417 printf(", ");
418
419 ast_node *ast = exec_node_data(ast_node, n, link);
420 ast->print();
421 }
422 printf(") ");
423 break;
424 }
425
426 default:
427 assert(0);
428 break;
429 }
430 }
431
432 ast_expression::ast_expression(int oper,
433 ast_expression *ex0,
434 ast_expression *ex1,
435 ast_expression *ex2)
436 {
437 this->oper = ast_operators(oper);
438 this->subexpressions[0] = ex0;
439 this->subexpressions[1] = ex1;
440 this->subexpressions[2] = ex2;
441 }
442
443
444 void
445 ast_expression_statement::print(void) const
446 {
447 if (expression)
448 expression->print();
449
450 printf("; ");
451 }
452
453
454 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
455 expression(ex)
456 {
457 /* empty */
458 }
459
460
461 void
462 ast_function::print(void) const
463 {
464 return_type->print();
465 printf(" %s (", identifier);
466
467 foreach_list_const(n, & this->parameters) {
468 ast_node *ast = exec_node_data(ast_node, n, link);
469 ast->print();
470 }
471
472 printf(")");
473 }
474
475
476 ast_function::ast_function(void)
477 : is_definition(false), signature(NULL)
478 {
479 /* empty */
480 }
481
482
483 void
484 ast_fully_specified_type::print(void) const
485 {
486 _mesa_ast_type_qualifier_print(& qualifier);
487 specifier->print();
488 }
489
490
491 void
492 ast_parameter_declarator::print(void) const
493 {
494 type->print();
495 if (identifier)
496 printf("%s ", identifier);
497 ast_opt_array_size_print(is_array, array_size);
498 }
499
500
501 void
502 ast_function_definition::print(void) const
503 {
504 prototype->print();
505 body->print();
506 }
507
508
509 void
510 ast_declaration::print(void) const
511 {
512 printf("%s ", identifier);
513 ast_opt_array_size_print(is_array, array_size);
514
515 if (initializer) {
516 printf("= ");
517 initializer->print();
518 }
519 }
520
521
522 ast_declaration::ast_declaration(char *identifier, int is_array,
523 ast_expression *array_size,
524 ast_expression *initializer)
525 {
526 this->identifier = identifier;
527 this->is_array = is_array;
528 this->array_size = array_size;
529 this->initializer = initializer;
530 }
531
532
533 void
534 ast_declarator_list::print(void) const
535 {
536 assert(type || invariant);
537
538 if (type)
539 type->print();
540 else
541 printf("invariant ");
542
543 foreach_list_const (ptr, & this->declarations) {
544 if (ptr != this->declarations.get_head())
545 printf(", ");
546
547 ast_node *ast = exec_node_data(ast_node, ptr, link);
548 ast->print();
549 }
550
551 printf("; ");
552 }
553
554
555 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
556 {
557 this->type = type;
558 this->invariant = false;
559 }
560
561 void
562 ast_jump_statement::print(void) const
563 {
564 switch (mode) {
565 case ast_continue:
566 printf("continue; ");
567 break;
568 case ast_break:
569 printf("break; ");
570 break;
571 case ast_return:
572 printf("return ");
573 if (opt_return_value)
574 opt_return_value->print();
575
576 printf("; ");
577 break;
578 case ast_discard:
579 printf("discard; ");
580 break;
581 }
582 }
583
584
585 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
586 {
587 this->mode = ast_jump_modes(mode);
588
589 if (mode == ast_return)
590 opt_return_value = return_value;
591 }
592
593
594 void
595 ast_selection_statement::print(void) const
596 {
597 printf("if ( ");
598 condition->print();
599 printf(") ");
600
601 then_statement->print();
602
603 if (else_statement) {
604 printf("else ");
605 else_statement->print();
606 }
607
608 }
609
610
611 ast_selection_statement::ast_selection_statement(ast_expression *condition,
612 ast_node *then_statement,
613 ast_node *else_statement)
614 {
615 this->condition = condition;
616 this->then_statement = then_statement;
617 this->else_statement = else_statement;
618 }
619
620
621 void
622 ast_iteration_statement::print(void) const
623 {
624 switch (mode) {
625 case ast_for:
626 printf("for( ");
627 if (init_statement)
628 init_statement->print();
629 printf("; ");
630
631 if (condition)
632 condition->print();
633 printf("; ");
634
635 if (rest_expression)
636 rest_expression->print();
637 printf(") ");
638
639 body->print();
640 break;
641
642 case ast_while:
643 printf("while ( ");
644 if (condition)
645 condition->print();
646 printf(") ");
647 body->print();
648 break;
649
650 case ast_do_while:
651 printf("do ");
652 body->print();
653 printf("while ( ");
654 if (condition)
655 condition->print();
656 printf("); ");
657 break;
658 }
659 }
660
661
662 ast_iteration_statement::ast_iteration_statement(int mode,
663 ast_node *init,
664 ast_node *condition,
665 ast_expression *rest_expression,
666 ast_node *body)
667 {
668 this->mode = ast_iteration_modes(mode);
669 this->init_statement = init;
670 this->condition = condition;
671 this->rest_expression = rest_expression;
672 this->body = body;
673 }
674
675
676 void
677 ast_struct_specifier::print(void) const
678 {
679 printf("struct %s { ", name);
680 foreach_list_const(n, &this->declarations) {
681 ast_node *ast = exec_node_data(ast_node, n, link);
682 ast->print();
683 }
684 printf("} ");
685 }
686
687
688 ast_struct_specifier::ast_struct_specifier(char *identifier,
689 ast_node *declarator_list)
690 {
691 name = identifier;
692 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
693 }