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.");
53 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
60 assert(state
->info_log
!= NULL
);
61 state
->info_log
= talloc_asprintf_append(state
->info_log
,
67 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
69 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
74 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
79 assert(state
->info_log
!= NULL
);
80 state
->info_log
= talloc_asprintf_append(state
->info_log
,
81 "%u:%u(%u): warning: ",
86 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
88 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
93 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
94 const char *behavior
, YYLTYPE
*behavior_locp
,
95 _mesa_glsl_parse_state
*state
)
104 if (strcmp(behavior
, "warn") == 0) {
105 ext_mode
= extension_warn
;
106 } else if (strcmp(behavior
, "require") == 0) {
107 ext_mode
= extension_require
;
108 } else if (strcmp(behavior
, "enable") == 0) {
109 ext_mode
= extension_enable
;
110 } else if (strcmp(behavior
, "disable") == 0) {
111 ext_mode
= extension_disable
;
113 _mesa_glsl_error(behavior_locp
, state
,
114 "Unknown extension behavior `%s'",
119 bool unsupported
= false;
121 if (strcmp(name
, "all") == 0) {
122 if ((ext_mode
== extension_enable
) || (ext_mode
== extension_require
)) {
123 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
124 (ext_mode
== extension_enable
)
125 ? "enable" : "require");
128 } else if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
129 /* This extension is only supported in fragment shaders.
131 if (state
->target
!= fragment_shader
) {
134 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
135 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
137 } else if (strcmp(name
, "GL_ARB_texture_rectangle") == 0) {
138 state
->ARB_texture_rectangle_enable
= (ext_mode
!= extension_disable
);
139 state
->ARB_texture_rectangle_warn
= (ext_mode
== extension_warn
);
140 } else if (strcmp(name
, "GL_EXT_texture_array") == 0) {
141 state
->EXT_texture_array_enable
= (ext_mode
!= extension_disable
);
142 state
->EXT_texture_array_warn
= (ext_mode
== extension_warn
);
144 unsupported
= !state
->extensions
->EXT_texture_array
;
150 static const char *const fmt
= "extension `%s' unsupported in %s shader";
152 if (ext_mode
== extension_require
) {
153 _mesa_glsl_error(name_locp
, state
, fmt
,
154 name
, _mesa_glsl_shader_target_name(state
->target
));
157 _mesa_glsl_warning(name_locp
, state
, fmt
,
158 name
, _mesa_glsl_shader_target_name(state
->target
));
166 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
172 printf("invariant ");
175 printf("attribute ");
198 if (q
->noperspective
)
199 printf("noperspective ");
204 ast_node::print(void) const
206 printf("unhandled node ");
210 ast_node::ast_node(void)
217 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
231 ast_compound_statement::print(void) const
235 foreach_list_const(n
, &this->statements
) {
236 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
244 ast_compound_statement::ast_compound_statement(int new_scope
,
245 ast_node
*statements
)
247 this->new_scope
= new_scope
;
249 if (statements
!= NULL
) {
250 this->statements
.push_degenerate_list_at_head(&statements
->link
);
256 ast_expression::print(void) const
270 subexpressions
[0]->print();
271 printf("%s ", operator_string(oper
));
272 subexpressions
[1]->print();
275 case ast_field_selection
:
276 subexpressions
[0]->print();
277 printf(". %s ", primary_expression
.identifier
);
286 printf("%s ", operator_string(oper
));
287 subexpressions
[0]->print();
292 subexpressions
[0]->print();
293 printf("%s ", operator_string(oper
));
296 case ast_conditional
:
297 subexpressions
[0]->print();
299 subexpressions
[1]->print();
301 subexpressions
[1]->print();
304 case ast_array_index
:
305 subexpressions
[0]->print();
307 subexpressions
[1]->print();
311 case ast_function_call
: {
312 subexpressions
[0]->print();
315 foreach_list_const (n
, &this->expressions
) {
316 if (n
!= this->expressions
.get_head())
319 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
328 printf("%s ", primary_expression
.identifier
);
331 case ast_int_constant
:
332 printf("%d ", primary_expression
.int_constant
);
335 case ast_uint_constant
:
336 printf("%u ", primary_expression
.uint_constant
);
339 case ast_float_constant
:
340 printf("%f ", primary_expression
.float_constant
);
343 case ast_bool_constant
:
345 primary_expression
.bool_constant
351 foreach_list_const(n
, & this->expressions
) {
352 if (n
!= this->expressions
.get_head())
355 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
368 ast_expression::ast_expression(int oper
,
373 this->oper
= ast_operators(oper
);
374 this->subexpressions
[0] = ex0
;
375 this->subexpressions
[1] = ex1
;
376 this->subexpressions
[2] = ex2
;
381 ast_expression_statement::print(void) const
390 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
398 ast_function::print(void) const
400 return_type
->print();
401 printf(" %s (", identifier
);
403 foreach_list_const(n
, & this->parameters
) {
404 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
412 ast_function::ast_function(void)
413 : is_definition(false), signature(NULL
)
420 ast_fully_specified_type::print(void) const
422 _mesa_ast_type_qualifier_print(& qualifier
);
428 ast_parameter_declarator::print(void) const
432 printf("%s ", identifier
);
433 ast_opt_array_size_print(is_array
, array_size
);
438 ast_function_definition::print(void) const
446 ast_declaration::print(void) const
448 printf("%s ", identifier
);
449 ast_opt_array_size_print(is_array
, array_size
);
453 initializer
->print();
458 ast_declaration::ast_declaration(char *identifier
, int is_array
,
459 ast_expression
*array_size
,
460 ast_expression
*initializer
)
462 this->identifier
= identifier
;
463 this->is_array
= is_array
;
464 this->array_size
= array_size
;
465 this->initializer
= initializer
;
470 ast_declarator_list::print(void) const
472 assert(type
|| invariant
);
477 printf("invariant ");
479 foreach_list_const (ptr
, & this->declarations
) {
480 if (ptr
!= this->declarations
.get_head())
483 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
491 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
494 this->invariant
= false;
498 ast_jump_statement::print(void) const
502 printf("continue; ");
509 if (opt_return_value
)
510 opt_return_value
->print();
521 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
523 this->mode
= ast_jump_modes(mode
);
525 if (mode
== ast_return
)
526 opt_return_value
= return_value
;
531 ast_selection_statement::print(void) const
537 then_statement
->print();
539 if (else_statement
) {
541 else_statement
->print();
547 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
548 ast_node
*then_statement
,
549 ast_node
*else_statement
)
551 this->condition
= condition
;
552 this->then_statement
= then_statement
;
553 this->else_statement
= else_statement
;
558 ast_iteration_statement::print(void) const
564 init_statement
->print();
572 rest_expression
->print();
598 ast_iteration_statement::ast_iteration_statement(int mode
,
601 ast_expression
*rest_expression
,
604 this->mode
= ast_iteration_modes(mode
);
605 this->init_statement
= init
;
606 this->condition
= condition
;
607 this->rest_expression
= rest_expression
;
613 ast_struct_specifier::print(void) const
615 printf("struct %s { ", name
);
616 foreach_list_const(n
, &this->declarations
) {
617 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
624 ast_struct_specifier::ast_struct_specifier(char *identifier
,
625 ast_node
*declarator_list
)
628 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);