usp10: Add GPOS features to scripts as defined by VOLT.
[wine/multimedia.git] / dlls / d3dcompiler_43 / hlsl.y
blobcf0c7b5f87c88d518d2c0f865122e28d59cc3598
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 BOOL declare_variable(struct hlsl_ir_var *decl, BOOL local)
114 BOOL ret;
116 TRACE("Declaring variable %s.\n", decl->name);
117 if (decl->node.data_type->type == HLSL_CLASS_MATRIX)
119 if (!(decl->modifiers & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR)))
121 decl->modifiers |= hlsl_ctx.matrix_majority == HLSL_ROW_MAJOR
122 ? HLSL_MODIFIER_ROW_MAJOR : HLSL_MODIFIER_COLUMN_MAJOR;
125 if (local)
127 DWORD invalid = decl->modifiers & (HLSL_STORAGE_EXTERN | HLSL_STORAGE_SHARED
128 | HLSL_STORAGE_GROUPSHARED | HLSL_STORAGE_UNIFORM);
129 if (invalid)
131 hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_ERROR,
132 "modifier '%s' invalid for local variables", debug_modifiers(invalid));
135 ret = add_declaration(hlsl_ctx.cur_scope, decl, local);
136 if (ret == FALSE)
138 struct hlsl_ir_var *old = get_variable(hlsl_ctx.cur_scope, decl->name);
140 hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_ERROR,
141 "\"%s\" already declared", decl->name);
142 hlsl_report_message(old->node.loc.file, old->node.loc.line, old->node.loc.col, HLSL_LEVEL_NOTE,
143 "\"%s\" was previously declared here", old->name);
144 return FALSE;
146 return TRUE;
149 static DWORD add_modifier(DWORD modifiers, DWORD mod, const struct YYLTYPE *loc);
151 static unsigned int components_count_expr_list(struct list *list)
153 struct hlsl_ir_node *node;
154 unsigned int count = 0;
156 LIST_FOR_EACH_ENTRY(node, list, struct hlsl_ir_node, entry)
158 count += components_count_type(node->data_type);
160 return count;
165 %locations
166 %error-verbose
168 %union
170 struct hlsl_type *type;
171 INT intval;
172 FLOAT floatval;
173 BOOL boolval;
174 char *name;
175 DWORD modifiers;
176 struct hlsl_ir_var *var;
177 struct hlsl_ir_node *instr;
178 struct list *list;
179 struct hlsl_ir_function_decl *function;
180 struct parse_parameter parameter;
181 struct parse_variable_def *variable_def;
184 %token KW_BLENDSTATE
185 %token KW_BREAK
186 %token KW_BUFFER
187 %token KW_CBUFFER
188 %token KW_COLUMN_MAJOR
189 %token KW_COMPILE
190 %token KW_CONST
191 %token KW_CONTINUE
192 %token KW_DEPTHSTENCILSTATE
193 %token KW_DEPTHSTENCILVIEW
194 %token KW_DISCARD
195 %token KW_DO
196 %token KW_DOUBLE
197 %token KW_ELSE
198 %token KW_EXTERN
199 %token KW_FALSE
200 %token KW_FOR
201 %token KW_GEOMETRYSHADER
202 %token KW_GROUPSHARED
203 %token KW_IF
204 %token KW_IN
205 %token KW_INLINE
206 %token KW_INOUT
207 %token KW_MATRIX
208 %token KW_NAMESPACE
209 %token KW_NOINTERPOLATION
210 %token KW_OUT
211 %token KW_PASS
212 %token KW_PIXELSHADER
213 %token KW_PRECISE
214 %token KW_RASTERIZERSTATE
215 %token KW_RENDERTARGETVIEW
216 %token KW_RETURN
217 %token KW_REGISTER
218 %token KW_ROW_MAJOR
219 %token KW_SAMPLER
220 %token KW_SAMPLER1D
221 %token KW_SAMPLER2D
222 %token KW_SAMPLER3D
223 %token KW_SAMPLERCUBE
224 %token KW_SAMPLER_STATE
225 %token KW_SAMPLERCOMPARISONSTATE
226 %token KW_SHARED
227 %token KW_STATEBLOCK
228 %token KW_STATEBLOCK_STATE
229 %token KW_STATIC
230 %token KW_STRING
231 %token KW_STRUCT
232 %token KW_SWITCH
233 %token KW_TBUFFER
234 %token KW_TECHNIQUE
235 %token KW_TECHNIQUE10
236 %token KW_TEXTURE
237 %token KW_TEXTURE1D
238 %token KW_TEXTURE1DARRAY
239 %token KW_TEXTURE2D
240 %token KW_TEXTURE2DARRAY
241 %token KW_TEXTURE2DMS
242 %token KW_TEXTURE2DMSARRAY
243 %token KW_TEXTURE3D
244 %token KW_TEXTURE3DARRAY
245 %token KW_TEXTURECUBE
246 %token KW_TRUE
247 %token KW_TYPEDEF
248 %token KW_UNIFORM
249 %token KW_VECTOR
250 %token KW_VERTEXSHADER
251 %token KW_VOID
252 %token KW_VOLATILE
253 %token KW_WHILE
255 %token OP_INC
256 %token OP_DEC
257 %token OP_AND
258 %token OP_OR
259 %token OP_EQ
260 %token OP_LEFTSHIFT
261 %token OP_LEFTSHIFTASSIGN
262 %token OP_RIGHTSHIFT
263 %token OP_RIGHTSHIFTASSIGN
264 %token OP_ELLIPSIS
265 %token OP_LE
266 %token OP_GE
267 %token OP_NE
268 %token OP_ADDASSIGN
269 %token OP_SUBASSIGN
270 %token OP_MULASSIGN
271 %token OP_DIVASSIGN
272 %token OP_MODASSIGN
273 %token OP_ANDASSIGN
274 %token OP_ORASSIGN
275 %token OP_XORASSIGN
276 %token OP_UNKNOWN1
277 %token OP_UNKNOWN2
278 %token OP_UNKNOWN3
279 %token OP_UNKNOWN4
281 %token <intval> PRE_LINE
283 %token <name> VAR_IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
284 %type <name> any_identifier var_identifier
285 %token <name> STRING
286 %token <floatval> C_FLOAT
287 %token <intval> C_INTEGER
288 %type <boolval> boolean
289 %type <type> base_type
290 %type <type> type
291 %type <list> declaration_statement
292 %type <list> complex_initializer
293 %type <list> initializer_expr_list
294 %type <instr> initializer_expr
295 %type <modifiers> var_modifiers
296 %type <list> parameters
297 %type <list> param_list
298 %type <instr> expr
299 %type <var> variable
300 %type <intval> array
301 %type <list> statement
302 %type <list> statement_list
303 %type <list> compound_statement
304 %type <function> func_declaration
305 %type <function> func_prototype
306 %type <parameter> parameter
307 %type <name> semantic
308 %type <variable_def> variable_def
309 %type <list> variables_def
310 %type <instr> primary_expr
311 %type <instr> postfix_expr
312 %type <instr> unary_expr
313 %type <instr> mul_expr
314 %type <instr> add_expr
315 %type <instr> shift_expr
316 %type <instr> relational_expr
317 %type <instr> equality_expr
318 %type <instr> bitand_expr
319 %type <instr> bitxor_expr
320 %type <instr> bitor_expr
321 %type <instr> logicand_expr
322 %type <instr> logicor_expr
323 %type <instr> conditional_expr
324 %type <instr> assignment_expr
325 %type <list> expr_statement
326 %type <modifiers> input_mod
329 hlsl_prog: /* empty */
332 | hlsl_prog func_declaration
334 FIXME("Check that the function doesn't conflict with an already declared one.\n");
335 list_add_tail(&hlsl_ctx.functions, &$2->node.entry);
337 | hlsl_prog declaration_statement
339 TRACE("Declaration statement parsed.\n");
341 | hlsl_prog preproc_directive
345 preproc_directive: PRE_LINE STRING
347 TRACE("Updating line information to file %s, line %u\n", debugstr_a($2), $1);
348 hlsl_ctx.line_no = $1;
349 if (strcmp($2, hlsl_ctx.source_file))
351 const char **new_array;
353 hlsl_ctx.source_file = $2;
354 new_array = d3dcompiler_realloc(hlsl_ctx.source_files,
355 sizeof(*hlsl_ctx.source_files) * hlsl_ctx.source_files_count + 1);
356 if (new_array)
358 hlsl_ctx.source_files = new_array;
359 hlsl_ctx.source_files[hlsl_ctx.source_files_count++] = $2;
364 any_identifier: VAR_IDENTIFIER
365 | TYPE_IDENTIFIER
366 | NEW_IDENTIFIER
368 func_declaration: func_prototype compound_statement
370 TRACE("Function %s parsed.\n", $1->name);
371 $$ = $1;
372 $$->body = $2;
373 pop_scope(&hlsl_ctx);
375 | func_prototype ';'
377 TRACE("Function prototype for %s.\n", $1->name);
378 $$ = $1;
379 pop_scope(&hlsl_ctx);
382 func_prototype: var_modifiers type var_identifier '(' parameters ')' semantic
384 $$ = new_func_decl($3, $2, $5);
385 if (!$$)
387 ERR("Out of memory.\n");
388 return -1;
390 $$->semantic = $7;
393 compound_statement: '{' '}'
395 $$ = d3dcompiler_alloc(sizeof(*$$));
396 list_init($$);
398 | '{' scope_start statement_list '}'
400 pop_scope(&hlsl_ctx);
401 $$ = $3;
404 scope_start: /* Empty */
406 push_scope(&hlsl_ctx);
409 var_identifier: VAR_IDENTIFIER
410 | NEW_IDENTIFIER
412 semantic: /* Empty */
414 $$ = NULL;
416 | ':' any_identifier
418 $$ = $2;
421 parameters: scope_start
423 $$ = d3dcompiler_alloc(sizeof(*$$));
424 list_init($$);
426 | scope_start param_list
428 $$ = $2;
431 param_list: parameter
433 $$ = d3dcompiler_alloc(sizeof(*$$));
434 list_init($$);
435 if (!add_func_parameter($$, &$1, hlsl_ctx.line_no))
437 ERR("Error adding function parameter %s.\n", $1.name);
438 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
439 return -1;
442 | param_list ',' parameter
444 $$ = $1;
445 if (!add_func_parameter($$, &$3, hlsl_ctx.line_no))
447 hlsl_message("Line %u: duplicate parameter %s.\n",
448 hlsl_ctx.line_no, $3.name);
449 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
450 return 1;
454 parameter: input_mod var_modifiers type any_identifier semantic
456 $$.modifiers = $1;
457 $$.modifiers |= $2;
458 $$.type = $3;
459 $$.name = $4;
460 $$.semantic = $5;
463 input_mod: /* Empty */
465 $$ = HLSL_MODIFIER_IN;
467 | KW_IN
469 $$ = HLSL_MODIFIER_IN;
471 | KW_OUT
473 $$ = HLSL_MODIFIER_OUT;
475 | KW_INOUT
477 $$ = HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT;
480 type: base_type
482 $$ = $1;
484 | KW_VECTOR '<' base_type ',' C_INTEGER '>'
486 if ($3->type != HLSL_CLASS_SCALAR)
488 hlsl_message("Line %u: vectors of non-scalar types are not allowed.\n",
489 hlsl_ctx.line_no);
490 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
491 return 1;
493 if ($5 < 1 || $5 > 4)
495 hlsl_message("Line %u: vector size must be between 1 and 4.\n",
496 hlsl_ctx.line_no);
497 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
498 return 1;
501 $$ = new_hlsl_type(NULL, HLSL_CLASS_VECTOR, $3->base_type, $5, 1);
503 | KW_MATRIX '<' base_type ',' C_INTEGER ',' C_INTEGER '>'
505 if ($3->type != HLSL_CLASS_SCALAR)
507 hlsl_message("Line %u: matrices of non-scalar types are not allowed.\n",
508 hlsl_ctx.line_no);
509 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
510 return 1;
512 if ($5 < 1 || $5 > 4 || $7 < 1 || $7 > 4)
514 hlsl_message("Line %u: matrix dimensions must be between 1 and 4.\n",
515 hlsl_ctx.line_no);
516 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
517 return 1;
520 $$ = new_hlsl_type(NULL, HLSL_CLASS_MATRIX, $3->base_type, $5, $7);
523 base_type: KW_VOID
525 $$ = new_hlsl_type(d3dcompiler_strdup("void"), HLSL_CLASS_SCALAR, HLSL_TYPE_VOID, 1, 1);
527 | KW_SAMPLER
529 $$ = new_hlsl_type(d3dcompiler_strdup("sampler"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
530 $$->sampler_dim = HLSL_SAMPLER_DIM_GENERIC;
532 | KW_SAMPLER1D
534 $$ = new_hlsl_type(d3dcompiler_strdup("sampler1D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
535 $$->sampler_dim = HLSL_SAMPLER_DIM_1D;
537 | KW_SAMPLER2D
539 $$ = new_hlsl_type(d3dcompiler_strdup("sampler2D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
540 $$->sampler_dim = HLSL_SAMPLER_DIM_2D;
542 | KW_SAMPLER3D
544 $$ = new_hlsl_type(d3dcompiler_strdup("sampler3D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
545 $$->sampler_dim = HLSL_SAMPLER_DIM_3D;
547 | KW_SAMPLERCUBE
549 $$ = new_hlsl_type(d3dcompiler_strdup("samplerCUBE"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
550 $$->sampler_dim = HLSL_SAMPLER_DIM_CUBE;
552 | TYPE_IDENTIFIER
554 struct hlsl_type *type;
556 TRACE("Type %s.\n", $1);
557 type = get_type(hlsl_ctx.cur_scope, $1, TRUE);
558 $$ = type;
559 d3dcompiler_free($1);
561 | KW_STRUCT TYPE_IDENTIFIER
563 struct hlsl_type *type;
565 TRACE("Struct type %s.\n", $2);
566 type = get_type(hlsl_ctx.cur_scope, $2, TRUE);
567 if (type->type != HLSL_CLASS_STRUCT)
569 hlsl_message("Line %u: redefining %s as a structure.\n",
570 hlsl_ctx.line_no, $2);
571 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
573 else
575 $$ = type;
577 d3dcompiler_free($2);
580 declaration_statement: declaration
582 $$ = d3dcompiler_alloc(sizeof(*$$));
583 list_init($$);
586 declaration: var_modifiers type variables_def ';'
588 struct parse_variable_def *v, *v_next;
589 struct hlsl_ir_var *var;
590 BOOL ret, local = TRUE;
592 LIST_FOR_EACH_ENTRY_SAFE(v, v_next, $3, struct parse_variable_def, entry)
594 debug_dump_decl($2, $1, v->name, hlsl_ctx.line_no);
595 var = d3dcompiler_alloc(sizeof(*var));
596 var->node.type = HLSL_IR_VAR;
597 if (v->array_size)
598 var->node.data_type = new_array_type($2, v->array_size);
599 else
600 var->node.data_type = $2;
601 var->node.loc = v->loc;
602 var->name = v->name;
603 var->modifiers = $1;
604 var->semantic = v->semantic;
605 if (v->initializer)
607 FIXME("Variable with an initializer.\n");
608 free_instr_list(v->initializer);
611 if (hlsl_ctx.cur_scope == hlsl_ctx.globals)
613 var->modifiers |= HLSL_STORAGE_UNIFORM;
614 local = FALSE;
617 ret = declare_variable(var, local);
618 if (ret == FALSE)
619 free_declaration(var);
620 else
621 TRACE("Declared variable %s.\n", var->name);
622 d3dcompiler_free(v);
624 d3dcompiler_free($3);
627 variables_def: variable_def
629 $$ = d3dcompiler_alloc(sizeof(*$$));
630 list_init($$);
631 list_add_head($$, &$1->entry);
633 | variables_def ',' variable_def
635 $$ = $1;
636 list_add_tail($$, &$3->entry);
639 /* FIXME: Local variables can't have semantics. */
640 variable_def: any_identifier array semantic
642 $$ = d3dcompiler_alloc(sizeof(*$$));
643 set_location(&$$->loc, &@1);
644 $$->name = $1;
645 $$->array_size = $2;
646 $$->semantic = $3;
648 | any_identifier array semantic '=' complex_initializer
650 TRACE("Declaration with initializer.\n");
651 $$ = d3dcompiler_alloc(sizeof(*$$));
652 set_location(&$$->loc, &@1);
653 $$->name = $1;
654 $$->array_size = $2;
655 $$->semantic = $3;
656 $$->initializer = $5;
659 array: /* Empty */
661 $$ = 0;
663 | '[' expr ']'
665 FIXME("Array.\n");
666 $$ = 0;
667 free_instr($2);
670 var_modifiers: /* Empty */
672 $$ = 0;
674 | KW_EXTERN var_modifiers
676 $$ = add_modifier($2, HLSL_STORAGE_EXTERN, &@1);
678 | KW_NOINTERPOLATION var_modifiers
680 $$ = add_modifier($2, HLSL_STORAGE_NOINTERPOLATION, &@1);
682 | KW_PRECISE var_modifiers
684 $$ = add_modifier($2, HLSL_MODIFIER_PRECISE, &@1);
686 | KW_SHARED var_modifiers
688 $$ = add_modifier($2, HLSL_STORAGE_SHARED, &@1);
690 | KW_GROUPSHARED var_modifiers
692 $$ = add_modifier($2, HLSL_STORAGE_GROUPSHARED, &@1);
694 | KW_STATIC var_modifiers
696 $$ = add_modifier($2, HLSL_STORAGE_STATIC, &@1);
698 | KW_UNIFORM var_modifiers
700 $$ = add_modifier($2, HLSL_STORAGE_UNIFORM, &@1);
702 | KW_VOLATILE var_modifiers
704 $$ = add_modifier($2, HLSL_STORAGE_VOLATILE, &@1);
706 | KW_CONST var_modifiers
708 $$ = add_modifier($2, HLSL_MODIFIER_CONST, &@1);
710 | KW_ROW_MAJOR var_modifiers
712 $$ = add_modifier($2, HLSL_MODIFIER_ROW_MAJOR, &@1);
714 | KW_COLUMN_MAJOR var_modifiers
716 $$ = add_modifier($2, HLSL_MODIFIER_COLUMN_MAJOR, &@1);
719 complex_initializer: initializer_expr
721 $$ = d3dcompiler_alloc(sizeof(*$$));
722 list_init($$);
723 list_add_head($$, &$1->entry);
725 | '{' initializer_expr_list '}'
727 $$ = $2;
730 initializer_expr: assignment_expr
732 $$ = $1;
735 initializer_expr_list: initializer_expr
737 $$ = d3dcompiler_alloc(sizeof(*$$));
738 list_init($$);
739 list_add_head($$, &$1->entry);
741 | initializer_expr_list ',' initializer_expr
743 $$ = $1;
744 list_add_tail($$, &$3->entry);
747 boolean: KW_TRUE
749 $$ = TRUE;
751 | KW_FALSE
753 $$ = FALSE;
756 statement_list: statement
758 $$ = $1;
760 | statement_list statement
762 $$ = $1;
763 list_move_tail($$, $2);
764 d3dcompiler_free($2);
767 statement: declaration_statement
769 $$ = $1;
771 | expr_statement
773 $$ = $1;
775 | compound_statement
777 $$ = $1;
780 expr_statement: ';'
782 $$ = d3dcompiler_alloc(sizeof(*$$));
783 list_init($$);
785 | expr ';'
787 $$ = d3dcompiler_alloc(sizeof(*$$));
788 list_init($$);
789 if ($1)
790 list_add_head($$, &$1->entry);
793 primary_expr: C_FLOAT
795 struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
796 if (!c)
798 ERR("Out of memory.\n");
799 return -1;
801 c->node.type = HLSL_IR_CONSTANT;
802 c->node.data_type = new_hlsl_type("float", HLSL_CLASS_SCALAR, HLSL_TYPE_FLOAT, 1, 1);
803 c->v.value.f[0] = $1;
804 $$ = &c->node;
806 | C_INTEGER
808 struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
809 if (!c)
811 ERR("Out of memory.\n");
812 return -1;
814 c->node.type = HLSL_IR_CONSTANT;
815 c->node.data_type = new_hlsl_type("int", HLSL_CLASS_SCALAR, HLSL_TYPE_INT, 1, 1);
816 c->v.value.i[0] = $1;
817 $$ = &c->node;
819 | boolean
821 struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
822 if (!c)
824 ERR("Out of memory.\n");
825 return -1;
827 c->node.type = HLSL_IR_CONSTANT;
828 c->node.data_type = new_hlsl_type("bool", HLSL_CLASS_SCALAR, HLSL_TYPE_BOOL, 1, 1);
829 c->v.value.b[0] = $1;
830 $$ = &c->node;
832 | variable
834 struct hlsl_ir_deref *deref = new_var_deref($1);
835 $$ = deref ? &deref->node : NULL;
837 | '(' expr ')'
839 $$ = $2;
842 variable: VAR_IDENTIFIER
844 struct hlsl_ir_var *var;
845 var = get_variable(hlsl_ctx.cur_scope, $1);
846 if (!var)
848 hlsl_message("Line %d: variable '%s' not declared\n",
849 hlsl_ctx.line_no, $1);
850 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
851 return 1;
853 $$ = var;
856 postfix_expr: primary_expr
858 $$ = $1;
860 /* "var_modifiers" doesn't make sense in this case, but it's needed
861 in the grammar to avoid shift/reduce conflicts. */
862 | var_modifiers type '(' initializer_expr_list ')'
864 struct hlsl_ir_constructor *constructor;
866 TRACE("%s constructor.\n", debug_hlsl_type($2));
867 if ($1)
869 hlsl_message("Line %u: unexpected modifier in a constructor.\n",
870 hlsl_ctx.line_no);
871 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
872 return -1;
874 if ($2->type > HLSL_CLASS_LAST_NUMERIC)
876 hlsl_message("Line %u: constructors are allowed only for numeric data types.\n",
877 hlsl_ctx.line_no);
878 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
879 return -1;
881 if ($2->dimx * $2->dimy != components_count_expr_list($4))
883 hlsl_message("Line %u: wrong number of components in constructor.\n",
884 hlsl_ctx.line_no);
885 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
886 return -1;
889 constructor = d3dcompiler_alloc(sizeof(*constructor));
890 constructor->node.type = HLSL_IR_CONSTRUCTOR;
891 constructor->node.data_type = $2;
892 constructor->arguments = $4;
894 $$ = &constructor->node;
897 unary_expr: postfix_expr
899 $$ = $1;
902 mul_expr: unary_expr
904 $$ = $1;
907 add_expr: mul_expr
909 $$ = $1;
912 shift_expr: add_expr
914 $$ = $1;
917 relational_expr: shift_expr
919 $$ = $1;
922 equality_expr: relational_expr
924 $$ = $1;
927 bitand_expr: equality_expr
929 $$ = $1;
932 bitxor_expr: bitand_expr
934 $$ = $1;
937 bitor_expr: bitxor_expr
939 $$ = $1;
942 logicand_expr: bitor_expr
944 $$ = $1;
947 logicor_expr: logicand_expr
949 $$ = $1;
952 conditional_expr: logicor_expr
954 $$ = $1;
957 assignment_expr: conditional_expr
959 $$ = $1;
962 expr: assignment_expr
964 $$ = $1;
966 | expr ',' assignment_expr
968 FIXME("Comma expression\n");
973 static void set_location(struct source_location *loc, const struct YYLTYPE *l)
975 loc->file = hlsl_ctx.source_file;
976 loc->line = l->first_line;
977 loc->col = l->first_column;
980 static DWORD add_modifier(DWORD modifiers, DWORD mod, const struct YYLTYPE *loc)
982 if (modifiers & mod)
984 hlsl_report_message(hlsl_ctx.source_file, loc->first_line, loc->first_column, HLSL_LEVEL_ERROR,
985 "modifier '%s' already specified", debug_modifiers(mod));
986 return modifiers;
988 if (mod & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR)
989 && modifiers & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR))
991 hlsl_report_message(hlsl_ctx.source_file, loc->first_line, loc->first_column, HLSL_LEVEL_ERROR,
992 "more than one matrix majority keyword");
993 return modifiers;
995 return modifiers | mod;
998 struct bwriter_shader *parse_hlsl(enum shader_type type, DWORD major, DWORD minor,
999 const char *entrypoint, char **messages)
1001 struct hlsl_ir_function_decl *function;
1002 struct hlsl_scope *scope, *next_scope;
1003 struct hlsl_type *hlsl_type, *next_type;
1004 struct hlsl_ir_var *var, *next_var;
1005 unsigned int i;
1007 hlsl_ctx.status = PARSE_SUCCESS;
1008 hlsl_ctx.messages.size = hlsl_ctx.messages.capacity = 0;
1009 hlsl_ctx.line_no = hlsl_ctx.column = 1;
1010 hlsl_ctx.source_file = d3dcompiler_strdup("");
1011 hlsl_ctx.source_files = d3dcompiler_alloc(sizeof(*hlsl_ctx.source_files));
1012 if (hlsl_ctx.source_files)
1013 hlsl_ctx.source_files[0] = hlsl_ctx.source_file;
1014 hlsl_ctx.source_files_count = 1;
1015 hlsl_ctx.cur_scope = NULL;
1016 hlsl_ctx.matrix_majority = HLSL_COLUMN_MAJOR;
1017 list_init(&hlsl_ctx.scopes);
1018 list_init(&hlsl_ctx.types);
1019 list_init(&hlsl_ctx.functions);
1021 push_scope(&hlsl_ctx);
1022 hlsl_ctx.globals = hlsl_ctx.cur_scope;
1024 hlsl_parse();
1026 if (TRACE_ON(hlsl_parser))
1028 struct hlsl_ir_function_decl *func;
1030 TRACE("IR dump.\n");
1031 LIST_FOR_EACH_ENTRY(func, &hlsl_ctx.functions, struct hlsl_ir_function_decl, node.entry)
1033 if (func->body)
1034 debug_dump_ir_function(func);
1038 TRACE("Compilation status = %d\n", hlsl_ctx.status);
1039 if (messages)
1041 if (hlsl_ctx.messages.size)
1042 *messages = hlsl_ctx.messages.string;
1043 else
1044 *messages = NULL;
1046 else
1048 if (hlsl_ctx.messages.capacity)
1049 d3dcompiler_free(hlsl_ctx.messages.string);
1052 for (i = 0; i < hlsl_ctx.source_files_count; ++i)
1053 d3dcompiler_free((void *)hlsl_ctx.source_files[i]);
1054 d3dcompiler_free(hlsl_ctx.source_files);
1056 TRACE("Freeing functions IR.\n");
1057 LIST_FOR_EACH_ENTRY(function, &hlsl_ctx.functions, struct hlsl_ir_function_decl, node.entry)
1058 free_function(function);
1060 TRACE("Freeing variables.\n");
1061 LIST_FOR_EACH_ENTRY_SAFE(scope, next_scope, &hlsl_ctx.scopes, struct hlsl_scope, entry)
1063 LIST_FOR_EACH_ENTRY_SAFE(var, next_var, &scope->vars, struct hlsl_ir_var, scope_entry)
1065 free_declaration(var);
1067 d3dcompiler_free(scope);
1070 TRACE("Freeing types.\n");
1071 LIST_FOR_EACH_ENTRY_SAFE(hlsl_type, next_type, &hlsl_ctx.types, struct hlsl_type, entry)
1073 free_hlsl_type(hlsl_type);
1076 return NULL;