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 static struct gl_extensions null_extensions
;
73 memset(&null_extensions
, 0, sizeof(null_extensions
));
74 this->extensions
= &null_extensions
;
79 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
82 case vertex_shader
: return "vertex";
83 case fragment_shader
: return "fragment";
84 case geometry_shader
: return "geometry";
85 case ir_shader
: break;
88 assert(!"Should not get here.");
94 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
101 assert(state
->info_log
!= NULL
);
102 state
->info_log
= talloc_asprintf_append(state
->info_log
,
103 "%u:%u(%u): error: ",
108 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
110 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
115 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
116 const char *fmt
, ...)
120 assert(state
->info_log
!= NULL
);
121 state
->info_log
= talloc_asprintf_append(state
->info_log
,
122 "%u:%u(%u): warning: ",
127 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
129 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
134 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
135 const char *behavior
, YYLTYPE
*behavior_locp
,
136 _mesa_glsl_parse_state
*state
)
145 if (strcmp(behavior
, "warn") == 0) {
146 ext_mode
= extension_warn
;
147 } else if (strcmp(behavior
, "require") == 0) {
148 ext_mode
= extension_require
;
149 } else if (strcmp(behavior
, "enable") == 0) {
150 ext_mode
= extension_enable
;
151 } else if (strcmp(behavior
, "disable") == 0) {
152 ext_mode
= extension_disable
;
154 _mesa_glsl_error(behavior_locp
, state
,
155 "Unknown extension behavior `%s'",
160 bool unsupported
= false;
162 if (strcmp(name
, "all") == 0) {
163 if ((ext_mode
== extension_enable
) || (ext_mode
== extension_require
)) {
164 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
165 (ext_mode
== extension_enable
)
166 ? "enable" : "require");
169 } else if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
170 /* This extension is only supported in fragment shaders.
172 if (state
->target
!= fragment_shader
) {
175 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
176 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
178 } else if (strcmp(name
, "GL_ARB_texture_rectangle") == 0) {
179 state
->ARB_texture_rectangle_enable
= (ext_mode
!= extension_disable
);
180 state
->ARB_texture_rectangle_warn
= (ext_mode
== extension_warn
);
181 } else if (strcmp(name
, "GL_EXT_texture_array") == 0) {
182 state
->EXT_texture_array_enable
= (ext_mode
!= extension_disable
);
183 state
->EXT_texture_array_warn
= (ext_mode
== extension_warn
);
185 unsupported
= !state
->extensions
->EXT_texture_array
;
191 static const char *const fmt
= "extension `%s' unsupported in %s shader";
193 if (ext_mode
== extension_require
) {
194 _mesa_glsl_error(name_locp
, state
, fmt
,
195 name
, _mesa_glsl_shader_target_name(state
->target
));
198 _mesa_glsl_warning(name_locp
, state
, fmt
,
199 name
, _mesa_glsl_shader_target_name(state
->target
));
207 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
213 printf("invariant ");
216 printf("attribute ");
239 if (q
->noperspective
)
240 printf("noperspective ");
245 ast_node::print(void) const
247 printf("unhandled node ");
251 ast_node::ast_node(void)
258 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
272 ast_compound_statement::print(void) const
276 foreach_list_const(n
, &this->statements
) {
277 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
285 ast_compound_statement::ast_compound_statement(int new_scope
,
286 ast_node
*statements
)
288 this->new_scope
= new_scope
;
290 if (statements
!= NULL
) {
291 this->statements
.push_degenerate_list_at_head(&statements
->link
);
297 ast_expression::print(void) const
311 subexpressions
[0]->print();
312 printf("%s ", operator_string(oper
));
313 subexpressions
[1]->print();
316 case ast_field_selection
:
317 subexpressions
[0]->print();
318 printf(". %s ", primary_expression
.identifier
);
327 printf("%s ", operator_string(oper
));
328 subexpressions
[0]->print();
333 subexpressions
[0]->print();
334 printf("%s ", operator_string(oper
));
337 case ast_conditional
:
338 subexpressions
[0]->print();
340 subexpressions
[1]->print();
342 subexpressions
[1]->print();
345 case ast_array_index
:
346 subexpressions
[0]->print();
348 subexpressions
[1]->print();
352 case ast_function_call
: {
353 subexpressions
[0]->print();
356 foreach_list_const (n
, &this->expressions
) {
357 if (n
!= this->expressions
.get_head())
360 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
369 printf("%s ", primary_expression
.identifier
);
372 case ast_int_constant
:
373 printf("%d ", primary_expression
.int_constant
);
376 case ast_uint_constant
:
377 printf("%u ", primary_expression
.uint_constant
);
380 case ast_float_constant
:
381 printf("%f ", primary_expression
.float_constant
);
384 case ast_bool_constant
:
386 primary_expression
.bool_constant
392 foreach_list_const(n
, & this->expressions
) {
393 if (n
!= this->expressions
.get_head())
396 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
409 ast_expression::ast_expression(int oper
,
414 this->oper
= ast_operators(oper
);
415 this->subexpressions
[0] = ex0
;
416 this->subexpressions
[1] = ex1
;
417 this->subexpressions
[2] = ex2
;
422 ast_expression_statement::print(void) const
431 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
439 ast_function::print(void) const
441 return_type
->print();
442 printf(" %s (", identifier
);
444 foreach_list_const(n
, & this->parameters
) {
445 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
453 ast_function::ast_function(void)
454 : is_definition(false), signature(NULL
)
461 ast_fully_specified_type::print(void) const
463 _mesa_ast_type_qualifier_print(& qualifier
);
469 ast_parameter_declarator::print(void) const
473 printf("%s ", identifier
);
474 ast_opt_array_size_print(is_array
, array_size
);
479 ast_function_definition::print(void) const
487 ast_declaration::print(void) const
489 printf("%s ", identifier
);
490 ast_opt_array_size_print(is_array
, array_size
);
494 initializer
->print();
499 ast_declaration::ast_declaration(char *identifier
, int is_array
,
500 ast_expression
*array_size
,
501 ast_expression
*initializer
)
503 this->identifier
= identifier
;
504 this->is_array
= is_array
;
505 this->array_size
= array_size
;
506 this->initializer
= initializer
;
511 ast_declarator_list::print(void) const
513 assert(type
|| invariant
);
518 printf("invariant ");
520 foreach_list_const (ptr
, & this->declarations
) {
521 if (ptr
!= this->declarations
.get_head())
524 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
532 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
535 this->invariant
= false;
539 ast_jump_statement::print(void) const
543 printf("continue; ");
550 if (opt_return_value
)
551 opt_return_value
->print();
562 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
564 this->mode
= ast_jump_modes(mode
);
566 if (mode
== ast_return
)
567 opt_return_value
= return_value
;
572 ast_selection_statement::print(void) const
578 then_statement
->print();
580 if (else_statement
) {
582 else_statement
->print();
588 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
589 ast_node
*then_statement
,
590 ast_node
*else_statement
)
592 this->condition
= condition
;
593 this->then_statement
= then_statement
;
594 this->else_statement
= else_statement
;
599 ast_iteration_statement::print(void) const
605 init_statement
->print();
613 rest_expression
->print();
639 ast_iteration_statement::ast_iteration_statement(int mode
,
642 ast_expression
*rest_expression
,
645 this->mode
= ast_iteration_modes(mode
);
646 this->init_statement
= init
;
647 this->condition
= condition
;
648 this->rest_expression
= rest_expression
;
654 ast_struct_specifier::print(void) const
656 printf("struct %s { ", name
);
657 foreach_list_const(n
, &this->declarations
) {
658 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
665 ast_struct_specifier::ast_struct_specifier(char *identifier
,
666 ast_node
*declarator_list
)
669 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);