blob: fc3f9e90b6be810c1b55406ebf37bcac3fad57e3 [file] [log] [blame]
Ian Romanicka87ac252010-02-22 13:19:34 -08001/*
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>
Ian Romanicka87ac252010-02-22 13:19:34 -080025#include <string.h>
26#include <assert.h>
Ian Romanicka87ac252010-02-22 13:19:34 -080027
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070028extern "C" {
29#include <talloc.h>
Ian Romanick667f4e12010-06-30 16:42:07 -070030#include "main/mtypes.h"
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070031}
32
Ian Romanicka87ac252010-02-22 13:19:34 -080033#include "ast.h"
34#include "glsl_parser_extras.h"
Ian Romanickd59673c2010-02-25 17:17:23 -080035#include "glsl_parser.h"
Ian Romanicka87ac252010-02-22 13:19:34 -080036
Ian Romanick5bfe30a2010-04-07 16:44:30 -070037const 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";
Eric Anholt81f49a72010-04-29 17:57:28 -070044 case ir_shader: break;
Ian Romanick5bfe30a2010-04-07 16:44:30 -070045 }
46
47 assert(!"Should not get here.");
48}
49
50
Ian Romanicka87ac252010-02-22 13:19:34 -080051void
Ian Romanick1f585182010-03-11 14:08:33 -080052_mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
53 const char *fmt, ...)
Ian Romanicka87ac252010-02-22 13:19:34 -080054{
Ian Romanicka87ac252010-02-22 13:19:34 -080055 va_list ap;
56
Ian Romanick71d0bbf2010-03-23 13:21:19 -070057 state->error = true;
Ian Romanick1f585182010-03-11 14:08:33 -080058
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070059 assert(state->info_log != NULL);
60 state->info_log = talloc_asprintf_append(state->info_log,
61 "%u:%u(%u): error: ",
62 locp->source,
63 locp->first_line,
64 locp->first_column);
Ian Romanicka87ac252010-02-22 13:19:34 -080065 va_start(ap, fmt);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070066 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
Ian Romanicka87ac252010-02-22 13:19:34 -080067 va_end(ap);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070068 state->info_log = talloc_strdup_append(state->info_log, "\n");
Ian Romanicka87ac252010-02-22 13:19:34 -080069}
70
71
Ian Romanick56b8b212010-04-07 14:47:46 -070072void
Eric Anholt3623df62010-04-29 18:00:33 -070073_mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
Ian Romanick56b8b212010-04-07 14:47:46 -070074 const char *fmt, ...)
75{
Ian Romanick56b8b212010-04-07 14:47:46 -070076 va_list ap;
77
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070078 assert(state->info_log != NULL);
79 state->info_log = talloc_asprintf_append(state->info_log,
80 "%u:%u(%u): warning: ",
81 locp->source,
82 locp->first_line,
83 locp->first_column);
Ian Romanick56b8b212010-04-07 14:47:46 -070084 va_start(ap, fmt);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070085 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
Ian Romanick56b8b212010-04-07 14:47:46 -070086 va_end(ap);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070087 state->info_log = talloc_strdup_append(state->info_log, "\n");
Ian Romanick56b8b212010-04-07 14:47:46 -070088}
89
90
Ian Romanicke7017612010-04-07 16:46:25 -070091bool
92_mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
93 const char *behavior, YYLTYPE *behavior_locp,
94 _mesa_glsl_parse_state *state)
95{
96 enum {
97 extension_disable,
98 extension_enable,
99 extension_require,
100 extension_warn
101 } ext_mode;
Ian Romanicke7017612010-04-07 16:46:25 -0700102
103 if (strcmp(behavior, "warn") == 0) {
104 ext_mode = extension_warn;
105 } else if (strcmp(behavior, "require") == 0) {
106 ext_mode = extension_require;
107 } else if (strcmp(behavior, "enable") == 0) {
108 ext_mode = extension_enable;
109 } else if (strcmp(behavior, "disable") == 0) {
110 ext_mode = extension_disable;
111 } else {
112 _mesa_glsl_error(behavior_locp, state,
113 "Unknown extension behavior `%s'",
114 behavior);
115 return false;
116 }
117
Ian Romanick887a8b02010-04-07 16:57:56 -0700118 bool unsupported = false;
119
Ian Romanicke7017612010-04-07 16:46:25 -0700120 if (strcmp(name, "all") == 0) {
121 if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
122 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
123 (ext_mode == extension_enable)
124 ? "enable" : "require");
125 return false;
126 }
Ian Romanickeb56cea2010-04-23 13:32:23 -0700127 } else if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
Ian Romanickc77b2572010-04-07 16:59:46 -0700128 /* This extension is only supported in fragment shaders.
129 */
130 if (state->target != fragment_shader) {
131 unsupported = true;
132 } else {
133 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
134 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
135 }
Ian Romanickeb56cea2010-04-23 13:32:23 -0700136 } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
Ian Romanick0c824652010-04-07 17:13:44 -0700137 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
138 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
Ian Romanick667f4e12010-06-30 16:42:07 -0700139 } else if (strcmp(name, "GL_EXT_texture_array") == 0) {
140 state->EXT_texture_array_enable = (ext_mode != extension_disable);
141 state->EXT_texture_array_warn = (ext_mode == extension_warn);
142
143 unsupported = !state->extensions->EXT_texture_array;
Ian Romanicke7017612010-04-07 16:46:25 -0700144 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700145 unsupported = true;
146 }
147
148 if (unsupported) {
149 static const char *const fmt = "extension `%s' unsupported in %s shader";
150
Ian Romanicke7017612010-04-07 16:46:25 -0700151 if (ext_mode == extension_require) {
Ian Romanick887a8b02010-04-07 16:57:56 -0700152 _mesa_glsl_error(name_locp, state, fmt,
153 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700154 return false;
Ian Romanick1799a0c2010-04-07 14:50:36 -0700155 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700156 _mesa_glsl_warning(name_locp, state, fmt,
157 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700158 }
159 }
160
161 return true;
162}
163
Ian Romanicka87ac252010-02-22 13:19:34 -0800164void
165_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
166{
167 if (q->constant)
168 printf("const ");
169
170 if (q->invariant)
171 printf("invariant ");
172
173 if (q->attribute)
174 printf("attribute ");
175
176 if (q->varying)
177 printf("varying ");
178
179 if (q->in && q->out)
180 printf("inout ");
181 else {
182 if (q->in)
183 printf("in ");
184
185 if (q->out)
186 printf("out ");
187 }
188
189 if (q->centroid)
190 printf("centroid ");
191 if (q->uniform)
192 printf("uniform ");
193 if (q->smooth)
194 printf("smooth ");
195 if (q->flat)
196 printf("flat ");
197 if (q->noperspective)
198 printf("noperspective ");
199}
200
201
202void
203ast_node::print(void) const
204{
Ian Romanick03d3f3a2010-04-02 11:03:47 -0700205 printf("unhandled node ");
Ian Romanicka87ac252010-02-22 13:19:34 -0800206}
207
208
209ast_node::ast_node(void)
210{
Ian Romanick304ea902010-05-10 11:17:53 -0700211 /* empty */
Ian Romanicka87ac252010-02-22 13:19:34 -0800212}
213
Ian Romanicka87ac252010-02-22 13:19:34 -0800214
215static void
216ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
217{
218 if (is_array) {
219 printf("[ ");
220
221 if (array_size)
222 array_size->print();
223
224 printf("] ");
225 }
226}
227
228
Ian Romanicka87ac252010-02-22 13:19:34 -0800229void
230ast_compound_statement::print(void) const
231{
Ian Romanicka87ac252010-02-22 13:19:34 -0800232 printf("{\n");
233
Ian Romanick304ea902010-05-10 11:17:53 -0700234 foreach_list_const(n, &this->statements) {
235 ast_node *ast = exec_node_data(ast_node, n, link);
236 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800237 }
238
239 printf("}\n");
240}
241
242
243ast_compound_statement::ast_compound_statement(int new_scope,
244 ast_node *statements)
245{
246 this->new_scope = new_scope;
Ian Romanicka87ac252010-02-22 13:19:34 -0800247
248 if (statements != NULL) {
Ian Romanick304ea902010-05-10 11:17:53 -0700249 this->statements.push_degenerate_list_at_head(&statements->link);
Ian Romanicka87ac252010-02-22 13:19:34 -0800250 }
251}
252
253
254void
255ast_expression::print(void) const
256{
Ian Romanicka87ac252010-02-22 13:19:34 -0800257 switch (oper) {
258 case ast_assign:
Ian Romanicka87ac252010-02-22 13:19:34 -0800259 case ast_mul_assign:
260 case ast_div_assign:
261 case ast_mod_assign:
262 case ast_add_assign:
263 case ast_sub_assign:
264 case ast_ls_assign:
265 case ast_rs_assign:
266 case ast_and_assign:
267 case ast_xor_assign:
268 case ast_or_assign:
269 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800270 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800271 subexpressions[1]->print();
272 break;
273
274 case ast_field_selection:
275 subexpressions[0]->print();
276 printf(". %s ", primary_expression.identifier);
277 break;
278
279 case ast_plus:
280 case ast_neg:
281 case ast_bit_not:
282 case ast_logic_not:
283 case ast_pre_inc:
284 case ast_pre_dec:
Ian Romanick88349b22010-02-22 19:10:25 -0800285 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800286 subexpressions[0]->print();
287 break;
288
289 case ast_post_inc:
290 case ast_post_dec:
291 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800292 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800293 break;
294
295 case ast_conditional:
296 subexpressions[0]->print();
297 printf("? ");
298 subexpressions[1]->print();
299 printf(": ");
300 subexpressions[1]->print();
301 break;
302
303 case ast_array_index:
304 subexpressions[0]->print();
305 printf("[ ");
306 subexpressions[1]->print();
307 printf("] ");
308 break;
309
310 case ast_function_call: {
Ian Romanicka87ac252010-02-22 13:19:34 -0800311 subexpressions[0]->print();
312 printf("( ");
313
Ian Romanick304ea902010-05-10 11:17:53 -0700314 foreach_list_const (n, &this->expressions) {
Ian Romanick23849372010-05-14 16:06:41 -0700315 if (n != this->expressions.get_head())
316 printf(", ");
Ian Romanick304ea902010-05-10 11:17:53 -0700317
318 ast_node *ast = exec_node_data(ast_node, n, link);
319 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800320 }
321
322 printf(") ");
323 break;
324 }
325
326 case ast_identifier:
327 printf("%s ", primary_expression.identifier);
328 break;
329
330 case ast_int_constant:
331 printf("%d ", primary_expression.int_constant);
332 break;
333
334 case ast_uint_constant:
335 printf("%u ", primary_expression.uint_constant);
336 break;
337
338 case ast_float_constant:
339 printf("%f ", primary_expression.float_constant);
340 break;
341
342 case ast_bool_constant:
343 printf("%s ",
344 primary_expression.bool_constant
345 ? "true" : "false");
346 break;
347
348 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -0800349 printf("( ");
Ian Romanick304ea902010-05-10 11:17:53 -0700350 foreach_list_const(n, & this->expressions) {
351 if (n != this->expressions.get_head())
Ian Romanicka87ac252010-02-22 13:19:34 -0800352 printf(", ");
353
Ian Romanick304ea902010-05-10 11:17:53 -0700354 ast_node *ast = exec_node_data(ast_node, n, link);
355 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800356 }
357 printf(") ");
358 break;
359 }
Ian Romanick88349b22010-02-22 19:10:25 -0800360
361 default:
362 assert(0);
363 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800364 }
365}
366
367ast_expression::ast_expression(int oper,
368 ast_expression *ex0,
369 ast_expression *ex1,
370 ast_expression *ex2)
371{
372 this->oper = ast_operators(oper);
373 this->subexpressions[0] = ex0;
374 this->subexpressions[1] = ex1;
375 this->subexpressions[2] = ex2;
Ian Romanicka87ac252010-02-22 13:19:34 -0800376}
377
378
379void
380ast_expression_statement::print(void) const
381{
382 if (expression)
383 expression->print();
384
385 printf("; ");
386}
387
388
389ast_expression_statement::ast_expression_statement(ast_expression *ex) :
390 expression(ex)
391{
392 /* empty */
393}
394
395
396void
397ast_function::print(void) const
398{
Ian Romanicka87ac252010-02-22 13:19:34 -0800399 return_type->print();
400 printf(" %s (", identifier);
401
Ian Romanick304ea902010-05-10 11:17:53 -0700402 foreach_list_const(n, & this->parameters) {
403 ast_node *ast = exec_node_data(ast_node, n, link);
404 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800405 }
406
407 printf(")");
408}
409
410
411ast_function::ast_function(void)
Ian Romanick92318a92010-03-31 18:23:21 -0700412 : is_definition(false), signature(NULL)
Ian Romanicka87ac252010-02-22 13:19:34 -0800413{
Ian Romanick304ea902010-05-10 11:17:53 -0700414 /* empty */
Ian Romanicka87ac252010-02-22 13:19:34 -0800415}
416
417
418void
419ast_fully_specified_type::print(void) const
420{
421 _mesa_ast_type_qualifier_print(& qualifier);
422 specifier->print();
423}
424
425
426void
427ast_parameter_declarator::print(void) const
428{
429 type->print();
430 if (identifier)
431 printf("%s ", identifier);
432 ast_opt_array_size_print(is_array, array_size);
433}
434
435
436void
437ast_function_definition::print(void) const
438{
439 prototype->print();
440 body->print();
441}
442
443
444void
445ast_declaration::print(void) const
446{
447 printf("%s ", identifier);
448 ast_opt_array_size_print(is_array, array_size);
449
450 if (initializer) {
451 printf("= ");
452 initializer->print();
453 }
454}
455
456
457ast_declaration::ast_declaration(char *identifier, int is_array,
458 ast_expression *array_size,
459 ast_expression *initializer)
460{
461 this->identifier = identifier;
462 this->is_array = is_array;
463 this->array_size = array_size;
464 this->initializer = initializer;
465}
466
467
468void
469ast_declarator_list::print(void) const
470{
Ian Romanicka87ac252010-02-22 13:19:34 -0800471 assert(type || invariant);
472
473 if (type)
474 type->print();
475 else
476 printf("invariant ");
477
Ian Romanick304ea902010-05-10 11:17:53 -0700478 foreach_list_const (ptr, & this->declarations) {
479 if (ptr != this->declarations.get_head())
Ian Romanicka87ac252010-02-22 13:19:34 -0800480 printf(", ");
481
Ian Romanick304ea902010-05-10 11:17:53 -0700482 ast_node *ast = exec_node_data(ast_node, ptr, link);
483 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800484 }
485
486 printf("; ");
487}
488
489
490ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
491{
492 this->type = type;
Ian Romanick38327062010-07-01 17:10:11 -0700493 this->invariant = false;
Ian Romanicka87ac252010-02-22 13:19:34 -0800494}
495
496void
497ast_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
520ast_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
529void
530ast_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
546ast_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
556void
557ast_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
597ast_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
611void
612ast_struct_specifier::print(void) const
613{
Ian Romanicka87ac252010-02-22 13:19:34 -0800614 printf("struct %s { ", name);
Ian Romanick304ea902010-05-10 11:17:53 -0700615 foreach_list_const(n, &this->declarations) {
616 ast_node *ast = exec_node_data(ast_node, n, link);
617 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800618 }
619 printf("} ");
620}
621
622
623ast_struct_specifier::ast_struct_specifier(char *identifier,
624 ast_node *declarator_list)
625{
626 name = identifier;
Ian Romanick304ea902010-05-10 11:17:53 -0700627 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
Ian Romanicka87ac252010-02-22 13:19:34 -0800628}