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