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.
30 #include "main/mtypes.h"
34 #include "glsl_parser_extras.h"
35 #include "glsl_parser.h"
38 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
41 case vertex_shader
: return "vertex";
42 case fragment_shader
: return "fragment";
43 case geometry_shader
: return "geometry";
44 case ir_shader
: break;
47 assert(!"Should not get here.");
52 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
59 assert(state
->info_log
!= NULL
);
60 state
->info_log
= talloc_asprintf_append(state
->info_log
,
66 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
68 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
73 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
78 assert(state
->info_log
!= NULL
);
79 state
->info_log
= talloc_asprintf_append(state
->info_log
,
80 "%u:%u(%u): warning: ",
85 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
87 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
92 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
93 const char *behavior
, YYLTYPE
*behavior_locp
,
94 _mesa_glsl_parse_state
*state
)
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
;
112 _mesa_glsl_error(behavior_locp
, state
,
113 "Unknown extension behavior `%s'",
118 bool unsupported
= false;
120 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");
127 } else if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
128 /* This extension is only supported in fragment shaders.
130 if (state
->target
!= fragment_shader
) {
133 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
134 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
136 } else if (strcmp(name
, "GL_ARB_texture_rectangle") == 0) {
137 state
->ARB_texture_rectangle_enable
= (ext_mode
!= extension_disable
);
138 state
->ARB_texture_rectangle_warn
= (ext_mode
== extension_warn
);
139 } 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
);
143 unsupported
= !state
->extensions
->EXT_texture_array
;
149 static const char *const fmt
= "extension `%s' unsupported in %s shader";
151 if (ext_mode
== extension_require
) {
152 _mesa_glsl_error(name_locp
, state
, fmt
,
153 name
, _mesa_glsl_shader_target_name(state
->target
));
156 _mesa_glsl_warning(name_locp
, state
, fmt
,
157 name
, _mesa_glsl_shader_target_name(state
->target
));
165 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
171 printf("invariant ");
174 printf("attribute ");
197 if (q
->noperspective
)
198 printf("noperspective ");
203 ast_node::print(void) const
205 printf("unhandled node ");
209 ast_node::ast_node(void)
216 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
230 ast_compound_statement::print(void) const
234 foreach_list_const(n
, &this->statements
) {
235 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
243 ast_compound_statement::ast_compound_statement(int new_scope
,
244 ast_node
*statements
)
246 this->new_scope
= new_scope
;
248 if (statements
!= NULL
) {
249 this->statements
.push_degenerate_list_at_head(&statements
->link
);
255 ast_expression::print(void) const
269 subexpressions
[0]->print();
270 printf("%s ", operator_string(oper
));
271 subexpressions
[1]->print();
274 case ast_field_selection
:
275 subexpressions
[0]->print();
276 printf(". %s ", primary_expression
.identifier
);
285 printf("%s ", operator_string(oper
));
286 subexpressions
[0]->print();
291 subexpressions
[0]->print();
292 printf("%s ", operator_string(oper
));
295 case ast_conditional
:
296 subexpressions
[0]->print();
298 subexpressions
[1]->print();
300 subexpressions
[1]->print();
303 case ast_array_index
:
304 subexpressions
[0]->print();
306 subexpressions
[1]->print();
310 case ast_function_call
: {
311 subexpressions
[0]->print();
314 foreach_list_const (n
, &this->expressions
) {
315 if (n
!= this->expressions
.get_head())
318 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
327 printf("%s ", primary_expression
.identifier
);
330 case ast_int_constant
:
331 printf("%d ", primary_expression
.int_constant
);
334 case ast_uint_constant
:
335 printf("%u ", primary_expression
.uint_constant
);
338 case ast_float_constant
:
339 printf("%f ", primary_expression
.float_constant
);
342 case ast_bool_constant
:
344 primary_expression
.bool_constant
350 foreach_list_const(n
, & this->expressions
) {
351 if (n
!= this->expressions
.get_head())
354 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
367 ast_expression::ast_expression(int oper
,
372 this->oper
= ast_operators(oper
);
373 this->subexpressions
[0] = ex0
;
374 this->subexpressions
[1] = ex1
;
375 this->subexpressions
[2] = ex2
;
380 ast_expression_statement::print(void) const
389 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
397 ast_function::print(void) const
399 return_type
->print();
400 printf(" %s (", identifier
);
402 foreach_list_const(n
, & this->parameters
) {
403 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
411 ast_function::ast_function(void)
412 : is_definition(false), signature(NULL
)
419 ast_fully_specified_type::print(void) const
421 _mesa_ast_type_qualifier_print(& qualifier
);
427 ast_parameter_declarator::print(void) const
431 printf("%s ", identifier
);
432 ast_opt_array_size_print(is_array
, array_size
);
437 ast_function_definition::print(void) const
445 ast_declaration::print(void) const
447 printf("%s ", identifier
);
448 ast_opt_array_size_print(is_array
, array_size
);
452 initializer
->print();
457 ast_declaration::ast_declaration(char *identifier
, int is_array
,
458 ast_expression
*array_size
,
459 ast_expression
*initializer
)
461 this->identifier
= identifier
;
462 this->is_array
= is_array
;
463 this->array_size
= array_size
;
464 this->initializer
= initializer
;
469 ast_declarator_list::print(void) const
471 assert(type
|| invariant
);
476 printf("invariant ");
478 foreach_list_const (ptr
, & this->declarations
) {
479 if (ptr
!= this->declarations
.get_head())
482 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
490 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
493 this->invariant
= false;
497 ast_jump_statement::print(void) const
501 printf("continue; ");
508 if (opt_return_value
)
509 opt_return_value
->print();
520 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
522 this->mode
= ast_jump_modes(mode
);
524 if (mode
== ast_return
)
525 opt_return_value
= return_value
;
530 ast_selection_statement::print(void) const
536 then_statement
->print();
538 if (else_statement
) {
540 else_statement
->print();
546 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
547 ast_node
*then_statement
,
548 ast_node
*else_statement
)
550 this->condition
= condition
;
551 this->then_statement
= then_statement
;
552 this->else_statement
= else_statement
;
557 ast_iteration_statement::print(void) const
563 init_statement
->print();
571 rest_expression
->print();
597 ast_iteration_statement::ast_iteration_statement(int mode
,
600 ast_expression
*rest_expression
,
603 this->mode
= ast_iteration_modes(mode
);
604 this->init_statement
= init
;
605 this->condition
= condition
;
606 this->rest_expression
= rest_expression
;
612 ast_struct_specifier::print(void) const
614 printf("struct %s { ", name
);
615 foreach_list_const(n
, &this->declarations
) {
616 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
623 ast_struct_specifier::ast_struct_specifier(char *identifier
,
624 ast_node
*declarator_list
)
627 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);