Use yy_scan_string and stop caring about shader->SourceLen.
[mesa.git] / 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 }
31
32 #include "ast.h"
33 #include "glsl_parser_extras.h"
34 #include "glsl_parser.h"
35
36 const char *
37 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
38 {
39 switch (target) {
40 case vertex_shader: return "vertex";
41 case fragment_shader: return "fragment";
42 case geometry_shader: return "geometry";
43 case ir_shader: break;
44 }
45
46 assert(!"Should not get here.");
47 }
48
49
50 void
51 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
52 const char *fmt, ...)
53 {
54 va_list ap;
55
56 state->error = true;
57
58 assert(state->info_log != NULL);
59 state->info_log = talloc_asprintf_append(state->info_log,
60 "%u:%u(%u): error: ",
61 locp->source,
62 locp->first_line,
63 locp->first_column);
64 va_start(ap, fmt);
65 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
66 va_end(ap);
67 state->info_log = talloc_strdup_append(state->info_log, "\n");
68 }
69
70
71 void
72 _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
73 const char *fmt, ...)
74 {
75 va_list ap;
76
77 assert(state->info_log != NULL);
78 state->info_log = talloc_asprintf_append(state->info_log,
79 "%u:%u(%u): warning: ",
80 locp->source,
81 locp->first_line,
82 locp->first_column);
83 va_start(ap, fmt);
84 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
85 va_end(ap);
86 state->info_log = talloc_strdup_append(state->info_log, "\n");
87 }
88
89
90 bool
91 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
92 const char *behavior, YYLTYPE *behavior_locp,
93 _mesa_glsl_parse_state *state)
94 {
95 enum {
96 extension_disable,
97 extension_enable,
98 extension_require,
99 extension_warn
100 } ext_mode;
101
102 if (strcmp(behavior, "warn") == 0) {
103 ext_mode = extension_warn;
104 } else if (strcmp(behavior, "require") == 0) {
105 ext_mode = extension_require;
106 } else if (strcmp(behavior, "enable") == 0) {
107 ext_mode = extension_enable;
108 } else if (strcmp(behavior, "disable") == 0) {
109 ext_mode = extension_disable;
110 } else {
111 _mesa_glsl_error(behavior_locp, state,
112 "Unknown extension behavior `%s'",
113 behavior);
114 return false;
115 }
116
117 bool unsupported = false;
118
119 if (strcmp(name, "all") == 0) {
120 if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
121 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
122 (ext_mode == extension_enable)
123 ? "enable" : "require");
124 return false;
125 }
126 } else if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
127 /* This extension is only supported in fragment shaders.
128 */
129 if (state->target != fragment_shader) {
130 unsupported = true;
131 } else {
132 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
133 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
134 }
135 } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
136 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
137 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
138 } else {
139 unsupported = true;
140 }
141
142 if (unsupported) {
143 static const char *const fmt = "extension `%s' unsupported in %s shader";
144
145 if (ext_mode == extension_require) {
146 _mesa_glsl_error(name_locp, state, fmt,
147 name, _mesa_glsl_shader_target_name(state->target));
148 return false;
149 } else {
150 _mesa_glsl_warning(name_locp, state, fmt,
151 name, _mesa_glsl_shader_target_name(state->target));
152 }
153 }
154
155 return true;
156 }
157
158
159 ast_node::~ast_node()
160 {
161 /* empty */
162 }
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 }
495
496 void
497 ast_jump_statement::print(void) const
498 {
499 switch (mode) {
500 case ast_continue:
501 printf("continue; ");
502 break;
503 case ast_break:
504 printf("break; ");
505 break;
506 case ast_return:
507 printf("return ");
508 if (opt_return_value)
509 opt_return_value->print();
510
511 printf("; ");
512 break;
513 case ast_discard:
514 printf("discard; ");
515 break;
516 }
517 }
518
519
520 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
521 {
522 this->mode = ast_jump_modes(mode);
523
524 if (mode == ast_return)
525 opt_return_value = return_value;
526 }
527
528
529 void
530 ast_selection_statement::print(void) const
531 {
532 printf("if ( ");
533 condition->print();
534 printf(") ");
535
536 then_statement->print();
537
538 if (else_statement) {
539 printf("else ");
540 else_statement->print();
541 }
542
543 }
544
545
546 ast_selection_statement::ast_selection_statement(ast_expression *condition,
547 ast_node *then_statement,
548 ast_node *else_statement)
549 {
550 this->condition = condition;
551 this->then_statement = then_statement;
552 this->else_statement = else_statement;
553 }
554
555
556 void
557 ast_iteration_statement::print(void) const
558 {
559 switch (mode) {
560 case ast_for:
561 printf("for( ");
562 if (init_statement)
563 init_statement->print();
564 printf("; ");
565
566 if (condition)
567 condition->print();
568 printf("; ");
569
570 if (rest_expression)
571 rest_expression->print();
572 printf(") ");
573
574 body->print();
575 break;
576
577 case ast_while:
578 printf("while ( ");
579 if (condition)
580 condition->print();
581 printf(") ");
582 body->print();
583 break;
584
585 case ast_do_while:
586 printf("do ");
587 body->print();
588 printf("while ( ");
589 if (condition)
590 condition->print();
591 printf("); ");
592 break;
593 }
594 }
595
596
597 ast_iteration_statement::ast_iteration_statement(int mode,
598 ast_node *init,
599 ast_node *condition,
600 ast_expression *rest_expression,
601 ast_node *body)
602 {
603 this->mode = ast_iteration_modes(mode);
604 this->init_statement = init;
605 this->condition = condition;
606 this->rest_expression = rest_expression;
607 this->body = body;
608 }
609
610
611 void
612 ast_struct_specifier::print(void) const
613 {
614 printf("struct %s { ", name);
615 foreach_list_const(n, &this->declarations) {
616 ast_node *ast = exec_node_data(ast_node, n, link);
617 ast->print();
618 }
619 printf("} ");
620 }
621
622
623 ast_struct_specifier::ast_struct_specifier(char *identifier,
624 ast_node *declarator_list)
625 {
626 name = identifier;
627 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
628 }