2 * Copyright © 2008, 2009 Intel Corporation
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:
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
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.
33 #include "glsl_parser_extras.h"
34 #include "glsl_parser.h"
37 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
40 case vertex_shader
: return "vertex";
41 case fragment_shader
: return "fragment";
42 case geometry_shader
: return "geometry";
43 case ir_shader
: break;
46 assert(!"Should not get here.");
51 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
58 assert(state
->info_log
!= NULL
);
59 state
->info_log
= talloc_asprintf_append(state
->info_log
,
65 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
67 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
72 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
77 assert(state
->info_log
!= NULL
);
78 state
->info_log
= talloc_asprintf_append(state
->info_log
,
79 "%u:%u(%u): warning: ",
84 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
86 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
91 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
92 const char *behavior
, YYLTYPE
*behavior_locp
,
93 _mesa_glsl_parse_state
*state
)
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
;
111 _mesa_glsl_error(behavior_locp
, state
,
112 "Unknown extension behavior `%s'",
117 bool unsupported
= false;
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");
126 } else if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
127 /* This extension is only supported in fragment shaders.
129 if (state
->target
!= fragment_shader
) {
132 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
133 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
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
);
143 static const char *const fmt
= "extension `%s' unsupported in %s shader";
145 if (ext_mode
== extension_require
) {
146 _mesa_glsl_error(name_locp
, state
, fmt
,
147 name
, _mesa_glsl_shader_target_name(state
->target
));
150 _mesa_glsl_warning(name_locp
, state
, fmt
,
151 name
, _mesa_glsl_shader_target_name(state
->target
));
159 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
165 printf("invariant ");
168 printf("attribute ");
191 if (q
->noperspective
)
192 printf("noperspective ");
197 ast_node::print(void) const
199 printf("unhandled node ");
203 ast_node::ast_node(void)
210 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
224 ast_compound_statement::print(void) const
228 foreach_list_const(n
, &this->statements
) {
229 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
237 ast_compound_statement::ast_compound_statement(int new_scope
,
238 ast_node
*statements
)
240 this->new_scope
= new_scope
;
242 if (statements
!= NULL
) {
243 this->statements
.push_degenerate_list_at_head(&statements
->link
);
249 ast_expression::print(void) const
263 subexpressions
[0]->print();
264 printf("%s ", operator_string(oper
));
265 subexpressions
[1]->print();
268 case ast_field_selection
:
269 subexpressions
[0]->print();
270 printf(". %s ", primary_expression
.identifier
);
279 printf("%s ", operator_string(oper
));
280 subexpressions
[0]->print();
285 subexpressions
[0]->print();
286 printf("%s ", operator_string(oper
));
289 case ast_conditional
:
290 subexpressions
[0]->print();
292 subexpressions
[1]->print();
294 subexpressions
[1]->print();
297 case ast_array_index
:
298 subexpressions
[0]->print();
300 subexpressions
[1]->print();
304 case ast_function_call
: {
305 subexpressions
[0]->print();
308 foreach_list_const (n
, &this->expressions
) {
309 if (n
!= this->expressions
.get_head())
312 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
321 printf("%s ", primary_expression
.identifier
);
324 case ast_int_constant
:
325 printf("%d ", primary_expression
.int_constant
);
328 case ast_uint_constant
:
329 printf("%u ", primary_expression
.uint_constant
);
332 case ast_float_constant
:
333 printf("%f ", primary_expression
.float_constant
);
336 case ast_bool_constant
:
338 primary_expression
.bool_constant
344 foreach_list_const(n
, & this->expressions
) {
345 if (n
!= this->expressions
.get_head())
348 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
361 ast_expression::ast_expression(int oper
,
366 this->oper
= ast_operators(oper
);
367 this->subexpressions
[0] = ex0
;
368 this->subexpressions
[1] = ex1
;
369 this->subexpressions
[2] = ex2
;
374 ast_expression_statement::print(void) const
383 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
391 ast_function::print(void) const
393 return_type
->print();
394 printf(" %s (", identifier
);
396 foreach_list_const(n
, & this->parameters
) {
397 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
405 ast_function::ast_function(void)
406 : is_definition(false), signature(NULL
)
413 ast_fully_specified_type::print(void) const
415 _mesa_ast_type_qualifier_print(& qualifier
);
421 ast_parameter_declarator::print(void) const
425 printf("%s ", identifier
);
426 ast_opt_array_size_print(is_array
, array_size
);
431 ast_function_definition::print(void) const
439 ast_declaration::print(void) const
441 printf("%s ", identifier
);
442 ast_opt_array_size_print(is_array
, array_size
);
446 initializer
->print();
451 ast_declaration::ast_declaration(char *identifier
, int is_array
,
452 ast_expression
*array_size
,
453 ast_expression
*initializer
)
455 this->identifier
= identifier
;
456 this->is_array
= is_array
;
457 this->array_size
= array_size
;
458 this->initializer
= initializer
;
463 ast_declarator_list::print(void) const
465 assert(type
|| invariant
);
470 printf("invariant ");
472 foreach_list_const (ptr
, & this->declarations
) {
473 if (ptr
!= this->declarations
.get_head())
476 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
484 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
490 ast_jump_statement::print(void) const
494 printf("continue; ");
501 if (opt_return_value
)
502 opt_return_value
->print();
513 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
515 this->mode
= ast_jump_modes(mode
);
517 if (mode
== ast_return
)
518 opt_return_value
= return_value
;
523 ast_selection_statement::print(void) const
529 then_statement
->print();
531 if (else_statement
) {
533 else_statement
->print();
539 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
540 ast_node
*then_statement
,
541 ast_node
*else_statement
)
543 this->condition
= condition
;
544 this->then_statement
= then_statement
;
545 this->else_statement
= else_statement
;
550 ast_iteration_statement::print(void) const
556 init_statement
->print();
564 rest_expression
->print();
590 ast_iteration_statement::ast_iteration_statement(int mode
,
593 ast_expression
*rest_expression
,
596 this->mode
= ast_iteration_modes(mode
);
597 this->init_statement
= init
;
598 this->condition
= condition
;
599 this->rest_expression
= rest_expression
;
605 ast_struct_specifier::print(void) const
607 printf("struct %s { ", name
);
608 foreach_list_const(n
, &this->declarations
) {
609 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
616 ast_struct_specifier::ast_struct_specifier(char *identifier
,
617 ast_node
*declarator_list
)
620 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);