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 this->extensions
= &null_extensions
;
80 this->Const
.MaxLights
= 8;
81 this->Const
.MaxClipPlanes
= 8;
82 this->Const
.MaxTextureUnits
= 2;
84 /* More than the 1.10 minimum to appease parser tests taken from
85 * apps that (hopefully) already checked the number of coords.
87 this->Const
.MaxTextureCoords
= 4;
89 this->Const
.MaxVertexAttribs
= 16;
90 this->Const
.MaxVertexUniformComponents
= 512;
91 this->Const
.MaxVaryingFloats
= 32;
92 this->Const
.MaxVertexTextureImageUnits
= 0;
93 this->Const
.MaxCombinedTextureImageUnits
= 2;
94 this->Const
.MaxTextureImageUnits
= 2;
95 this->Const
.MaxFragmentUniformComponents
= 64;
97 this->Const
.MaxDrawBuffers
= 2;
102 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
105 case vertex_shader
: return "vertex";
106 case fragment_shader
: return "fragment";
107 case geometry_shader
: return "geometry";
108 case ir_shader
: break;
111 assert(!"Should not get here.");
117 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
118 const char *fmt
, ...)
124 assert(state
->info_log
!= NULL
);
125 state
->info_log
= talloc_asprintf_append(state
->info_log
,
126 "%u:%u(%u): error: ",
131 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
133 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
138 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
139 const char *fmt
, ...)
143 assert(state
->info_log
!= NULL
);
144 state
->info_log
= talloc_asprintf_append(state
->info_log
,
145 "%u:%u(%u): warning: ",
150 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
152 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
157 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
158 const char *behavior
, YYLTYPE
*behavior_locp
,
159 _mesa_glsl_parse_state
*state
)
168 if (strcmp(behavior
, "warn") == 0) {
169 ext_mode
= extension_warn
;
170 } else if (strcmp(behavior
, "require") == 0) {
171 ext_mode
= extension_require
;
172 } else if (strcmp(behavior
, "enable") == 0) {
173 ext_mode
= extension_enable
;
174 } else if (strcmp(behavior
, "disable") == 0) {
175 ext_mode
= extension_disable
;
177 _mesa_glsl_error(behavior_locp
, state
,
178 "Unknown extension behavior `%s'",
183 bool unsupported
= false;
185 if (strcmp(name
, "all") == 0) {
186 if ((ext_mode
== extension_enable
) || (ext_mode
== extension_require
)) {
187 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
188 (ext_mode
== extension_enable
)
189 ? "enable" : "require");
192 } else if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
193 /* This extension is only supported in fragment shaders.
195 if (state
->target
!= fragment_shader
) {
198 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
199 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
201 } else if (strcmp(name
, "GL_ARB_texture_rectangle") == 0) {
202 state
->ARB_texture_rectangle_enable
= (ext_mode
!= extension_disable
);
203 state
->ARB_texture_rectangle_warn
= (ext_mode
== extension_warn
);
204 } else if (strcmp(name
, "GL_EXT_texture_array") == 0) {
205 state
->EXT_texture_array_enable
= (ext_mode
!= extension_disable
);
206 state
->EXT_texture_array_warn
= (ext_mode
== extension_warn
);
208 unsupported
= !state
->extensions
->EXT_texture_array
;
214 static const char *const fmt
= "extension `%s' unsupported in %s shader";
216 if (ext_mode
== extension_require
) {
217 _mesa_glsl_error(name_locp
, state
, fmt
,
218 name
, _mesa_glsl_shader_target_name(state
->target
));
221 _mesa_glsl_warning(name_locp
, state
, fmt
,
222 name
, _mesa_glsl_shader_target_name(state
->target
));
230 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
236 printf("invariant ");
239 printf("attribute ");
262 if (q
->noperspective
)
263 printf("noperspective ");
268 ast_node::print(void) const
270 printf("unhandled node ");
274 ast_node::ast_node(void)
281 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
295 ast_compound_statement::print(void) const
299 foreach_list_const(n
, &this->statements
) {
300 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
308 ast_compound_statement::ast_compound_statement(int new_scope
,
309 ast_node
*statements
)
311 this->new_scope
= new_scope
;
313 if (statements
!= NULL
) {
314 this->statements
.push_degenerate_list_at_head(&statements
->link
);
320 ast_expression::print(void) const
334 subexpressions
[0]->print();
335 printf("%s ", operator_string(oper
));
336 subexpressions
[1]->print();
339 case ast_field_selection
:
340 subexpressions
[0]->print();
341 printf(". %s ", primary_expression
.identifier
);
350 printf("%s ", operator_string(oper
));
351 subexpressions
[0]->print();
356 subexpressions
[0]->print();
357 printf("%s ", operator_string(oper
));
360 case ast_conditional
:
361 subexpressions
[0]->print();
363 subexpressions
[1]->print();
365 subexpressions
[1]->print();
368 case ast_array_index
:
369 subexpressions
[0]->print();
371 subexpressions
[1]->print();
375 case ast_function_call
: {
376 subexpressions
[0]->print();
379 foreach_list_const (n
, &this->expressions
) {
380 if (n
!= this->expressions
.get_head())
383 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
392 printf("%s ", primary_expression
.identifier
);
395 case ast_int_constant
:
396 printf("%d ", primary_expression
.int_constant
);
399 case ast_uint_constant
:
400 printf("%u ", primary_expression
.uint_constant
);
403 case ast_float_constant
:
404 printf("%f ", primary_expression
.float_constant
);
407 case ast_bool_constant
:
409 primary_expression
.bool_constant
415 foreach_list_const(n
, & this->expressions
) {
416 if (n
!= this->expressions
.get_head())
419 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
432 ast_expression::ast_expression(int oper
,
437 this->oper
= ast_operators(oper
);
438 this->subexpressions
[0] = ex0
;
439 this->subexpressions
[1] = ex1
;
440 this->subexpressions
[2] = ex2
;
445 ast_expression_statement::print(void) const
454 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
462 ast_function::print(void) const
464 return_type
->print();
465 printf(" %s (", identifier
);
467 foreach_list_const(n
, & this->parameters
) {
468 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
476 ast_function::ast_function(void)
477 : is_definition(false), signature(NULL
)
484 ast_fully_specified_type::print(void) const
486 _mesa_ast_type_qualifier_print(& qualifier
);
492 ast_parameter_declarator::print(void) const
496 printf("%s ", identifier
);
497 ast_opt_array_size_print(is_array
, array_size
);
502 ast_function_definition::print(void) const
510 ast_declaration::print(void) const
512 printf("%s ", identifier
);
513 ast_opt_array_size_print(is_array
, array_size
);
517 initializer
->print();
522 ast_declaration::ast_declaration(char *identifier
, int is_array
,
523 ast_expression
*array_size
,
524 ast_expression
*initializer
)
526 this->identifier
= identifier
;
527 this->is_array
= is_array
;
528 this->array_size
= array_size
;
529 this->initializer
= initializer
;
534 ast_declarator_list::print(void) const
536 assert(type
|| invariant
);
541 printf("invariant ");
543 foreach_list_const (ptr
, & this->declarations
) {
544 if (ptr
!= this->declarations
.get_head())
547 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
555 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
558 this->invariant
= false;
562 ast_jump_statement::print(void) const
566 printf("continue; ");
573 if (opt_return_value
)
574 opt_return_value
->print();
585 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
587 this->mode
= ast_jump_modes(mode
);
589 if (mode
== ast_return
)
590 opt_return_value
= return_value
;
595 ast_selection_statement::print(void) const
601 then_statement
->print();
603 if (else_statement
) {
605 else_statement
->print();
611 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
612 ast_node
*then_statement
,
613 ast_node
*else_statement
)
615 this->condition
= condition
;
616 this->then_statement
= then_statement
;
617 this->else_statement
= else_statement
;
622 ast_iteration_statement::print(void) const
628 init_statement
->print();
636 rest_expression
->print();
662 ast_iteration_statement::ast_iteration_statement(int mode
,
665 ast_expression
*rest_expression
,
668 this->mode
= ast_iteration_modes(mode
);
669 this->init_statement
= init
;
670 this->condition
= condition
;
671 this->rest_expression
= rest_expression
;
677 ast_struct_specifier::print(void) const
679 printf("struct %s { ", name
);
680 foreach_list_const(n
, &this->declarations
) {
681 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
688 ast_struct_specifier::ast_struct_specifier(char *identifier
,
689 ast_node
*declarator_list
)
692 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);