d3dcompiler: "void" is not a plain scalar data type.
[wine/multimedia.git] / dlls / d3dcompiler_43 / hlsl.y
blob177330f2d3dc51e7961094c46549297c7e1ea257
1 /*
2 * HLSL parser
4 * Copyright 2008 Stefan Dösinger
5 * Copyright 2012 Matteo Bruni for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/debug.h"
25 #include <stdio.h>
27 #include "d3dcompiler_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(hlsl_parser);
31 int hlsl_lex(void);
33 struct hlsl_parse_ctx hlsl_ctx;
35 struct YYLTYPE;
36 static void set_location(struct source_location *loc, const struct YYLTYPE *l);
38 void hlsl_message(const char *fmt, ...)
40 va_list args;
42 va_start(args, fmt);
43 compilation_message(&hlsl_ctx.messages, fmt, args);
44 va_end(args);
47 static const char *hlsl_get_error_level_name(enum hlsl_error_level level)
49 const char *names[] =
51 "error",
52 "warning",
53 "note",
55 return names[level];
58 void hlsl_report_message(const char *filename, DWORD line, DWORD column,
59 enum hlsl_error_level level, const char *fmt, ...)
61 va_list args;
62 char *string = NULL;
63 int rc, size = 0;
65 while (1)
67 va_start(args, fmt);
68 rc = vsnprintf(string, size, fmt, args);
69 va_end(args);
71 if (rc >= 0 && rc < size)
72 break;
74 if (rc >= size)
75 size = rc + 1;
76 else
77 size = size ? size * 2 : 32;
79 if (!string)
80 string = d3dcompiler_alloc(size);
81 else
82 string = d3dcompiler_realloc(string, size);
83 if (!string)
85 ERR("Error reallocating memory for a string.\n");
86 return;
90 hlsl_message("%s:%u:%u: %s: %s\n", filename, line, column, hlsl_get_error_level_name(level), string);
91 d3dcompiler_free(string);
93 if (level == HLSL_LEVEL_ERROR)
94 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
95 else if (level == HLSL_LEVEL_WARNING)
96 set_parse_status(&hlsl_ctx.status, PARSE_WARN);
99 static void hlsl_error(const char *s)
101 hlsl_report_message(hlsl_ctx.source_file, hlsl_ctx.line_no, hlsl_ctx.column, HLSL_LEVEL_ERROR, "%s", s);
104 static void debug_dump_decl(struct hlsl_type *type, DWORD modifiers, const char *declname, unsigned int line_no)
106 TRACE("Line %u: ", line_no);
107 if (modifiers)
108 TRACE("%s ", debug_modifiers(modifiers));
109 TRACE("%s %s;\n", debug_hlsl_type(type), declname);
112 static void check_invalid_matrix_modifiers(DWORD modifiers, struct source_location *loc)
114 if (modifiers & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR))
116 hlsl_report_message(loc->file, loc->line, loc->col, HLSL_LEVEL_ERROR,
117 "'row_major' or 'column_major' modifiers are only allowed for matrices");
121 static BOOL declare_variable(struct hlsl_ir_var *decl, BOOL local)
123 BOOL ret;
125 TRACE("Declaring variable %s.\n", decl->name);
126 if (decl->node.data_type->type == HLSL_CLASS_MATRIX)
128 if (!(decl->modifiers & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR)))
130 decl->modifiers |= hlsl_ctx.matrix_majority == HLSL_ROW_MAJOR
131 ? HLSL_MODIFIER_ROW_MAJOR : HLSL_MODIFIER_COLUMN_MAJOR;
134 else
135 check_invalid_matrix_modifiers(decl->modifiers, &decl->node.loc);
137 if (local)
139 DWORD invalid = decl->modifiers & (HLSL_STORAGE_EXTERN | HLSL_STORAGE_SHARED
140 | HLSL_STORAGE_GROUPSHARED | HLSL_STORAGE_UNIFORM);
141 if (invalid)
143 hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_ERROR,
144 "modifier '%s' invalid for local variables", debug_modifiers(invalid));
146 if (decl->semantic)
148 hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_ERROR,
149 "semantics are not allowed on local variables");
150 return FALSE;
153 else
155 if (find_function(decl->name))
157 hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_ERROR,
158 "redefinition of '%s'", decl->name);
159 return FALSE;
162 ret = add_declaration(hlsl_ctx.cur_scope, decl, local);
163 if (!ret)
165 struct hlsl_ir_var *old = get_variable(hlsl_ctx.cur_scope, decl->name);
167 hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_ERROR,
168 "\"%s\" already declared", decl->name);
169 hlsl_report_message(old->node.loc.file, old->node.loc.line, old->node.loc.col, HLSL_LEVEL_NOTE,
170 "\"%s\" was previously declared here", old->name);
171 return FALSE;
173 return TRUE;
176 static DWORD add_modifier(DWORD modifiers, DWORD mod, const struct YYLTYPE *loc);
178 static unsigned int components_count_expr_list(struct list *list)
180 struct hlsl_ir_node *node;
181 unsigned int count = 0;
183 LIST_FOR_EACH_ENTRY(node, list, struct hlsl_ir_node, entry)
185 count += components_count_type(node->data_type);
187 return count;
192 %locations
193 %error-verbose
195 %union
197 struct hlsl_type *type;
198 INT intval;
199 FLOAT floatval;
200 BOOL boolval;
201 char *name;
202 DWORD modifiers;
203 struct hlsl_ir_var *var;
204 struct hlsl_ir_node *instr;
205 struct list *list;
206 struct hlsl_ir_function_decl *function;
207 struct parse_parameter parameter;
208 struct parse_variable_def *variable_def;
209 enum parse_unary_op unary_op;
212 %token KW_BLENDSTATE
213 %token KW_BREAK
214 %token KW_BUFFER
215 %token KW_CBUFFER
216 %token KW_COLUMN_MAJOR
217 %token KW_COMPILE
218 %token KW_CONST
219 %token KW_CONTINUE
220 %token KW_DEPTHSTENCILSTATE
221 %token KW_DEPTHSTENCILVIEW
222 %token KW_DISCARD
223 %token KW_DO
224 %token KW_DOUBLE
225 %token KW_ELSE
226 %token KW_EXTERN
227 %token KW_FALSE
228 %token KW_FOR
229 %token KW_GEOMETRYSHADER
230 %token KW_GROUPSHARED
231 %token KW_IF
232 %token KW_IN
233 %token KW_INLINE
234 %token KW_INOUT
235 %token KW_MATRIX
236 %token KW_NAMESPACE
237 %token KW_NOINTERPOLATION
238 %token KW_OUT
239 %token KW_PASS
240 %token KW_PIXELSHADER
241 %token KW_PRECISE
242 %token KW_RASTERIZERSTATE
243 %token KW_RENDERTARGETVIEW
244 %token KW_RETURN
245 %token KW_REGISTER
246 %token KW_ROW_MAJOR
247 %token KW_SAMPLER
248 %token KW_SAMPLER1D
249 %token KW_SAMPLER2D
250 %token KW_SAMPLER3D
251 %token KW_SAMPLERCUBE
252 %token KW_SAMPLER_STATE
253 %token KW_SAMPLERCOMPARISONSTATE
254 %token KW_SHARED
255 %token KW_STATEBLOCK
256 %token KW_STATEBLOCK_STATE
257 %token KW_STATIC
258 %token KW_STRING
259 %token KW_STRUCT
260 %token KW_SWITCH
261 %token KW_TBUFFER
262 %token KW_TECHNIQUE
263 %token KW_TECHNIQUE10
264 %token KW_TEXTURE
265 %token KW_TEXTURE1D
266 %token KW_TEXTURE1DARRAY
267 %token KW_TEXTURE2D
268 %token KW_TEXTURE2DARRAY
269 %token KW_TEXTURE2DMS
270 %token KW_TEXTURE2DMSARRAY
271 %token KW_TEXTURE3D
272 %token KW_TEXTURE3DARRAY
273 %token KW_TEXTURECUBE
274 %token KW_TRUE
275 %token KW_TYPEDEF
276 %token KW_UNIFORM
277 %token KW_VECTOR
278 %token KW_VERTEXSHADER
279 %token KW_VOID
280 %token KW_VOLATILE
281 %token KW_WHILE
283 %token OP_INC
284 %token OP_DEC
285 %token OP_AND
286 %token OP_OR
287 %token OP_EQ
288 %token OP_LEFTSHIFT
289 %token OP_LEFTSHIFTASSIGN
290 %token OP_RIGHTSHIFT
291 %token OP_RIGHTSHIFTASSIGN
292 %token OP_ELLIPSIS
293 %token OP_LE
294 %token OP_GE
295 %token OP_NE
296 %token OP_ADDASSIGN
297 %token OP_SUBASSIGN
298 %token OP_MULASSIGN
299 %token OP_DIVASSIGN
300 %token OP_MODASSIGN
301 %token OP_ANDASSIGN
302 %token OP_ORASSIGN
303 %token OP_XORASSIGN
304 %token OP_UNKNOWN1
305 %token OP_UNKNOWN2
306 %token OP_UNKNOWN3
307 %token OP_UNKNOWN4
309 %token <intval> PRE_LINE
311 %token <name> VAR_IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
312 %type <name> any_identifier var_identifier
313 %token <name> STRING
314 %token <floatval> C_FLOAT
315 %token <intval> C_INTEGER
316 %type <boolval> boolean
317 %type <type> base_type
318 %type <type> type
319 %type <list> declaration_statement
320 %type <list> complex_initializer
321 %type <list> initializer_expr_list
322 %type <instr> initializer_expr
323 %type <modifiers> var_modifiers
324 %type <list> parameters
325 %type <list> param_list
326 %type <instr> expr
327 %type <var> variable
328 %type <intval> array
329 %type <list> statement
330 %type <list> statement_list
331 %type <list> compound_statement
332 %type <function> func_declaration
333 %type <function> func_prototype
334 %type <parameter> parameter
335 %type <name> semantic
336 %type <variable_def> variable_def
337 %type <list> variables_def
338 %type <instr> primary_expr
339 %type <instr> postfix_expr
340 %type <instr> unary_expr
341 %type <instr> mul_expr
342 %type <instr> add_expr
343 %type <instr> shift_expr
344 %type <instr> relational_expr
345 %type <instr> equality_expr
346 %type <instr> bitand_expr
347 %type <instr> bitxor_expr
348 %type <instr> bitor_expr
349 %type <instr> logicand_expr
350 %type <instr> logicor_expr
351 %type <instr> conditional_expr
352 %type <instr> assignment_expr
353 %type <list> expr_statement
354 %type <unary_op> unary_op
355 %type <modifiers> input_mod
358 hlsl_prog: /* empty */
361 | hlsl_prog func_declaration
363 FIXME("Check that the function doesn't conflict with an already declared one.\n");
364 list_add_tail(&hlsl_ctx.functions, &$2->node.entry);
366 | hlsl_prog declaration_statement
368 TRACE("Declaration statement parsed.\n");
370 | hlsl_prog preproc_directive
374 preproc_directive: PRE_LINE STRING
376 TRACE("Updating line information to file %s, line %u\n", debugstr_a($2), $1);
377 hlsl_ctx.line_no = $1;
378 if (strcmp($2, hlsl_ctx.source_file))
380 const char **new_array;
382 hlsl_ctx.source_file = $2;
383 new_array = d3dcompiler_realloc(hlsl_ctx.source_files,
384 sizeof(*hlsl_ctx.source_files) * hlsl_ctx.source_files_count + 1);
385 if (new_array)
387 hlsl_ctx.source_files = new_array;
388 hlsl_ctx.source_files[hlsl_ctx.source_files_count++] = $2;
393 any_identifier: VAR_IDENTIFIER
394 | TYPE_IDENTIFIER
395 | NEW_IDENTIFIER
397 func_declaration: func_prototype compound_statement
399 TRACE("Function %s parsed.\n", $1->name);
400 $$ = $1;
401 $$->body = $2;
402 pop_scope(&hlsl_ctx);
404 | func_prototype ';'
406 TRACE("Function prototype for %s.\n", $1->name);
407 $$ = $1;
408 pop_scope(&hlsl_ctx);
411 func_prototype: var_modifiers type var_identifier '(' parameters ')' semantic
413 if (get_variable(hlsl_ctx.globals, $3))
415 hlsl_report_message(hlsl_ctx.source_file, @3.first_line, @3.first_column,
416 HLSL_LEVEL_ERROR, "redefinition of '%s'\n", $3);
417 return 1;
419 if ($2->base_type == HLSL_TYPE_VOID && $7)
421 hlsl_report_message(hlsl_ctx.source_file, @7.first_line, @7.first_column,
422 HLSL_LEVEL_ERROR, "void function with a semantic");
425 $$ = new_func_decl($3, $2, $5);
426 if (!$$)
428 ERR("Out of memory.\n");
429 return -1;
431 $$->semantic = $7;
434 compound_statement: '{' '}'
436 $$ = d3dcompiler_alloc(sizeof(*$$));
437 list_init($$);
439 | '{' scope_start statement_list '}'
441 pop_scope(&hlsl_ctx);
442 $$ = $3;
445 scope_start: /* Empty */
447 push_scope(&hlsl_ctx);
450 var_identifier: VAR_IDENTIFIER
451 | NEW_IDENTIFIER
453 semantic: /* Empty */
455 $$ = NULL;
457 | ':' any_identifier
459 $$ = $2;
462 parameters: scope_start
464 $$ = d3dcompiler_alloc(sizeof(*$$));
465 list_init($$);
467 | scope_start param_list
469 $$ = $2;
472 param_list: parameter
474 struct source_location loc;
476 $$ = d3dcompiler_alloc(sizeof(*$$));
477 list_init($$);
478 set_location(&loc, &@1);
479 if (!add_func_parameter($$, &$1, &loc))
481 ERR("Error adding function parameter %s.\n", $1.name);
482 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
483 return -1;
486 | param_list ',' parameter
488 struct source_location loc;
490 $$ = $1;
491 set_location(&loc, &@3);
492 if (!add_func_parameter($$, &$3, &loc))
494 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
495 "duplicate parameter %s", $3.name);
496 return 1;
500 parameter: input_mod var_modifiers type any_identifier semantic
502 $$.modifiers = $1;
503 $$.modifiers |= $2;
504 $$.type = $3;
505 $$.name = $4;
506 $$.semantic = $5;
509 input_mod: /* Empty */
511 $$ = HLSL_MODIFIER_IN;
513 | KW_IN
515 $$ = HLSL_MODIFIER_IN;
517 | KW_OUT
519 $$ = HLSL_MODIFIER_OUT;
521 | KW_INOUT
523 $$ = HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT;
526 type: base_type
528 $$ = $1;
530 | KW_VECTOR '<' base_type ',' C_INTEGER '>'
532 if ($3->type != HLSL_CLASS_SCALAR)
534 hlsl_message("Line %u: vectors of non-scalar types are not allowed.\n",
535 hlsl_ctx.line_no);
536 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
537 return 1;
539 if ($5 < 1 || $5 > 4)
541 hlsl_message("Line %u: vector size must be between 1 and 4.\n",
542 hlsl_ctx.line_no);
543 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
544 return 1;
547 $$ = new_hlsl_type(NULL, HLSL_CLASS_VECTOR, $3->base_type, $5, 1);
549 | KW_MATRIX '<' base_type ',' C_INTEGER ',' C_INTEGER '>'
551 if ($3->type != HLSL_CLASS_SCALAR)
553 hlsl_message("Line %u: matrices of non-scalar types are not allowed.\n",
554 hlsl_ctx.line_no);
555 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
556 return 1;
558 if ($5 < 1 || $5 > 4 || $7 < 1 || $7 > 4)
560 hlsl_message("Line %u: matrix dimensions must be between 1 and 4.\n",
561 hlsl_ctx.line_no);
562 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
563 return 1;
566 $$ = new_hlsl_type(NULL, HLSL_CLASS_MATRIX, $3->base_type, $5, $7);
569 base_type: KW_VOID
571 $$ = new_hlsl_type(d3dcompiler_strdup("void"), HLSL_CLASS_OBJECT, HLSL_TYPE_VOID, 1, 1);
573 | KW_SAMPLER
575 $$ = new_hlsl_type(d3dcompiler_strdup("sampler"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
576 $$->sampler_dim = HLSL_SAMPLER_DIM_GENERIC;
578 | KW_SAMPLER1D
580 $$ = new_hlsl_type(d3dcompiler_strdup("sampler1D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
581 $$->sampler_dim = HLSL_SAMPLER_DIM_1D;
583 | KW_SAMPLER2D
585 $$ = new_hlsl_type(d3dcompiler_strdup("sampler2D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
586 $$->sampler_dim = HLSL_SAMPLER_DIM_2D;
588 | KW_SAMPLER3D
590 $$ = new_hlsl_type(d3dcompiler_strdup("sampler3D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
591 $$->sampler_dim = HLSL_SAMPLER_DIM_3D;
593 | KW_SAMPLERCUBE
595 $$ = new_hlsl_type(d3dcompiler_strdup("samplerCUBE"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
596 $$->sampler_dim = HLSL_SAMPLER_DIM_CUBE;
598 | TYPE_IDENTIFIER
600 struct hlsl_type *type;
602 TRACE("Type %s.\n", $1);
603 type = get_type(hlsl_ctx.cur_scope, $1, TRUE);
604 $$ = type;
605 d3dcompiler_free($1);
607 | KW_STRUCT TYPE_IDENTIFIER
609 struct hlsl_type *type;
611 TRACE("Struct type %s.\n", $2);
612 type = get_type(hlsl_ctx.cur_scope, $2, TRUE);
613 if (type->type != HLSL_CLASS_STRUCT)
615 hlsl_message("Line %u: redefining %s as a structure.\n",
616 hlsl_ctx.line_no, $2);
617 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
619 else
621 $$ = type;
623 d3dcompiler_free($2);
626 declaration_statement: declaration
628 $$ = d3dcompiler_alloc(sizeof(*$$));
629 list_init($$);
632 declaration: var_modifiers type variables_def ';'
634 struct parse_variable_def *v, *v_next;
635 struct hlsl_ir_var *var;
636 BOOL ret, local = TRUE;
638 LIST_FOR_EACH_ENTRY_SAFE(v, v_next, $3, struct parse_variable_def, entry)
640 debug_dump_decl($2, $1, v->name, hlsl_ctx.line_no);
641 var = d3dcompiler_alloc(sizeof(*var));
642 var->node.type = HLSL_IR_VAR;
643 if (v->array_size)
644 var->node.data_type = new_array_type($2, v->array_size);
645 else
646 var->node.data_type = $2;
647 var->node.loc = v->loc;
648 var->name = v->name;
649 var->modifiers = $1;
650 var->semantic = v->semantic;
652 if (hlsl_ctx.cur_scope == hlsl_ctx.globals)
654 var->modifiers |= HLSL_STORAGE_UNIFORM;
655 local = FALSE;
658 if (var->modifiers & HLSL_MODIFIER_CONST && !v->initializer)
660 hlsl_report_message(v->loc.file, v->loc.line, v->loc.col,
661 HLSL_LEVEL_ERROR, "const variable without initializer");
662 free_declaration(var);
663 d3dcompiler_free(v);
664 continue;
667 ret = declare_variable(var, local);
668 if (!ret)
669 free_declaration(var);
670 else
671 TRACE("Declared variable %s.\n", var->name);
673 if (v->initializer)
675 FIXME("Variable with an initializer.\n");
676 free_instr_list(v->initializer);
679 d3dcompiler_free(v);
681 d3dcompiler_free($3);
684 variables_def: variable_def
686 $$ = d3dcompiler_alloc(sizeof(*$$));
687 list_init($$);
688 list_add_head($$, &$1->entry);
690 | variables_def ',' variable_def
692 $$ = $1;
693 list_add_tail($$, &$3->entry);
696 variable_def: any_identifier array semantic
698 $$ = d3dcompiler_alloc(sizeof(*$$));
699 set_location(&$$->loc, &@1);
700 $$->name = $1;
701 $$->array_size = $2;
702 $$->semantic = $3;
704 | any_identifier array semantic '=' complex_initializer
706 TRACE("Declaration with initializer.\n");
707 $$ = d3dcompiler_alloc(sizeof(*$$));
708 set_location(&$$->loc, &@1);
709 $$->name = $1;
710 $$->array_size = $2;
711 $$->semantic = $3;
712 $$->initializer = $5;
715 array: /* Empty */
717 $$ = 0;
719 | '[' expr ']'
721 FIXME("Array.\n");
722 $$ = 0;
723 free_instr($2);
726 var_modifiers: /* Empty */
728 $$ = 0;
730 | KW_EXTERN var_modifiers
732 $$ = add_modifier($2, HLSL_STORAGE_EXTERN, &@1);
734 | KW_NOINTERPOLATION var_modifiers
736 $$ = add_modifier($2, HLSL_STORAGE_NOINTERPOLATION, &@1);
738 | KW_PRECISE var_modifiers
740 $$ = add_modifier($2, HLSL_MODIFIER_PRECISE, &@1);
742 | KW_SHARED var_modifiers
744 $$ = add_modifier($2, HLSL_STORAGE_SHARED, &@1);
746 | KW_GROUPSHARED var_modifiers
748 $$ = add_modifier($2, HLSL_STORAGE_GROUPSHARED, &@1);
750 | KW_STATIC var_modifiers
752 $$ = add_modifier($2, HLSL_STORAGE_STATIC, &@1);
754 | KW_UNIFORM var_modifiers
756 $$ = add_modifier($2, HLSL_STORAGE_UNIFORM, &@1);
758 | KW_VOLATILE var_modifiers
760 $$ = add_modifier($2, HLSL_STORAGE_VOLATILE, &@1);
762 | KW_CONST var_modifiers
764 $$ = add_modifier($2, HLSL_MODIFIER_CONST, &@1);
766 | KW_ROW_MAJOR var_modifiers
768 $$ = add_modifier($2, HLSL_MODIFIER_ROW_MAJOR, &@1);
770 | KW_COLUMN_MAJOR var_modifiers
772 $$ = add_modifier($2, HLSL_MODIFIER_COLUMN_MAJOR, &@1);
775 complex_initializer: initializer_expr
777 $$ = d3dcompiler_alloc(sizeof(*$$));
778 list_init($$);
779 list_add_head($$, &$1->entry);
781 | '{' initializer_expr_list '}'
783 $$ = $2;
786 initializer_expr: assignment_expr
788 $$ = $1;
791 initializer_expr_list: initializer_expr
793 $$ = d3dcompiler_alloc(sizeof(*$$));
794 list_init($$);
795 list_add_head($$, &$1->entry);
797 | initializer_expr_list ',' initializer_expr
799 $$ = $1;
800 list_add_tail($$, &$3->entry);
803 boolean: KW_TRUE
805 $$ = TRUE;
807 | KW_FALSE
809 $$ = FALSE;
812 statement_list: statement
814 $$ = $1;
816 | statement_list statement
818 $$ = $1;
819 list_move_tail($$, $2);
820 d3dcompiler_free($2);
823 statement: declaration_statement
825 $$ = $1;
827 | expr_statement
829 $$ = $1;
831 | compound_statement
833 $$ = $1;
836 expr_statement: ';'
838 $$ = d3dcompiler_alloc(sizeof(*$$));
839 list_init($$);
841 | expr ';'
843 $$ = d3dcompiler_alloc(sizeof(*$$));
844 list_init($$);
845 if ($1)
846 list_add_head($$, &$1->entry);
849 primary_expr: C_FLOAT
851 struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
852 if (!c)
854 ERR("Out of memory.\n");
855 return -1;
857 c->node.type = HLSL_IR_CONSTANT;
858 set_location(&c->node.loc, &yylloc);
859 c->node.data_type = new_hlsl_type("float", HLSL_CLASS_SCALAR, HLSL_TYPE_FLOAT, 1, 1);
860 c->v.value.f[0] = $1;
861 $$ = &c->node;
863 | C_INTEGER
865 struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
866 if (!c)
868 ERR("Out of memory.\n");
869 return -1;
871 c->node.type = HLSL_IR_CONSTANT;
872 set_location(&c->node.loc, &yylloc);
873 c->node.data_type = new_hlsl_type("int", HLSL_CLASS_SCALAR, HLSL_TYPE_INT, 1, 1);
874 c->v.value.i[0] = $1;
875 $$ = &c->node;
877 | boolean
879 struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
880 if (!c)
882 ERR("Out of memory.\n");
883 return -1;
885 c->node.type = HLSL_IR_CONSTANT;
886 set_location(&c->node.loc, &yylloc);
887 c->node.data_type = new_hlsl_type("bool", HLSL_CLASS_SCALAR, HLSL_TYPE_BOOL, 1, 1);
888 c->v.value.b[0] = $1;
889 $$ = &c->node;
891 | variable
893 struct hlsl_ir_deref *deref = new_var_deref($1);
894 if (deref)
896 $$ = &deref->node;
897 set_location(&$$->loc, &@1);
899 else
900 $$ = NULL;
902 | '(' expr ')'
904 $$ = $2;
907 variable: VAR_IDENTIFIER
909 struct hlsl_ir_var *var;
910 var = get_variable(hlsl_ctx.cur_scope, $1);
911 if (!var)
913 hlsl_message("Line %d: variable '%s' not declared\n",
914 hlsl_ctx.line_no, $1);
915 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
916 return 1;
918 $$ = var;
921 postfix_expr: primary_expr
923 $$ = $1;
925 | postfix_expr OP_INC
927 struct hlsl_ir_node *operands[3];
928 struct source_location loc;
930 operands[0] = $1;
931 operands[1] = operands[2] = NULL;
932 set_location(&loc, &@2);
933 $$ = &new_expr(HLSL_IR_BINOP_POSTINC, operands, &loc)->node;
935 | postfix_expr OP_DEC
937 struct hlsl_ir_node *operands[3];
938 struct source_location loc;
940 operands[0] = $1;
941 operands[1] = operands[2] = NULL;
942 set_location(&loc, &@2);
943 $$ = &new_expr(HLSL_IR_BINOP_POSTDEC, operands, &loc)->node;
945 /* "var_modifiers" doesn't make sense in this case, but it's needed
946 in the grammar to avoid shift/reduce conflicts. */
947 | var_modifiers type '(' initializer_expr_list ')'
949 struct hlsl_ir_constructor *constructor;
951 TRACE("%s constructor.\n", debug_hlsl_type($2));
952 if ($1)
954 hlsl_message("Line %u: unexpected modifier in a constructor.\n",
955 hlsl_ctx.line_no);
956 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
957 return -1;
959 if ($2->type > HLSL_CLASS_LAST_NUMERIC)
961 hlsl_message("Line %u: constructors are allowed only for numeric data types.\n",
962 hlsl_ctx.line_no);
963 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
964 return -1;
966 if ($2->dimx * $2->dimy != components_count_expr_list($4))
968 hlsl_message("Line %u: wrong number of components in constructor.\n",
969 hlsl_ctx.line_no);
970 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
971 return -1;
974 constructor = d3dcompiler_alloc(sizeof(*constructor));
975 constructor->node.type = HLSL_IR_CONSTRUCTOR;
976 set_location(&constructor->node.loc, &@3);
977 constructor->node.data_type = $2;
978 constructor->arguments = $4;
980 $$ = &constructor->node;
983 unary_expr: postfix_expr
985 $$ = $1;
987 | OP_INC unary_expr
989 struct hlsl_ir_node *operands[3];
990 struct source_location loc;
992 operands[0] = $2;
993 operands[1] = operands[2] = NULL;
994 set_location(&loc, &@1);
995 $$ = &new_expr(HLSL_IR_BINOP_PREINC, operands, &loc)->node;
997 | OP_DEC unary_expr
999 struct hlsl_ir_node *operands[3];
1000 struct source_location loc;
1002 operands[0] = $2;
1003 operands[1] = operands[2] = NULL;
1004 set_location(&loc, &@1);
1005 $$ = &new_expr(HLSL_IR_BINOP_PREDEC, operands, &loc)->node;
1007 | unary_op unary_expr
1009 enum hlsl_ir_expr_op ops[] = {0, HLSL_IR_UNOP_NEG,
1010 HLSL_IR_UNOP_LOGIC_NOT, HLSL_IR_UNOP_BIT_NOT};
1011 struct hlsl_ir_node *operands[3];
1012 struct source_location loc;
1014 if ($1 == UNARY_OP_PLUS)
1016 $$ = $2;
1018 else
1020 operands[0] = $2;
1021 operands[1] = operands[2] = NULL;
1022 set_location(&loc, &@1);
1023 $$ = &new_expr(ops[$1], operands, &loc)->node;
1027 unary_op: '+'
1029 $$ = UNARY_OP_PLUS;
1031 | '-'
1033 $$ = UNARY_OP_MINUS;
1035 | '!'
1037 $$ = UNARY_OP_LOGICNOT;
1039 | '~'
1041 $$ = UNARY_OP_BITNOT;
1044 mul_expr: unary_expr
1046 $$ = $1;
1048 | mul_expr '*' unary_expr
1050 struct source_location loc;
1052 set_location(&loc, &@2);
1053 $$ = &hlsl_mul($1, $3, &loc)->node;
1055 | mul_expr '/' unary_expr
1057 struct source_location loc;
1059 set_location(&loc, &@2);
1060 $$ = &hlsl_div($1, $3, &loc)->node;
1062 | mul_expr '%' unary_expr
1064 struct source_location loc;
1066 set_location(&loc, &@2);
1067 $$ = &hlsl_mod($1, $3, &loc)->node;
1070 add_expr: mul_expr
1072 $$ = $1;
1074 | add_expr '+' mul_expr
1076 struct source_location loc;
1078 set_location(&loc, &@2);
1079 $$ = &hlsl_add($1, $3, &loc)->node;
1081 | add_expr '-' mul_expr
1083 struct source_location loc;
1085 set_location(&loc, &@2);
1086 $$ = &hlsl_sub($1, $3, &loc)->node;
1089 shift_expr: add_expr
1091 $$ = $1;
1093 | shift_expr OP_LEFTSHIFT add_expr
1095 FIXME("Left shift\n");
1097 | shift_expr OP_RIGHTSHIFT add_expr
1099 FIXME("Right shift\n");
1102 relational_expr: shift_expr
1104 $$ = $1;
1106 | relational_expr '<' shift_expr
1108 struct source_location loc;
1110 set_location(&loc, &@2);
1111 $$ = &hlsl_lt($1, $3, &loc)->node;
1113 | relational_expr '>' shift_expr
1115 struct source_location loc;
1117 set_location(&loc, &@2);
1118 $$ = &hlsl_gt($1, $3, &loc)->node;
1120 | relational_expr OP_LE shift_expr
1122 struct source_location loc;
1124 set_location(&loc, &@2);
1125 $$ = &hlsl_le($1, $3, &loc)->node;
1127 | relational_expr OP_GE shift_expr
1129 struct source_location loc;
1131 set_location(&loc, &@2);
1132 $$ = &hlsl_ge($1, $3, &loc)->node;
1135 equality_expr: relational_expr
1137 $$ = $1;
1139 | equality_expr OP_EQ relational_expr
1141 struct source_location loc;
1143 set_location(&loc, &@2);
1144 $$ = &hlsl_eq($1, $3, &loc)->node;
1146 | equality_expr OP_NE relational_expr
1148 struct source_location loc;
1150 set_location(&loc, &@2);
1151 $$ = &hlsl_ne($1, $3, &loc)->node;
1154 bitand_expr: equality_expr
1156 $$ = $1;
1158 | bitand_expr '&' equality_expr
1160 FIXME("bitwise AND\n");
1163 bitxor_expr: bitand_expr
1165 $$ = $1;
1167 | bitxor_expr '^' bitand_expr
1169 FIXME("bitwise XOR\n");
1172 bitor_expr: bitxor_expr
1174 $$ = $1;
1176 | bitor_expr '|' bitxor_expr
1178 FIXME("bitwise OR\n");
1181 logicand_expr: bitor_expr
1183 $$ = $1;
1185 | logicand_expr OP_AND bitor_expr
1187 FIXME("logic AND\n");
1190 logicor_expr: logicand_expr
1192 $$ = $1;
1194 | logicor_expr OP_OR logicand_expr
1196 FIXME("logic OR\n");
1199 conditional_expr: logicor_expr
1201 $$ = $1;
1203 | logicor_expr '?' expr ':' assignment_expr
1205 FIXME("ternary operator\n");
1208 assignment_expr: conditional_expr
1210 $$ = $1;
1213 expr: assignment_expr
1215 $$ = $1;
1217 | expr ',' assignment_expr
1219 FIXME("Comma expression\n");
1224 static void set_location(struct source_location *loc, const struct YYLTYPE *l)
1226 loc->file = hlsl_ctx.source_file;
1227 loc->line = l->first_line;
1228 loc->col = l->first_column;
1231 static DWORD add_modifier(DWORD modifiers, DWORD mod, const struct YYLTYPE *loc)
1233 if (modifiers & mod)
1235 hlsl_report_message(hlsl_ctx.source_file, loc->first_line, loc->first_column, HLSL_LEVEL_ERROR,
1236 "modifier '%s' already specified", debug_modifiers(mod));
1237 return modifiers;
1239 if (mod & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR)
1240 && modifiers & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR))
1242 hlsl_report_message(hlsl_ctx.source_file, loc->first_line, loc->first_column, HLSL_LEVEL_ERROR,
1243 "more than one matrix majority keyword");
1244 return modifiers;
1246 return modifiers | mod;
1249 struct bwriter_shader *parse_hlsl(enum shader_type type, DWORD major, DWORD minor,
1250 const char *entrypoint, char **messages)
1252 struct hlsl_ir_function_decl *function;
1253 struct hlsl_scope *scope, *next_scope;
1254 struct hlsl_type *hlsl_type, *next_type;
1255 struct hlsl_ir_var *var, *next_var;
1256 unsigned int i;
1258 hlsl_ctx.status = PARSE_SUCCESS;
1259 hlsl_ctx.messages.size = hlsl_ctx.messages.capacity = 0;
1260 hlsl_ctx.line_no = hlsl_ctx.column = 1;
1261 hlsl_ctx.source_file = d3dcompiler_strdup("");
1262 hlsl_ctx.source_files = d3dcompiler_alloc(sizeof(*hlsl_ctx.source_files));
1263 if (hlsl_ctx.source_files)
1264 hlsl_ctx.source_files[0] = hlsl_ctx.source_file;
1265 hlsl_ctx.source_files_count = 1;
1266 hlsl_ctx.cur_scope = NULL;
1267 hlsl_ctx.matrix_majority = HLSL_COLUMN_MAJOR;
1268 list_init(&hlsl_ctx.scopes);
1269 list_init(&hlsl_ctx.types);
1270 list_init(&hlsl_ctx.functions);
1272 push_scope(&hlsl_ctx);
1273 hlsl_ctx.globals = hlsl_ctx.cur_scope;
1275 hlsl_parse();
1277 if (TRACE_ON(hlsl_parser))
1279 struct hlsl_ir_function_decl *func;
1281 TRACE("IR dump.\n");
1282 LIST_FOR_EACH_ENTRY(func, &hlsl_ctx.functions, struct hlsl_ir_function_decl, node.entry)
1284 if (func->body)
1285 debug_dump_ir_function(func);
1289 TRACE("Compilation status = %d\n", hlsl_ctx.status);
1290 if (messages)
1292 if (hlsl_ctx.messages.size)
1293 *messages = hlsl_ctx.messages.string;
1294 else
1295 *messages = NULL;
1297 else
1299 if (hlsl_ctx.messages.capacity)
1300 d3dcompiler_free(hlsl_ctx.messages.string);
1303 for (i = 0; i < hlsl_ctx.source_files_count; ++i)
1304 d3dcompiler_free((void *)hlsl_ctx.source_files[i]);
1305 d3dcompiler_free(hlsl_ctx.source_files);
1307 TRACE("Freeing functions IR.\n");
1308 LIST_FOR_EACH_ENTRY(function, &hlsl_ctx.functions, struct hlsl_ir_function_decl, node.entry)
1309 free_function(function);
1311 TRACE("Freeing variables.\n");
1312 LIST_FOR_EACH_ENTRY_SAFE(scope, next_scope, &hlsl_ctx.scopes, struct hlsl_scope, entry)
1314 LIST_FOR_EACH_ENTRY_SAFE(var, next_var, &scope->vars, struct hlsl_ir_var, scope_entry)
1316 free_declaration(var);
1318 d3dcompiler_free(scope);
1321 TRACE("Freeing types.\n");
1322 LIST_FOR_EACH_ENTRY_SAFE(hlsl_type, next_type, &hlsl_ctx.types, struct hlsl_type, entry)
1324 free_hlsl_type(hlsl_type);
1327 return NULL;