blob: 8cf765f95de528a34a5be6dd377d3c8e22ccbfce [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
28#include "ast.h"
29#include "glsl_parser_extras.h"
Ian Romanickd59673c2010-02-25 17:17:23 -080030#include "glsl_parser.h"
Ian Romanicka87ac252010-02-22 13:19:34 -080031
Ian Romanick5bfe30a2010-04-07 16:44:30 -070032const char *
33_mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
34{
35 switch (target) {
36 case vertex_shader: return "vertex";
37 case fragment_shader: return "fragment";
38 case geometry_shader: return "geometry";
Eric Anholt81f49a72010-04-29 17:57:28 -070039 case ir_shader: break;
Ian Romanick5bfe30a2010-04-07 16:44:30 -070040 }
41
42 assert(!"Should not get here.");
43}
44
45
Ian Romanicka87ac252010-02-22 13:19:34 -080046void
Ian Romanick1f585182010-03-11 14:08:33 -080047_mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
48 const char *fmt, ...)
Ian Romanicka87ac252010-02-22 13:19:34 -080049{
50 char buf[1024];
51 int len;
52 va_list ap;
53
Ian Romanick71d0bbf2010-03-23 13:21:19 -070054 state->error = true;
Ian Romanick1f585182010-03-11 14:08:33 -080055
Ian Romanicka87ac252010-02-22 13:19:34 -080056 len = snprintf(buf, sizeof(buf), "%u:%u(%u): error: ",
57 locp->source, locp->first_line, locp->first_column);
58
59 va_start(ap, fmt);
60 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
61 va_end(ap);
62
63 printf("%s\n", buf);
Eric Anholt3623df62010-04-29 18:00:33 -070064
65 if (state->info_log)
66 free(state->info_log);
67 state->info_log = strdup(buf);
Ian Romanicka87ac252010-02-22 13:19:34 -080068}
69
70
Ian Romanick56b8b212010-04-07 14:47:46 -070071void
Eric Anholt3623df62010-04-29 18:00:33 -070072_mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
Ian Romanick56b8b212010-04-07 14:47:46 -070073 const char *fmt, ...)
74{
75 char buf[1024];
76 int len;
77 va_list ap;
78
79 len = snprintf(buf, sizeof(buf), "%u:%u(%u): warning: ",
80 locp->source, locp->first_line, locp->first_column);
81
82 va_start(ap, fmt);
83 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
84 va_end(ap);
85
86 printf("%s\n", buf);
Eric Anholt3623df62010-04-29 18:00:33 -070087
88 if (!state->info_log) {
89 state->info_log = strdup(buf);
90 }
Ian Romanick56b8b212010-04-07 14:47:46 -070091}
92
93
Ian Romanicke7017612010-04-07 16:46:25 -070094bool
95_mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
96 const char *behavior, YYLTYPE *behavior_locp,
97 _mesa_glsl_parse_state *state)
98{
99 enum {
100 extension_disable,
101 extension_enable,
102 extension_require,
103 extension_warn
104 } ext_mode;
Ian Romanicke7017612010-04-07 16:46:25 -0700105
106 if (strcmp(behavior, "warn") == 0) {
107 ext_mode = extension_warn;
108 } else if (strcmp(behavior, "require") == 0) {
109 ext_mode = extension_require;
110 } else if (strcmp(behavior, "enable") == 0) {
111 ext_mode = extension_enable;
112 } else if (strcmp(behavior, "disable") == 0) {
113 ext_mode = extension_disable;
114 } else {
115 _mesa_glsl_error(behavior_locp, state,
116 "Unknown extension behavior `%s'",
117 behavior);
118 return false;
119 }
120
Ian Romanick887a8b02010-04-07 16:57:56 -0700121 bool unsupported = false;
122
Ian Romanicke7017612010-04-07 16:46:25 -0700123 if (strcmp(name, "all") == 0) {
124 if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
125 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
126 (ext_mode == extension_enable)
127 ? "enable" : "require");
128 return false;
129 }
Ian Romanickeb56cea2010-04-23 13:32:23 -0700130 } else if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
Ian Romanickc77b2572010-04-07 16:59:46 -0700131 /* This extension is only supported in fragment shaders.
132 */
133 if (state->target != fragment_shader) {
134 unsupported = true;
135 } else {
136 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
137 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
138 }
Ian Romanickeb56cea2010-04-23 13:32:23 -0700139 } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
Ian Romanick0c824652010-04-07 17:13:44 -0700140 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
141 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
Ian Romanicke7017612010-04-07 16:46:25 -0700142 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700143 unsupported = true;
144 }
145
146 if (unsupported) {
147 static const char *const fmt = "extension `%s' unsupported in %s shader";
148
Ian Romanicke7017612010-04-07 16:46:25 -0700149 if (ext_mode == extension_require) {
Ian Romanick887a8b02010-04-07 16:57:56 -0700150 _mesa_glsl_error(name_locp, state, fmt,
151 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700152 return false;
Ian Romanick1799a0c2010-04-07 14:50:36 -0700153 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700154 _mesa_glsl_warning(name_locp, state, fmt,
155 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700156 }
157 }
158
159 return true;
160}
161
162
Ian Romanicka87ac252010-02-22 13:19:34 -0800163ast_node::~ast_node()
164{
165 /* empty */
166}
167
168
169void
170_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
171{
172 if (q->constant)
173 printf("const ");
174
175 if (q->invariant)
176 printf("invariant ");
177
178 if (q->attribute)
179 printf("attribute ");
180
181 if (q->varying)
182 printf("varying ");
183
184 if (q->in && q->out)
185 printf("inout ");
186 else {
187 if (q->in)
188 printf("in ");
189
190 if (q->out)
191 printf("out ");
192 }
193
194 if (q->centroid)
195 printf("centroid ");
196 if (q->uniform)
197 printf("uniform ");
198 if (q->smooth)
199 printf("smooth ");
200 if (q->flat)
201 printf("flat ");
202 if (q->noperspective)
203 printf("noperspective ");
204}
205
206
207void
208ast_node::print(void) const
209{
Ian Romanick03d3f3a2010-04-02 11:03:47 -0700210 printf("unhandled node ");
Ian Romanicka87ac252010-02-22 13:19:34 -0800211}
212
213
214ast_node::ast_node(void)
215{
Ian Romanick304ea902010-05-10 11:17:53 -0700216 /* empty */
Ian Romanicka87ac252010-02-22 13:19:34 -0800217}
218
Ian Romanicka87ac252010-02-22 13:19:34 -0800219
220static void
221ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
222{
223 if (is_array) {
224 printf("[ ");
225
226 if (array_size)
227 array_size->print();
228
229 printf("] ");
230 }
231}
232
233
Ian Romanicka87ac252010-02-22 13:19:34 -0800234void
235ast_compound_statement::print(void) const
236{
Ian Romanicka87ac252010-02-22 13:19:34 -0800237 printf("{\n");
238
Ian Romanick304ea902010-05-10 11:17:53 -0700239 foreach_list_const(n, &this->statements) {
240 ast_node *ast = exec_node_data(ast_node, n, link);
241 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800242 }
243
244 printf("}\n");
245}
246
247
248ast_compound_statement::ast_compound_statement(int new_scope,
249 ast_node *statements)
250{
251 this->new_scope = new_scope;
Ian Romanicka87ac252010-02-22 13:19:34 -0800252
253 if (statements != NULL) {
Ian Romanick304ea902010-05-10 11:17:53 -0700254 this->statements.push_degenerate_list_at_head(&statements->link);
Ian Romanicka87ac252010-02-22 13:19:34 -0800255 }
256}
257
258
259void
260ast_expression::print(void) const
261{
Ian Romanicka87ac252010-02-22 13:19:34 -0800262 switch (oper) {
263 case ast_assign:
Ian Romanicka87ac252010-02-22 13:19:34 -0800264 case ast_mul_assign:
265 case ast_div_assign:
266 case ast_mod_assign:
267 case ast_add_assign:
268 case ast_sub_assign:
269 case ast_ls_assign:
270 case ast_rs_assign:
271 case ast_and_assign:
272 case ast_xor_assign:
273 case ast_or_assign:
274 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800275 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800276 subexpressions[1]->print();
277 break;
278
279 case ast_field_selection:
280 subexpressions[0]->print();
281 printf(". %s ", primary_expression.identifier);
282 break;
283
284 case ast_plus:
285 case ast_neg:
286 case ast_bit_not:
287 case ast_logic_not:
288 case ast_pre_inc:
289 case ast_pre_dec:
Ian Romanick88349b22010-02-22 19:10:25 -0800290 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800291 subexpressions[0]->print();
292 break;
293
294 case ast_post_inc:
295 case ast_post_dec:
296 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800297 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800298 break;
299
300 case ast_conditional:
301 subexpressions[0]->print();
302 printf("? ");
303 subexpressions[1]->print();
304 printf(": ");
305 subexpressions[1]->print();
306 break;
307
308 case ast_array_index:
309 subexpressions[0]->print();
310 printf("[ ");
311 subexpressions[1]->print();
312 printf("] ");
313 break;
314
315 case ast_function_call: {
Ian Romanicka87ac252010-02-22 13:19:34 -0800316 subexpressions[0]->print();
317 printf("( ");
318
Ian Romanick304ea902010-05-10 11:17:53 -0700319 foreach_list_const (n, &this->expressions) {
Ian Romanick23849372010-05-14 16:06:41 -0700320 if (n != this->expressions.get_head())
321 printf(", ");
Ian Romanick304ea902010-05-10 11:17:53 -0700322
323 ast_node *ast = exec_node_data(ast_node, n, link);
324 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800325 }
326
327 printf(") ");
328 break;
329 }
330
331 case ast_identifier:
332 printf("%s ", primary_expression.identifier);
333 break;
334
335 case ast_int_constant:
336 printf("%d ", primary_expression.int_constant);
337 break;
338
339 case ast_uint_constant:
340 printf("%u ", primary_expression.uint_constant);
341 break;
342
343 case ast_float_constant:
344 printf("%f ", primary_expression.float_constant);
345 break;
346
347 case ast_bool_constant:
348 printf("%s ",
349 primary_expression.bool_constant
350 ? "true" : "false");
351 break;
352
353 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -0800354 printf("( ");
Ian Romanick304ea902010-05-10 11:17:53 -0700355 foreach_list_const(n, & this->expressions) {
356 if (n != this->expressions.get_head())
Ian Romanicka87ac252010-02-22 13:19:34 -0800357 printf(", ");
358
Ian Romanick304ea902010-05-10 11:17:53 -0700359 ast_node *ast = exec_node_data(ast_node, n, link);
360 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800361 }
362 printf(") ");
363 break;
364 }
Ian Romanick88349b22010-02-22 19:10:25 -0800365
366 default:
367 assert(0);
368 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800369 }
370}
371
372ast_expression::ast_expression(int oper,
373 ast_expression *ex0,
374 ast_expression *ex1,
375 ast_expression *ex2)
376{
377 this->oper = ast_operators(oper);
378 this->subexpressions[0] = ex0;
379 this->subexpressions[1] = ex1;
380 this->subexpressions[2] = ex2;
Ian Romanicka87ac252010-02-22 13:19:34 -0800381}
382
383
384void
385ast_expression_statement::print(void) const
386{
387 if (expression)
388 expression->print();
389
390 printf("; ");
391}
392
393
394ast_expression_statement::ast_expression_statement(ast_expression *ex) :
395 expression(ex)
396{
397 /* empty */
398}
399
400
401void
402ast_function::print(void) const
403{
Ian Romanicka87ac252010-02-22 13:19:34 -0800404 return_type->print();
405 printf(" %s (", identifier);
406
Ian Romanick304ea902010-05-10 11:17:53 -0700407 foreach_list_const(n, & this->parameters) {
408 ast_node *ast = exec_node_data(ast_node, n, link);
409 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800410 }
411
412 printf(")");
413}
414
415
416ast_function::ast_function(void)
Ian Romanick92318a92010-03-31 18:23:21 -0700417 : is_definition(false), signature(NULL)
Ian Romanicka87ac252010-02-22 13:19:34 -0800418{
Ian Romanick304ea902010-05-10 11:17:53 -0700419 /* empty */
Ian Romanicka87ac252010-02-22 13:19:34 -0800420}
421
422
423void
424ast_fully_specified_type::print(void) const
425{
426 _mesa_ast_type_qualifier_print(& qualifier);
427 specifier->print();
428}
429
430
431void
432ast_parameter_declarator::print(void) const
433{
434 type->print();
435 if (identifier)
436 printf("%s ", identifier);
437 ast_opt_array_size_print(is_array, array_size);
438}
439
440
441void
442ast_function_definition::print(void) const
443{
444 prototype->print();
445 body->print();
446}
447
448
449void
450ast_declaration::print(void) const
451{
452 printf("%s ", identifier);
453 ast_opt_array_size_print(is_array, array_size);
454
455 if (initializer) {
456 printf("= ");
457 initializer->print();
458 }
459}
460
461
462ast_declaration::ast_declaration(char *identifier, int is_array,
463 ast_expression *array_size,
464 ast_expression *initializer)
465{
466 this->identifier = identifier;
467 this->is_array = is_array;
468 this->array_size = array_size;
469 this->initializer = initializer;
470}
471
472
473void
474ast_declarator_list::print(void) const
475{
Ian Romanicka87ac252010-02-22 13:19:34 -0800476 assert(type || invariant);
477
478 if (type)
479 type->print();
480 else
481 printf("invariant ");
482
Ian Romanick304ea902010-05-10 11:17:53 -0700483 foreach_list_const (ptr, & this->declarations) {
484 if (ptr != this->declarations.get_head())
Ian Romanicka87ac252010-02-22 13:19:34 -0800485 printf(", ");
486
Ian Romanick304ea902010-05-10 11:17:53 -0700487 ast_node *ast = exec_node_data(ast_node, ptr, link);
488 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800489 }
490
491 printf("; ");
492}
493
494
495ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
496{
497 this->type = type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800498}
499
500void
501ast_jump_statement::print(void) const
502{
503 switch (mode) {
504 case ast_continue:
505 printf("continue; ");
506 break;
507 case ast_break:
508 printf("break; ");
509 break;
510 case ast_return:
511 printf("return ");
512 if (opt_return_value)
513 opt_return_value->print();
514
515 printf("; ");
516 break;
517 case ast_discard:
518 printf("discard; ");
519 break;
520 }
521}
522
523
524ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
525{
526 this->mode = ast_jump_modes(mode);
527
528 if (mode == ast_return)
529 opt_return_value = return_value;
530}
531
532
533void
534ast_selection_statement::print(void) const
535{
536 printf("if ( ");
537 condition->print();
538 printf(") ");
539
540 then_statement->print();
541
542 if (else_statement) {
543 printf("else ");
544 else_statement->print();
545 }
546
547}
548
549
550ast_selection_statement::ast_selection_statement(ast_expression *condition,
551 ast_node *then_statement,
552 ast_node *else_statement)
553{
554 this->condition = condition;
555 this->then_statement = then_statement;
556 this->else_statement = else_statement;
557}
558
559
560void
561ast_iteration_statement::print(void) const
562{
563 switch (mode) {
564 case ast_for:
565 printf("for( ");
566 if (init_statement)
567 init_statement->print();
568 printf("; ");
569
570 if (condition)
571 condition->print();
572 printf("; ");
573
574 if (rest_expression)
575 rest_expression->print();
576 printf(") ");
577
578 body->print();
579 break;
580
581 case ast_while:
582 printf("while ( ");
583 if (condition)
584 condition->print();
585 printf(") ");
586 body->print();
587 break;
588
589 case ast_do_while:
590 printf("do ");
591 body->print();
592 printf("while ( ");
593 if (condition)
594 condition->print();
595 printf("); ");
596 break;
597 }
598}
599
600
601ast_iteration_statement::ast_iteration_statement(int mode,
602 ast_node *init,
603 ast_node *condition,
604 ast_expression *rest_expression,
605 ast_node *body)
606{
607 this->mode = ast_iteration_modes(mode);
608 this->init_statement = init;
609 this->condition = condition;
610 this->rest_expression = rest_expression;
611 this->body = body;
612}
613
614
615void
616ast_struct_specifier::print(void) const
617{
Ian Romanicka87ac252010-02-22 13:19:34 -0800618 printf("struct %s { ", name);
Ian Romanick304ea902010-05-10 11:17:53 -0700619 foreach_list_const(n, &this->declarations) {
620 ast_node *ast = exec_node_data(ast_node, n, link);
621 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800622 }
623 printf("} ");
624}
625
626
627ast_struct_specifier::ast_struct_specifier(char *identifier,
628 ast_node *declarator_list)
629{
630 name = identifier;
Ian Romanick304ea902010-05-10 11:17:53 -0700631 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
Ian Romanicka87ac252010-02-22 13:19:34 -0800632}