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"
37 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct __GLcontextRec
*ctx
,
38 GLenum target
, void *mem_ctx
)
41 case GL_VERTEX_SHADER
: this->target
= vertex_shader
; break;
42 case GL_FRAGMENT_SHADER
: this->target
= fragment_shader
; break;
43 case GL_GEOMETRY_SHADER
: this->target
= geometry_shader
; break;
47 this->translation_unit
.make_empty();
48 this->symbols
= new(mem_ctx
) glsl_symbol_table
;
49 this->info_log
= talloc_strdup(mem_ctx
, "");
51 this->loop_or_switch_nesting
= NULL
;
52 this->ARB_texture_rectangle_enable
= true;
55 this->extensions
= &ctx
->Extensions
;
57 this->Const
.MaxLights
= ctx
->Const
.MaxLights
;
58 this->Const
.MaxClipPlanes
= ctx
->Const
.MaxClipPlanes
;
59 this->Const
.MaxTextureUnits
= ctx
->Const
.MaxTextureUnits
;
60 this->Const
.MaxTextureCoords
= ctx
->Const
.MaxTextureCoordUnits
;
61 this->Const
.MaxVertexAttribs
= ctx
->Const
.VertexProgram
.MaxAttribs
;
62 this->Const
.MaxVertexUniformComponents
= ctx
->Const
.VertexProgram
.MaxUniformComponents
;
63 this->Const
.MaxVaryingFloats
= ctx
->Const
.MaxVarying
* 4;
64 this->Const
.MaxVertexTextureImageUnits
= ctx
->Const
.MaxVertexTextureImageUnits
;
65 this->Const
.MaxCombinedTextureImageUnits
= ctx
->Const
.MaxCombinedTextureImageUnits
;
66 this->Const
.MaxTextureImageUnits
= ctx
->Const
.MaxTextureImageUnits
;
67 this->Const
.MaxFragmentUniformComponents
= ctx
->Const
.FragmentProgram
.MaxUniformComponents
;
69 this->Const
.MaxDrawBuffers
= ctx
->Const
.MaxDrawBuffers
;
71 /* If there is no GL context (standalone compiler), fill in constants
72 * with the minimum required values.
74 static struct gl_extensions null_extensions
;
76 memset(&null_extensions
, 0, sizeof(null_extensions
));
77 null_extensions
.ARB_draw_buffers
= GL_TRUE
;
78 null_extensions
.ARB_fragment_coord_conventions
= GL_TRUE
;
79 null_extensions
.EXT_texture_array
= GL_TRUE
;
80 null_extensions
.NV_texture_rectangle
= GL_TRUE
;
82 this->extensions
= &null_extensions
;
85 this->Const
.MaxLights
= 8;
86 this->Const
.MaxClipPlanes
= 8;
87 this->Const
.MaxTextureUnits
= 2;
89 /* More than the 1.10 minimum to appease parser tests taken from
90 * apps that (hopefully) already checked the number of coords.
92 this->Const
.MaxTextureCoords
= 4;
94 this->Const
.MaxVertexAttribs
= 16;
95 this->Const
.MaxVertexUniformComponents
= 512;
96 this->Const
.MaxVaryingFloats
= 32;
97 this->Const
.MaxVertexTextureImageUnits
= 0;
98 this->Const
.MaxCombinedTextureImageUnits
= 2;
99 this->Const
.MaxTextureImageUnits
= 2;
100 this->Const
.MaxFragmentUniformComponents
= 64;
102 this->Const
.MaxDrawBuffers
= 2;
107 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
110 case vertex_shader
: return "vertex";
111 case fragment_shader
: return "fragment";
112 case geometry_shader
: return "geometry";
113 case ir_shader
: break;
116 assert(!"Should not get here.");
122 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
123 const char *fmt
, ...)
129 assert(state
->info_log
!= NULL
);
130 state
->info_log
= talloc_asprintf_append(state
->info_log
,
131 "%u:%u(%u): error: ",
136 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
138 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
143 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
144 const char *fmt
, ...)
148 assert(state
->info_log
!= NULL
);
149 state
->info_log
= talloc_asprintf_append(state
->info_log
,
150 "%u:%u(%u): warning: ",
155 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
157 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
162 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
163 const char *behavior
, YYLTYPE
*behavior_locp
,
164 _mesa_glsl_parse_state
*state
)
173 if (strcmp(behavior
, "warn") == 0) {
174 ext_mode
= extension_warn
;
175 } else if (strcmp(behavior
, "require") == 0) {
176 ext_mode
= extension_require
;
177 } else if (strcmp(behavior
, "enable") == 0) {
178 ext_mode
= extension_enable
;
179 } else if (strcmp(behavior
, "disable") == 0) {
180 ext_mode
= extension_disable
;
182 _mesa_glsl_error(behavior_locp
, state
,
183 "Unknown extension behavior `%s'",
188 bool unsupported
= false;
190 if (strcmp(name
, "all") == 0) {
191 if ((ext_mode
== extension_enable
) || (ext_mode
== extension_require
)) {
192 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
193 (ext_mode
== extension_enable
)
194 ? "enable" : "require");
197 } else if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
198 /* This extension is only supported in fragment shaders.
200 if (state
->target
!= fragment_shader
) {
203 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
204 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
206 } else if (strcmp(name
, "GL_ARB_fragment_coord_conventions") == 0) {
207 state
->ARB_fragment_coord_conventions_enable
=
208 (ext_mode
!= extension_disable
);
209 state
->ARB_fragment_coord_conventions_warn
=
210 (ext_mode
== extension_warn
);
212 unsupported
= !state
->extensions
->ARB_fragment_coord_conventions
;
213 } else if (strcmp(name
, "GL_ARB_texture_rectangle") == 0) {
214 state
->ARB_texture_rectangle_enable
= (ext_mode
!= extension_disable
);
215 state
->ARB_texture_rectangle_warn
= (ext_mode
== extension_warn
);
216 } else if (strcmp(name
, "GL_EXT_texture_array") == 0) {
217 state
->EXT_texture_array_enable
= (ext_mode
!= extension_disable
);
218 state
->EXT_texture_array_warn
= (ext_mode
== extension_warn
);
220 unsupported
= !state
->extensions
->EXT_texture_array
;
226 static const char *const fmt
= "extension `%s' unsupported in %s shader";
228 if (ext_mode
== extension_require
) {
229 _mesa_glsl_error(name_locp
, state
, fmt
,
230 name
, _mesa_glsl_shader_target_name(state
->target
));
233 _mesa_glsl_warning(name_locp
, state
, fmt
,
234 name
, _mesa_glsl_shader_target_name(state
->target
));
242 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
248 printf("invariant ");
251 printf("attribute ");
274 if (q
->noperspective
)
275 printf("noperspective ");
280 ast_node::print(void) const
282 printf("unhandled node ");
286 ast_node::ast_node(void)
288 this->location
.source
= 0;
289 this->location
.line
= 0;
290 this->location
.column
= 0;
295 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
309 ast_compound_statement::print(void) const
313 foreach_list_const(n
, &this->statements
) {
314 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
322 ast_compound_statement::ast_compound_statement(int new_scope
,
323 ast_node
*statements
)
325 this->new_scope
= new_scope
;
327 if (statements
!= NULL
) {
328 this->statements
.push_degenerate_list_at_head(&statements
->link
);
334 ast_expression::print(void) const
348 subexpressions
[0]->print();
349 printf("%s ", operator_string(oper
));
350 subexpressions
[1]->print();
353 case ast_field_selection
:
354 subexpressions
[0]->print();
355 printf(". %s ", primary_expression
.identifier
);
364 printf("%s ", operator_string(oper
));
365 subexpressions
[0]->print();
370 subexpressions
[0]->print();
371 printf("%s ", operator_string(oper
));
374 case ast_conditional
:
375 subexpressions
[0]->print();
377 subexpressions
[1]->print();
379 subexpressions
[1]->print();
382 case ast_array_index
:
383 subexpressions
[0]->print();
385 subexpressions
[1]->print();
389 case ast_function_call
: {
390 subexpressions
[0]->print();
393 foreach_list_const (n
, &this->expressions
) {
394 if (n
!= this->expressions
.get_head())
397 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
406 printf("%s ", primary_expression
.identifier
);
409 case ast_int_constant
:
410 printf("%d ", primary_expression
.int_constant
);
413 case ast_uint_constant
:
414 printf("%u ", primary_expression
.uint_constant
);
417 case ast_float_constant
:
418 printf("%f ", primary_expression
.float_constant
);
421 case ast_bool_constant
:
423 primary_expression
.bool_constant
429 foreach_list_const(n
, & this->expressions
) {
430 if (n
!= this->expressions
.get_head())
433 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
446 ast_expression::ast_expression(int oper
,
451 this->oper
= ast_operators(oper
);
452 this->subexpressions
[0] = ex0
;
453 this->subexpressions
[1] = ex1
;
454 this->subexpressions
[2] = ex2
;
459 ast_expression_statement::print(void) const
468 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
476 ast_function::print(void) const
478 return_type
->print();
479 printf(" %s (", identifier
);
481 foreach_list_const(n
, & this->parameters
) {
482 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
490 ast_function::ast_function(void)
491 : is_definition(false), signature(NULL
)
498 ast_fully_specified_type::print(void) const
500 _mesa_ast_type_qualifier_print(& qualifier
);
506 ast_parameter_declarator::print(void) const
510 printf("%s ", identifier
);
511 ast_opt_array_size_print(is_array
, array_size
);
516 ast_function_definition::print(void) const
524 ast_declaration::print(void) const
526 printf("%s ", identifier
);
527 ast_opt_array_size_print(is_array
, array_size
);
531 initializer
->print();
536 ast_declaration::ast_declaration(char *identifier
, int is_array
,
537 ast_expression
*array_size
,
538 ast_expression
*initializer
)
540 this->identifier
= identifier
;
541 this->is_array
= is_array
;
542 this->array_size
= array_size
;
543 this->initializer
= initializer
;
548 ast_declarator_list::print(void) const
550 assert(type
|| invariant
);
555 printf("invariant ");
557 foreach_list_const (ptr
, & this->declarations
) {
558 if (ptr
!= this->declarations
.get_head())
561 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
569 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
572 this->invariant
= false;
576 ast_jump_statement::print(void) const
580 printf("continue; ");
587 if (opt_return_value
)
588 opt_return_value
->print();
599 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
601 this->mode
= ast_jump_modes(mode
);
603 if (mode
== ast_return
)
604 opt_return_value
= return_value
;
609 ast_selection_statement::print(void) const
615 then_statement
->print();
617 if (else_statement
) {
619 else_statement
->print();
625 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
626 ast_node
*then_statement
,
627 ast_node
*else_statement
)
629 this->condition
= condition
;
630 this->then_statement
= then_statement
;
631 this->else_statement
= else_statement
;
636 ast_iteration_statement::print(void) const
642 init_statement
->print();
650 rest_expression
->print();
676 ast_iteration_statement::ast_iteration_statement(int mode
,
679 ast_expression
*rest_expression
,
682 this->mode
= ast_iteration_modes(mode
);
683 this->init_statement
= init
;
684 this->condition
= condition
;
685 this->rest_expression
= rest_expression
;
691 ast_struct_specifier::print(void) const
693 printf("struct %s { ", name
);
694 foreach_list_const(n
, &this->declarations
) {
695 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
702 ast_struct_specifier::ast_struct_specifier(char *identifier
,
703 ast_node
*declarator_list
)
706 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);