Assorted spelling fixes.
[wine/multimedia.git] / dlls / d3dcompiler_43 / hlsl.y
blobf37e07eef09431947c47b3fab0fe520c6bbe27d5
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 static const char * const 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 BOOL check_type_modifiers(DWORD modifiers, struct source_location *loc)
180 if (modifiers & ~HLSL_TYPE_MODIFIERS_MASK)
182 hlsl_report_message(loc->file, loc->line, loc->col, HLSL_LEVEL_ERROR,
183 "modifier not allowed on typedefs");
184 return FALSE;
186 return TRUE;
189 static BOOL add_type_to_scope(struct hlsl_scope *scope, struct hlsl_type *def)
191 if (get_type(scope, def->name, FALSE))
192 return FALSE;
194 wine_rb_put(&scope->types, def->name, &def->scope_entry);
195 return TRUE;
198 static void declare_predefined_types(struct hlsl_scope *scope)
200 struct hlsl_type *type;
201 unsigned int x, y, bt;
202 static const char * const names[] =
204 "float",
205 "half",
206 "double",
207 "int",
208 "uint",
209 "bool",
211 char name[10];
213 for (bt = 0; bt <= HLSL_TYPE_LAST_SCALAR; ++bt)
215 for (y = 1; y <= 4; ++y)
217 for (x = 1; x <= 4; ++x)
219 sprintf(name, "%s%ux%u", names[bt], x, y);
220 type = new_hlsl_type(d3dcompiler_strdup(name), HLSL_CLASS_MATRIX, bt, x, y);
221 add_type_to_scope(scope, type);
223 if (y == 1)
225 sprintf(name, "%s%u", names[bt], x);
226 type = new_hlsl_type(d3dcompiler_strdup(name), HLSL_CLASS_VECTOR, bt, x, y);
227 add_type_to_scope(scope, type);
229 if (x == 1)
231 sprintf(name, "%s", names[bt]);
232 type = new_hlsl_type(d3dcompiler_strdup(name), HLSL_CLASS_SCALAR, bt, x, y);
233 add_type_to_scope(scope, type);
240 /* DX8 effects predefined types */
241 type = new_hlsl_type(d3dcompiler_strdup("DWORD"), HLSL_CLASS_SCALAR, HLSL_TYPE_INT, 1, 1);
242 add_type_to_scope(scope, type);
243 type = new_hlsl_type(d3dcompiler_strdup("FLOAT"), HLSL_CLASS_SCALAR, HLSL_TYPE_FLOAT, 1, 1);
244 add_type_to_scope(scope, type);
245 type = new_hlsl_type(d3dcompiler_strdup("VECTOR"), HLSL_CLASS_VECTOR, HLSL_TYPE_FLOAT, 4, 1);
246 add_type_to_scope(scope, type);
247 type = new_hlsl_type(d3dcompiler_strdup("MATRIX"), HLSL_CLASS_MATRIX, HLSL_TYPE_FLOAT, 4, 4);
248 add_type_to_scope(scope, type);
249 type = new_hlsl_type(d3dcompiler_strdup("STRING"), HLSL_CLASS_OBJECT, HLSL_TYPE_STRING, 1, 1);
250 add_type_to_scope(scope, type);
251 type = new_hlsl_type(d3dcompiler_strdup("TEXTURE"), HLSL_CLASS_OBJECT, HLSL_TYPE_TEXTURE, 1, 1);
252 add_type_to_scope(scope, type);
253 type = new_hlsl_type(d3dcompiler_strdup("PIXELSHADER"), HLSL_CLASS_OBJECT, HLSL_TYPE_PIXELSHADER, 1, 1);
254 add_type_to_scope(scope, type);
255 type = new_hlsl_type(d3dcompiler_strdup("VERTEXSHADER"), HLSL_CLASS_OBJECT, HLSL_TYPE_VERTEXSHADER, 1, 1);
256 add_type_to_scope(scope, type);
259 static struct hlsl_ir_if *loop_condition(struct list *cond_list)
261 struct hlsl_ir_if *out_cond;
262 struct hlsl_ir_expr *not_cond;
263 struct hlsl_ir_node *cond, *operands[3];
264 struct hlsl_ir_jump *jump;
265 unsigned int count = list_count(cond_list);
267 if (!count)
268 return NULL;
269 if (count != 1)
270 ERR("Got multiple expressions in a for condition.\n");
272 cond = LIST_ENTRY(list_head(cond_list), struct hlsl_ir_node, entry);
273 out_cond = d3dcompiler_alloc(sizeof(*out_cond));
274 if (!out_cond)
276 ERR("Out of memory.\n");
277 return NULL;
279 out_cond->node.type = HLSL_IR_IF;
280 operands[0] = cond;
281 operands[1] = operands[2] = NULL;
282 not_cond = new_expr(HLSL_IR_UNOP_LOGIC_NOT, operands, &cond->loc);
283 if (!not_cond)
285 ERR("Out of memory.\n");
286 d3dcompiler_free(out_cond);
287 return NULL;
289 out_cond->condition = &not_cond->node;
290 jump = d3dcompiler_alloc(sizeof(*jump));
291 if (!jump)
293 ERR("Out of memory.\n");
294 d3dcompiler_free(out_cond);
295 d3dcompiler_free(not_cond);
296 return NULL;
298 jump->node.type = HLSL_IR_JUMP;
299 jump->type = HLSL_IR_JUMP_BREAK;
300 out_cond->then_instrs = d3dcompiler_alloc(sizeof(*out_cond->then_instrs));
301 if (!out_cond->then_instrs)
303 ERR("Out of memory.\n");
304 d3dcompiler_free(out_cond);
305 d3dcompiler_free(not_cond);
306 d3dcompiler_free(jump);
307 return NULL;
309 list_init(out_cond->then_instrs);
310 list_add_head(out_cond->then_instrs, &jump->node.entry);
312 return out_cond;
315 enum loop_type
317 LOOP_FOR,
318 LOOP_WHILE,
319 LOOP_DO_WHILE
322 static struct list *create_loop(enum loop_type type, struct list *init, struct list *cond,
323 struct hlsl_ir_node *iter, struct list *body, struct source_location *loc)
325 struct list *list = NULL;
326 struct hlsl_ir_loop *loop = NULL;
327 struct hlsl_ir_if *cond_jump = NULL;
329 list = d3dcompiler_alloc(sizeof(*list));
330 if (!list)
331 goto oom;
332 list_init(list);
334 if (init)
335 list_move_head(list, init);
337 loop = d3dcompiler_alloc(sizeof(*loop));
338 if (!loop)
339 goto oom;
340 loop->node.type = HLSL_IR_LOOP;
341 loop->node.loc = *loc;
342 list_add_tail(list, &loop->node.entry);
343 loop->body = d3dcompiler_alloc(sizeof(*loop->body));
344 if (!loop->body)
345 goto oom;
346 list_init(loop->body);
348 cond_jump = loop_condition(cond);
349 if (!cond_jump)
350 goto oom;
352 if (type != LOOP_DO_WHILE)
353 list_add_tail(loop->body, &cond_jump->node.entry);
355 list_move_tail(loop->body, body);
357 if (iter)
358 list_add_tail(loop->body, &iter->entry);
360 if (type == LOOP_DO_WHILE)
361 list_add_tail(loop->body, &cond_jump->node.entry);
363 d3dcompiler_free(init);
364 d3dcompiler_free(cond);
365 d3dcompiler_free(body);
366 return list;
368 oom:
369 ERR("Out of memory.\n");
370 if (loop)
371 d3dcompiler_free(loop->body);
372 d3dcompiler_free(loop);
373 d3dcompiler_free(cond_jump);
374 d3dcompiler_free(list);
375 free_instr_list(init);
376 free_instr_list(cond);
377 free_instr(iter);
378 free_instr_list(body);
379 return NULL;
382 static unsigned int initializer_size(struct list *initializer)
384 unsigned int count = 0;
385 struct hlsl_ir_node *node;
387 LIST_FOR_EACH_ENTRY(node, initializer, struct hlsl_ir_node, entry)
389 count += components_count_type(node->data_type);
391 TRACE("Initializer size = %u\n", count);
392 return count;
395 static unsigned int components_count_expr_list(struct list *list)
397 struct hlsl_ir_node *node;
398 unsigned int count = 0;
400 LIST_FOR_EACH_ENTRY(node, list, struct hlsl_ir_node, entry)
402 count += components_count_type(node->data_type);
404 return count;
407 static struct hlsl_ir_swizzle *new_swizzle(DWORD s, unsigned int components,
408 struct hlsl_ir_node *val, struct source_location *loc)
410 struct hlsl_ir_swizzle *swizzle = d3dcompiler_alloc(sizeof(*swizzle));
412 if (!swizzle)
413 return NULL;
414 swizzle->node.type = HLSL_IR_SWIZZLE;
415 swizzle->node.loc = *loc;
416 swizzle->node.data_type = new_hlsl_type(NULL, HLSL_CLASS_VECTOR, val->data_type->base_type, components, 1);
417 swizzle->val = val;
418 swizzle->swizzle = s;
419 return swizzle;
422 static struct hlsl_ir_swizzle *get_swizzle(struct hlsl_ir_node *value, const char *swizzle,
423 struct source_location *loc)
425 unsigned int len = strlen(swizzle), component = 0;
426 unsigned int i, set, swiz = 0;
427 BOOL valid;
429 if (value->data_type->type == HLSL_CLASS_MATRIX)
431 /* Matrix swizzle */
432 BOOL m_swizzle;
433 unsigned int inc, x, y;
435 if (len < 3 || swizzle[0] != '_')
436 return NULL;
437 m_swizzle = swizzle[1] == 'm';
438 inc = m_swizzle ? 4 : 3;
440 if (len % inc || len > inc * 4)
441 return NULL;
443 for (i = 0; i < len; i += inc)
445 if (swizzle[i] != '_')
446 return NULL;
447 if (m_swizzle)
449 if (swizzle[i + 1] != 'm')
450 return NULL;
451 x = swizzle[i + 2] - '0';
452 y = swizzle[i + 3] - '0';
454 else
456 x = swizzle[i + 1] - '1';
457 y = swizzle[i + 2] - '1';
460 if (x >= value->data_type->dimx || y >= value->data_type->dimy)
461 return NULL;
462 swiz |= (y << 4 | x) << component * 8;
463 component++;
465 return new_swizzle(swiz, component, value, loc);
468 /* Vector swizzle */
469 if (len > 4)
470 return NULL;
472 for (set = 0; set < 2; ++set)
474 valid = TRUE;
475 component = 0;
476 for (i = 0; i < len; ++i)
478 char c[2][4] = {{'x', 'y', 'z', 'w'}, {'r', 'g', 'b', 'a'}};
479 unsigned int s = 0;
481 for (s = 0; s < 4; ++s)
483 if (swizzle[i] == c[set][s])
484 break;
486 if (s == 4)
488 valid = FALSE;
489 break;
492 if (s >= value->data_type->dimx)
493 return NULL;
494 swiz |= s << component * 2;
495 component++;
497 if (valid)
498 return new_swizzle(swiz, component, value, loc);
501 return NULL;
504 static void struct_var_initializer(struct list *list, struct hlsl_ir_var *var, struct list *initializer)
506 struct hlsl_type *type = var->node.data_type;
507 struct hlsl_ir_node *node;
508 struct hlsl_struct_field *field;
509 struct list *cur_node;
510 struct hlsl_ir_node *assignment;
511 struct hlsl_ir_deref *deref;
513 if (initializer_size(initializer) != components_count_type(type))
515 hlsl_report_message(var->node.loc.file, var->node.loc.line, var->node.loc.col, HLSL_LEVEL_ERROR,
516 "structure initializer mismatch");
517 free_instr_list(initializer);
518 return;
520 cur_node = list_head(initializer);
521 assert(cur_node);
522 node = LIST_ENTRY(cur_node, struct hlsl_ir_node, entry);
523 LIST_FOR_EACH_ENTRY(field, type->e.elements, struct hlsl_struct_field, entry)
525 if (!cur_node)
527 d3dcompiler_free(initializer);
528 return;
530 if (components_count_type(field->type) == components_count_type(node->data_type))
532 deref = new_record_deref(&var->node, field);
533 if (!deref)
535 ERR("Out of memory.\n");
536 break;
538 deref->node.loc = node->loc;
539 assignment = make_assignment(&deref->node, ASSIGN_OP_ASSIGN, BWRITERSP_WRITEMASK_ALL, node);
540 list_add_tail(list, &assignment->entry);
542 else
543 FIXME("Initializing with \"mismatched\" fields is not supported yet.\n");
544 cur_node = list_next(initializer, cur_node);
545 node = LIST_ENTRY(cur_node, struct hlsl_ir_node, entry);
548 /* Free initializer elements in excess. */
549 while (cur_node)
551 struct list *next = list_next(initializer, cur_node);
552 free_instr(node);
553 cur_node = next;
554 node = LIST_ENTRY(cur_node, struct hlsl_ir_node, entry);
556 d3dcompiler_free(initializer);
559 static struct list *declare_vars(struct hlsl_type *basic_type, DWORD modifiers, struct list *var_list)
561 struct hlsl_type *type;
562 struct parse_variable_def *v, *v_next;
563 struct hlsl_ir_var *var;
564 struct hlsl_ir_node *assignment;
565 BOOL ret, local = TRUE;
566 struct list *statements_list = d3dcompiler_alloc(sizeof(*statements_list));
568 if (!statements_list)
570 ERR("Out of memory.\n");
571 LIST_FOR_EACH_ENTRY_SAFE(v, v_next, var_list, struct parse_variable_def, entry)
572 d3dcompiler_free(v);
573 d3dcompiler_free(var_list);
574 return NULL;
576 list_init(statements_list);
578 if (!var_list)
579 return statements_list;
581 LIST_FOR_EACH_ENTRY_SAFE(v, v_next, var_list, struct parse_variable_def, entry)
583 var = d3dcompiler_alloc(sizeof(*var));
584 if (!var)
586 ERR("Out of memory.\n");
587 d3dcompiler_free(v);
588 continue;
590 var->node.type = HLSL_IR_VAR;
591 if (v->array_size)
592 type = new_array_type(basic_type, v->array_size);
593 else
594 type = basic_type;
595 var->node.data_type = type;
596 var->node.loc = v->loc;
597 var->name = v->name;
598 var->modifiers = modifiers;
599 var->semantic = v->semantic;
600 debug_dump_decl(type, modifiers, v->name, v->loc.line);
602 if (hlsl_ctx.cur_scope == hlsl_ctx.globals)
604 var->modifiers |= HLSL_STORAGE_UNIFORM;
605 local = FALSE;
608 if (var->modifiers & HLSL_MODIFIER_CONST && !(var->modifiers & HLSL_STORAGE_UNIFORM) && !v->initializer)
610 hlsl_report_message(v->loc.file, v->loc.line, v->loc.col,
611 HLSL_LEVEL_ERROR, "const variable without initializer");
612 free_declaration(var);
613 d3dcompiler_free(v);
614 continue;
617 ret = declare_variable(var, local);
618 if (!ret)
620 free_declaration(var);
621 d3dcompiler_free(v);
622 continue;
624 TRACE("Declared variable %s.\n", var->name);
626 if (v->initializer)
628 unsigned int size = initializer_size(v->initializer);
629 struct hlsl_ir_node *node;
631 TRACE("Variable with initializer.\n");
632 if (type->type <= HLSL_CLASS_LAST_NUMERIC
633 && type->dimx * type->dimy != size && size != 1)
635 if (size < type->dimx * type->dimy)
637 hlsl_report_message(v->loc.file, v->loc.line, v->loc.col, HLSL_LEVEL_ERROR,
638 "'%s' initializer does not match", v->name);
639 free_instr_list(v->initializer);
640 d3dcompiler_free(v);
641 continue;
644 if ((type->type == HLSL_CLASS_STRUCT || type->type == HLSL_CLASS_ARRAY)
645 && components_count_type(type) != size)
647 hlsl_report_message(v->loc.file, v->loc.line, v->loc.col, HLSL_LEVEL_ERROR,
648 "'%s' initializer does not match", v->name);
649 free_instr_list(v->initializer);
650 d3dcompiler_free(v);
651 continue;
654 if (type->type == HLSL_CLASS_STRUCT)
656 struct_var_initializer(statements_list, var, v->initializer);
657 d3dcompiler_free(v);
658 continue;
660 if (type->type > HLSL_CLASS_LAST_NUMERIC)
662 FIXME("Initializers for non scalar/struct variables not supported yet.\n");
663 free_instr_list(v->initializer);
664 d3dcompiler_free(v);
665 continue;
667 if (v->array_size > 0)
669 FIXME("Initializing arrays is not supported yet.\n");
670 free_instr_list(v->initializer);
671 d3dcompiler_free(v);
672 continue;
674 if (list_count(v->initializer) > 1)
676 FIXME("Complex initializers are not supported yet.\n");
677 free_instr_list(v->initializer);
678 d3dcompiler_free(v);
679 continue;
681 node = LIST_ENTRY(list_head(v->initializer), struct hlsl_ir_node, entry);
682 assignment = make_assignment(&var->node, ASSIGN_OP_ASSIGN,
683 BWRITERSP_WRITEMASK_ALL, node);
684 list_add_tail(statements_list, &assignment->entry);
685 d3dcompiler_free(v->initializer);
687 d3dcompiler_free(v);
689 d3dcompiler_free(var_list);
690 return statements_list;
693 static BOOL add_struct_field(struct list *fields, struct hlsl_struct_field *field)
695 struct hlsl_struct_field *f;
697 LIST_FOR_EACH_ENTRY(f, fields, struct hlsl_struct_field, entry)
699 if (!strcmp(f->name, field->name))
700 return FALSE;
702 list_add_tail(fields, &field->entry);
703 return TRUE;
706 static struct list *gen_struct_fields(struct hlsl_type *type, DWORD modifiers, struct list *fields)
708 struct parse_variable_def *v, *v_next;
709 struct hlsl_struct_field *field;
710 struct list *list;
712 list = d3dcompiler_alloc(sizeof(*list));
713 if (!list)
715 ERR("Out of memory.\n");
716 return NULL;
718 list_init(list);
719 LIST_FOR_EACH_ENTRY_SAFE(v, v_next, fields, struct parse_variable_def, entry)
721 debug_dump_decl(type, 0, v->name, v->loc.line);
722 field = d3dcompiler_alloc(sizeof(*field));
723 if (!field)
725 ERR("Out of memory.\n");
726 d3dcompiler_free(v);
727 return list;
729 field->type = type;
730 field->name = v->name;
731 field->modifiers = modifiers;
732 field->semantic = v->semantic;
733 if (v->initializer)
735 hlsl_report_message(v->loc.file, v->loc.line, v->loc.col, HLSL_LEVEL_ERROR,
736 "struct field with an initializer.\n");
737 free_instr_list(v->initializer);
739 list_add_tail(list, &field->entry);
740 d3dcompiler_free(v);
742 d3dcompiler_free(fields);
743 return list;
746 static struct hlsl_type *new_struct_type(const char *name, DWORD modifiers, struct list *fields)
748 struct hlsl_type *type = d3dcompiler_alloc(sizeof(*type));
750 if (!type)
752 ERR("Out of memory.\n");
753 return NULL;
755 type->type = HLSL_CLASS_STRUCT;
756 type->name = name;
757 type->dimx = type->dimy = 1;
758 type->modifiers = modifiers;
759 type->e.elements = fields;
761 list_add_tail(&hlsl_ctx.types, &type->entry);
763 return type;
766 static BOOL add_typedef(DWORD modifiers, struct hlsl_type *orig_type, struct list *list,
767 struct source_location *loc)
769 BOOL ret;
770 struct hlsl_type *type;
771 struct parse_variable_def *v, *v_next;
773 if (!check_type_modifiers(modifiers, loc))
775 LIST_FOR_EACH_ENTRY_SAFE(v, v_next, list, struct parse_variable_def, entry)
776 d3dcompiler_free(v);
777 d3dcompiler_free(list);
778 return FALSE;
781 LIST_FOR_EACH_ENTRY_SAFE(v, v_next, list, struct parse_variable_def, entry)
783 if (v->array_size)
784 type = new_array_type(orig_type, v->array_size);
785 else
786 type = clone_hlsl_type(orig_type);
787 if (!type)
789 ERR("Out of memory\n");
790 return FALSE;
792 d3dcompiler_free((void *)type->name);
793 type->name = v->name;
794 type->modifiers |= modifiers;
796 if (type->type != HLSL_CLASS_MATRIX)
797 check_invalid_matrix_modifiers(type->modifiers, &v->loc);
799 ret = add_type_to_scope(hlsl_ctx.cur_scope, type);
800 if (!ret)
802 hlsl_report_message(v->loc.file, v->loc.line, v->loc.col, HLSL_LEVEL_ERROR,
803 "redefinition of custom type '%s'", v->name);
805 d3dcompiler_free(v);
807 d3dcompiler_free(list);
808 return TRUE;
811 static BOOL add_func_parameter(struct list *list, struct parse_parameter *param, const struct source_location *loc)
813 struct hlsl_ir_var *decl = d3dcompiler_alloc(sizeof(*decl));
815 if (!decl)
817 ERR("Out of memory.\n");
818 return FALSE;
820 decl->node.type = HLSL_IR_VAR;
821 decl->node.data_type = param->type;
822 decl->node.loc = *loc;
823 decl->name = param->name;
824 decl->semantic = param->semantic;
825 decl->modifiers = param->modifiers;
827 if (!add_declaration(hlsl_ctx.cur_scope, decl, FALSE))
829 free_declaration(decl);
830 return FALSE;
832 list_add_tail(list, &decl->node.entry);
833 return TRUE;
836 static const struct hlsl_ir_function_decl *get_overloaded_func(struct wine_rb_tree *funcs, char *name,
837 struct list *params, BOOL exact_signature)
839 struct hlsl_ir_function *func;
840 struct wine_rb_entry *entry;
842 entry = wine_rb_get(funcs, name);
843 if (entry)
845 func = WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function, entry);
847 entry = wine_rb_get(&func->overloads, params);
848 if (!entry)
850 if (!exact_signature)
851 FIXME("No exact match, search for a compatible overloaded function (if any).\n");
852 return NULL;
854 return WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function_decl, entry);
856 return NULL;
861 %locations
862 %error-verbose
863 %expect 1
865 %union
867 struct hlsl_type *type;
868 INT intval;
869 FLOAT floatval;
870 BOOL boolval;
871 char *name;
872 DWORD modifiers;
873 struct hlsl_ir_var *var;
874 struct hlsl_ir_node *instr;
875 struct list *list;
876 struct parse_function function;
877 struct parse_parameter parameter;
878 struct parse_variable_def *variable_def;
879 struct parse_if_body if_body;
880 enum parse_unary_op unary_op;
881 enum parse_assign_op assign_op;
884 %token KW_BLENDSTATE
885 %token KW_BREAK
886 %token KW_BUFFER
887 %token KW_CBUFFER
888 %token KW_COLUMN_MAJOR
889 %token KW_COMPILE
890 %token KW_CONST
891 %token KW_CONTINUE
892 %token KW_DEPTHSTENCILSTATE
893 %token KW_DEPTHSTENCILVIEW
894 %token KW_DISCARD
895 %token KW_DO
896 %token KW_DOUBLE
897 %token KW_ELSE
898 %token KW_EXTERN
899 %token KW_FALSE
900 %token KW_FOR
901 %token KW_GEOMETRYSHADER
902 %token KW_GROUPSHARED
903 %token KW_IF
904 %token KW_IN
905 %token KW_INLINE
906 %token KW_INOUT
907 %token KW_MATRIX
908 %token KW_NAMESPACE
909 %token KW_NOINTERPOLATION
910 %token KW_OUT
911 %token KW_PASS
912 %token KW_PIXELSHADER
913 %token KW_PRECISE
914 %token KW_RASTERIZERSTATE
915 %token KW_RENDERTARGETVIEW
916 %token KW_RETURN
917 %token KW_REGISTER
918 %token KW_ROW_MAJOR
919 %token KW_SAMPLER
920 %token KW_SAMPLER1D
921 %token KW_SAMPLER2D
922 %token KW_SAMPLER3D
923 %token KW_SAMPLERCUBE
924 %token KW_SAMPLER_STATE
925 %token KW_SAMPLERCOMPARISONSTATE
926 %token KW_SHARED
927 %token KW_STATEBLOCK
928 %token KW_STATEBLOCK_STATE
929 %token KW_STATIC
930 %token KW_STRING
931 %token KW_STRUCT
932 %token KW_SWITCH
933 %token KW_TBUFFER
934 %token KW_TECHNIQUE
935 %token KW_TECHNIQUE10
936 %token KW_TEXTURE
937 %token KW_TEXTURE1D
938 %token KW_TEXTURE1DARRAY
939 %token KW_TEXTURE2D
940 %token KW_TEXTURE2DARRAY
941 %token KW_TEXTURE2DMS
942 %token KW_TEXTURE2DMSARRAY
943 %token KW_TEXTURE3D
944 %token KW_TEXTURE3DARRAY
945 %token KW_TEXTURECUBE
946 %token KW_TRUE
947 %token KW_TYPEDEF
948 %token KW_UNIFORM
949 %token KW_VECTOR
950 %token KW_VERTEXSHADER
951 %token KW_VOID
952 %token KW_VOLATILE
953 %token KW_WHILE
955 %token OP_INC
956 %token OP_DEC
957 %token OP_AND
958 %token OP_OR
959 %token OP_EQ
960 %token OP_LEFTSHIFT
961 %token OP_LEFTSHIFTASSIGN
962 %token OP_RIGHTSHIFT
963 %token OP_RIGHTSHIFTASSIGN
964 %token OP_ELLIPSIS
965 %token OP_LE
966 %token OP_GE
967 %token OP_NE
968 %token OP_ADDASSIGN
969 %token OP_SUBASSIGN
970 %token OP_MULASSIGN
971 %token OP_DIVASSIGN
972 %token OP_MODASSIGN
973 %token OP_ANDASSIGN
974 %token OP_ORASSIGN
975 %token OP_XORASSIGN
976 %token OP_UNKNOWN1
977 %token OP_UNKNOWN2
978 %token OP_UNKNOWN3
979 %token OP_UNKNOWN4
981 %token <intval> PRE_LINE
983 %token <name> VAR_IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
984 %type <name> any_identifier var_identifier
985 %token <name> STRING
986 %token <floatval> C_FLOAT
987 %token <intval> C_INTEGER
988 %type <boolval> boolean
989 %type <type> base_type
990 %type <type> type
991 %type <list> declaration_statement
992 %type <list> declaration
993 %type <list> struct_declaration
994 %type <type> struct_spec
995 %type <type> named_struct_spec
996 %type <type> unnamed_struct_spec
997 %type <list> type_specs
998 %type <variable_def> type_spec
999 %type <list> complex_initializer
1000 %type <list> initializer_expr_list
1001 %type <instr> initializer_expr
1002 %type <modifiers> var_modifiers
1003 %type <list> field
1004 %type <list> parameters
1005 %type <list> param_list
1006 %type <instr> expr
1007 %type <var> variable
1008 %type <intval> array
1009 %type <list> statement
1010 %type <list> statement_list
1011 %type <list> compound_statement
1012 %type <list> jump_statement
1013 %type <list> selection_statement
1014 %type <list> loop_statement
1015 %type <function> func_declaration
1016 %type <function> func_prototype
1017 %type <list> fields_list
1018 %type <parameter> parameter
1019 %type <name> semantic
1020 %type <variable_def> variable_def
1021 %type <list> variables_def
1022 %type <list> variables_def_optional
1023 %type <if_body> if_body
1024 %type <instr> primary_expr
1025 %type <instr> postfix_expr
1026 %type <instr> unary_expr
1027 %type <instr> mul_expr
1028 %type <instr> add_expr
1029 %type <instr> shift_expr
1030 %type <instr> relational_expr
1031 %type <instr> equality_expr
1032 %type <instr> bitand_expr
1033 %type <instr> bitxor_expr
1034 %type <instr> bitor_expr
1035 %type <instr> logicand_expr
1036 %type <instr> logicor_expr
1037 %type <instr> conditional_expr
1038 %type <instr> assignment_expr
1039 %type <list> expr_statement
1040 %type <unary_op> unary_op
1041 %type <assign_op> assign_op
1042 %type <modifiers> input_mods
1043 %type <modifiers> input_mod
1046 hlsl_prog: /* empty */
1049 | hlsl_prog func_declaration
1051 const struct hlsl_ir_function_decl *decl;
1053 decl = get_overloaded_func(&hlsl_ctx.functions, $2.name, $2.decl->parameters, TRUE);
1054 if (decl && !decl->func->intrinsic)
1056 if (decl->body && $2.decl->body)
1058 hlsl_report_message($2.decl->node.loc.file, $2.decl->node.loc.line,
1059 $2.decl->node.loc.col, HLSL_LEVEL_ERROR,
1060 "redefinition of function %s", debugstr_a($2.name));
1061 return 1;
1063 else if (!compare_hlsl_types(decl->node.data_type, $2.decl->node.data_type))
1065 hlsl_report_message($2.decl->node.loc.file, $2.decl->node.loc.line,
1066 $2.decl->node.loc.col, HLSL_LEVEL_ERROR,
1067 "redefining function %s with a different return type",
1068 debugstr_a($2.name));
1069 hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_NOTE,
1070 "%s previously declared here",
1071 debugstr_a($2.name));
1072 return 1;
1076 if ($2.decl->node.data_type->base_type == HLSL_TYPE_VOID && $2.decl->semantic)
1078 hlsl_report_message($2.decl->node.loc.file, $2.decl->node.loc.line,
1079 $2.decl->node.loc.col, HLSL_LEVEL_ERROR,
1080 "void function with a semantic");
1083 TRACE("Adding function '%s' to the function list.\n", $2.name);
1084 add_function_decl(&hlsl_ctx.functions, $2.name, $2.decl, FALSE);
1086 | hlsl_prog declaration_statement
1088 TRACE("Declaration statement parsed.\n");
1090 | hlsl_prog preproc_directive
1093 | hlsl_prog ';'
1095 TRACE("Skipping stray semicolon.\n");
1098 preproc_directive: PRE_LINE STRING
1100 TRACE("Updating line information to file %s, line %u\n", debugstr_a($2), $1);
1101 hlsl_ctx.line_no = $1;
1102 if (strcmp($2, hlsl_ctx.source_file))
1104 const char **new_array;
1106 hlsl_ctx.source_file = $2;
1107 new_array = d3dcompiler_realloc(hlsl_ctx.source_files,
1108 sizeof(*hlsl_ctx.source_files) * hlsl_ctx.source_files_count + 1);
1109 if (new_array)
1111 hlsl_ctx.source_files = new_array;
1112 hlsl_ctx.source_files[hlsl_ctx.source_files_count++] = $2;
1117 struct_declaration: struct_spec variables_def_optional ';'
1119 struct source_location loc;
1121 set_location(&loc, &@3);
1122 if (!$2)
1124 if (!$1->name)
1126 hlsl_report_message(loc.file, loc.line, loc.col,
1127 HLSL_LEVEL_ERROR, "anonymous struct declaration with no variables");
1129 check_type_modifiers($1->modifiers, &loc);
1131 $$ = declare_vars($1, 0, $2);
1134 struct_spec: named_struct_spec
1135 | unnamed_struct_spec
1137 named_struct_spec: var_modifiers KW_STRUCT any_identifier '{' fields_list '}'
1139 BOOL ret;
1140 struct source_location loc;
1142 TRACE("Structure %s declaration.\n", debugstr_a($3));
1143 set_location(&loc, &@1);
1144 check_invalid_matrix_modifiers($1, &loc);
1145 $$ = new_struct_type($3, $1, $5);
1147 if (get_variable(hlsl_ctx.cur_scope, $3))
1149 hlsl_report_message(hlsl_ctx.source_file, @3.first_line, @3.first_column,
1150 HLSL_LEVEL_ERROR, "redefinition of '%s'", $3);
1151 return 1;
1154 ret = add_type_to_scope(hlsl_ctx.cur_scope, $$);
1155 if (!ret)
1157 hlsl_report_message(hlsl_ctx.source_file, @3.first_line, @3.first_column,
1158 HLSL_LEVEL_ERROR, "redefinition of struct '%s'", $3);
1159 return 1;
1163 unnamed_struct_spec: var_modifiers KW_STRUCT '{' fields_list '}'
1165 struct source_location loc;
1167 TRACE("Anonymous structure declaration.\n");
1168 set_location(&loc, &@1);
1169 check_invalid_matrix_modifiers($1, &loc);
1170 $$ = new_struct_type(NULL, $1, $4);
1173 any_identifier: VAR_IDENTIFIER
1174 | TYPE_IDENTIFIER
1175 | NEW_IDENTIFIER
1177 fields_list: /* Empty */
1179 $$ = d3dcompiler_alloc(sizeof(*$$));
1180 list_init($$);
1182 | fields_list field
1184 BOOL ret;
1185 struct hlsl_struct_field *field, *next;
1187 $$ = $1;
1188 LIST_FOR_EACH_ENTRY_SAFE(field, next, $2, struct hlsl_struct_field, entry)
1190 ret = add_struct_field($$, field);
1191 if (ret == FALSE)
1193 hlsl_report_message(hlsl_ctx.source_file, @2.first_line, @2.first_column,
1194 HLSL_LEVEL_ERROR, "redefinition of '%s'", field->name);
1195 d3dcompiler_free(field);
1198 d3dcompiler_free($2);
1201 field: var_modifiers type variables_def ';'
1203 $$ = gen_struct_fields($2, $1, $3);
1205 | unnamed_struct_spec variables_def ';'
1207 $$ = gen_struct_fields($1, 0, $2);
1210 func_declaration: func_prototype compound_statement
1212 TRACE("Function %s parsed.\n", $1.name);
1213 $$ = $1;
1214 $$.decl->body = $2;
1215 pop_scope(&hlsl_ctx);
1217 | func_prototype ';'
1219 TRACE("Function prototype for %s.\n", $1.name);
1220 $$ = $1;
1221 pop_scope(&hlsl_ctx);
1224 func_prototype: var_modifiers type var_identifier '(' parameters ')' semantic
1226 if (get_variable(hlsl_ctx.globals, $3))
1228 hlsl_report_message(hlsl_ctx.source_file, @3.first_line, @3.first_column,
1229 HLSL_LEVEL_ERROR, "redefinition of '%s'\n", $3);
1230 return 1;
1232 if ($2->base_type == HLSL_TYPE_VOID && $7)
1234 hlsl_report_message(hlsl_ctx.source_file, @7.first_line, @7.first_column,
1235 HLSL_LEVEL_ERROR, "void function with a semantic");
1238 $$.decl = new_func_decl($2, $5);
1239 if (!$$.decl)
1241 ERR("Out of memory.\n");
1242 return -1;
1244 $$.name = $3;
1245 $$.decl->semantic = $7;
1246 set_location(&$$.decl->node.loc, &@3);
1249 compound_statement: '{' '}'
1251 $$ = d3dcompiler_alloc(sizeof(*$$));
1252 list_init($$);
1254 | '{' scope_start statement_list '}'
1256 pop_scope(&hlsl_ctx);
1257 $$ = $3;
1260 scope_start: /* Empty */
1262 push_scope(&hlsl_ctx);
1265 var_identifier: VAR_IDENTIFIER
1266 | NEW_IDENTIFIER
1268 semantic: /* Empty */
1270 $$ = NULL;
1272 | ':' any_identifier
1274 $$ = $2;
1277 parameters: scope_start
1279 $$ = d3dcompiler_alloc(sizeof(*$$));
1280 list_init($$);
1282 | scope_start param_list
1284 $$ = $2;
1287 param_list: parameter
1289 struct source_location loc;
1291 $$ = d3dcompiler_alloc(sizeof(*$$));
1292 list_init($$);
1293 set_location(&loc, &@1);
1294 if (!add_func_parameter($$, &$1, &loc))
1296 ERR("Error adding function parameter %s.\n", $1.name);
1297 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
1298 return -1;
1301 | param_list ',' parameter
1303 struct source_location loc;
1305 $$ = $1;
1306 set_location(&loc, &@3);
1307 if (!add_func_parameter($$, &$3, &loc))
1309 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
1310 "duplicate parameter %s", $3.name);
1311 return 1;
1315 parameter: input_mods var_modifiers type any_identifier semantic
1317 $$.modifiers = $1 ? $1 : HLSL_MODIFIER_IN;
1318 $$.modifiers |= $2;
1319 $$.type = $3;
1320 $$.name = $4;
1321 $$.semantic = $5;
1324 input_mods: /* Empty */
1326 $$ = 0;
1328 | input_mods input_mod
1330 if ($1 & $2)
1332 hlsl_report_message(hlsl_ctx.source_file, @2.first_line, @2.first_column,
1333 HLSL_LEVEL_ERROR, "duplicate input-output modifiers");
1334 return 1;
1336 $$ = $1 | $2;
1339 input_mod: KW_IN
1341 $$ = HLSL_MODIFIER_IN;
1343 | KW_OUT
1345 $$ = HLSL_MODIFIER_OUT;
1347 | KW_INOUT
1349 $$ = HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT;
1352 type: base_type
1354 $$ = $1;
1356 | KW_VECTOR '<' base_type ',' C_INTEGER '>'
1358 if ($3->type != HLSL_CLASS_SCALAR)
1360 hlsl_message("Line %u: vectors of non-scalar types are not allowed.\n",
1361 hlsl_ctx.line_no);
1362 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
1363 return 1;
1365 if ($5 < 1 || $5 > 4)
1367 hlsl_message("Line %u: vector size must be between 1 and 4.\n",
1368 hlsl_ctx.line_no);
1369 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
1370 return 1;
1373 $$ = new_hlsl_type(NULL, HLSL_CLASS_VECTOR, $3->base_type, $5, 1);
1375 | KW_MATRIX '<' base_type ',' C_INTEGER ',' C_INTEGER '>'
1377 if ($3->type != HLSL_CLASS_SCALAR)
1379 hlsl_message("Line %u: matrices of non-scalar types are not allowed.\n",
1380 hlsl_ctx.line_no);
1381 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
1382 return 1;
1384 if ($5 < 1 || $5 > 4 || $7 < 1 || $7 > 4)
1386 hlsl_message("Line %u: matrix dimensions must be between 1 and 4.\n",
1387 hlsl_ctx.line_no);
1388 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
1389 return 1;
1392 $$ = new_hlsl_type(NULL, HLSL_CLASS_MATRIX, $3->base_type, $5, $7);
1395 base_type: KW_VOID
1397 $$ = new_hlsl_type(d3dcompiler_strdup("void"), HLSL_CLASS_OBJECT, HLSL_TYPE_VOID, 1, 1);
1399 | KW_SAMPLER
1401 $$ = new_hlsl_type(d3dcompiler_strdup("sampler"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
1402 $$->sampler_dim = HLSL_SAMPLER_DIM_GENERIC;
1404 | KW_SAMPLER1D
1406 $$ = new_hlsl_type(d3dcompiler_strdup("sampler1D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
1407 $$->sampler_dim = HLSL_SAMPLER_DIM_1D;
1409 | KW_SAMPLER2D
1411 $$ = new_hlsl_type(d3dcompiler_strdup("sampler2D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
1412 $$->sampler_dim = HLSL_SAMPLER_DIM_2D;
1414 | KW_SAMPLER3D
1416 $$ = new_hlsl_type(d3dcompiler_strdup("sampler3D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
1417 $$->sampler_dim = HLSL_SAMPLER_DIM_3D;
1419 | KW_SAMPLERCUBE
1421 $$ = new_hlsl_type(d3dcompiler_strdup("samplerCUBE"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
1422 $$->sampler_dim = HLSL_SAMPLER_DIM_CUBE;
1424 | TYPE_IDENTIFIER
1426 struct hlsl_type *type;
1428 type = get_type(hlsl_ctx.cur_scope, $1, TRUE);
1429 $$ = type;
1430 d3dcompiler_free($1);
1432 | KW_STRUCT TYPE_IDENTIFIER
1434 struct hlsl_type *type;
1436 type = get_type(hlsl_ctx.cur_scope, $2, TRUE);
1437 if (type->type != HLSL_CLASS_STRUCT)
1439 hlsl_message("Line %u: redefining %s as a structure.\n",
1440 hlsl_ctx.line_no, $2);
1441 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
1443 else
1445 $$ = type;
1447 d3dcompiler_free($2);
1450 declaration_statement: declaration
1451 | struct_declaration
1452 | typedef
1454 $$ = d3dcompiler_alloc(sizeof(*$$));
1455 if (!$$)
1457 ERR("Out of memory\n");
1458 return -1;
1460 list_init($$);
1463 typedef: KW_TYPEDEF var_modifiers type type_specs ';'
1465 struct source_location loc;
1467 set_location(&loc, &@1);
1468 if (!add_typedef($2, $3, $4, &loc))
1469 return 1;
1471 | KW_TYPEDEF struct_spec type_specs ';'
1473 struct source_location loc;
1475 set_location(&loc, &@1);
1476 if (!add_typedef(0, $2, $3, &loc))
1477 return 1;
1480 type_specs: type_spec
1482 $$ = d3dcompiler_alloc(sizeof(*$$));
1483 list_init($$);
1484 list_add_head($$, &$1->entry);
1486 | type_specs ',' type_spec
1488 $$ = $1;
1489 list_add_tail($$, &$3->entry);
1492 type_spec: any_identifier array
1494 $$ = d3dcompiler_alloc(sizeof(*$$));
1495 set_location(&$$->loc, &@1);
1496 $$->name = $1;
1497 $$->array_size = $2;
1500 declaration: var_modifiers type variables_def ';'
1502 $$ = declare_vars($2, $1, $3);
1505 variables_def_optional: /* Empty */
1507 $$ = NULL;
1509 | variables_def
1511 $$ = $1;
1514 variables_def: variable_def
1516 $$ = d3dcompiler_alloc(sizeof(*$$));
1517 list_init($$);
1518 list_add_head($$, &$1->entry);
1520 | variables_def ',' variable_def
1522 $$ = $1;
1523 list_add_tail($$, &$3->entry);
1526 variable_def: any_identifier array semantic
1528 $$ = d3dcompiler_alloc(sizeof(*$$));
1529 set_location(&$$->loc, &@1);
1530 $$->name = $1;
1531 $$->array_size = $2;
1532 $$->semantic = $3;
1534 | any_identifier array semantic '=' complex_initializer
1536 TRACE("Declaration with initializer.\n");
1537 $$ = d3dcompiler_alloc(sizeof(*$$));
1538 set_location(&$$->loc, &@1);
1539 $$->name = $1;
1540 $$->array_size = $2;
1541 $$->semantic = $3;
1542 $$->initializer = $5;
1545 array: /* Empty */
1547 $$ = 0;
1549 | '[' expr ']'
1551 FIXME("Array.\n");
1552 $$ = 0;
1553 free_instr($2);
1556 var_modifiers: /* Empty */
1558 $$ = 0;
1560 | KW_EXTERN var_modifiers
1562 $$ = add_modifier($2, HLSL_STORAGE_EXTERN, &@1);
1564 | KW_NOINTERPOLATION var_modifiers
1566 $$ = add_modifier($2, HLSL_STORAGE_NOINTERPOLATION, &@1);
1568 | KW_PRECISE var_modifiers
1570 $$ = add_modifier($2, HLSL_MODIFIER_PRECISE, &@1);
1572 | KW_SHARED var_modifiers
1574 $$ = add_modifier($2, HLSL_STORAGE_SHARED, &@1);
1576 | KW_GROUPSHARED var_modifiers
1578 $$ = add_modifier($2, HLSL_STORAGE_GROUPSHARED, &@1);
1580 | KW_STATIC var_modifiers
1582 $$ = add_modifier($2, HLSL_STORAGE_STATIC, &@1);
1584 | KW_UNIFORM var_modifiers
1586 $$ = add_modifier($2, HLSL_STORAGE_UNIFORM, &@1);
1588 | KW_VOLATILE var_modifiers
1590 $$ = add_modifier($2, HLSL_STORAGE_VOLATILE, &@1);
1592 | KW_CONST var_modifiers
1594 $$ = add_modifier($2, HLSL_MODIFIER_CONST, &@1);
1596 | KW_ROW_MAJOR var_modifiers
1598 $$ = add_modifier($2, HLSL_MODIFIER_ROW_MAJOR, &@1);
1600 | KW_COLUMN_MAJOR var_modifiers
1602 $$ = add_modifier($2, HLSL_MODIFIER_COLUMN_MAJOR, &@1);
1605 complex_initializer: initializer_expr
1607 $$ = d3dcompiler_alloc(sizeof(*$$));
1608 list_init($$);
1609 list_add_head($$, &$1->entry);
1611 | '{' initializer_expr_list '}'
1613 $$ = $2;
1615 | '{' initializer_expr_list ',' '}'
1617 $$ = $2;
1620 initializer_expr: assignment_expr
1622 $$ = $1;
1625 initializer_expr_list: initializer_expr
1627 $$ = d3dcompiler_alloc(sizeof(*$$));
1628 list_init($$);
1629 list_add_head($$, &$1->entry);
1631 | initializer_expr_list ',' initializer_expr
1633 $$ = $1;
1634 list_add_tail($$, &$3->entry);
1637 boolean: KW_TRUE
1639 $$ = TRUE;
1641 | KW_FALSE
1643 $$ = FALSE;
1646 statement_list: statement
1648 $$ = $1;
1650 | statement_list statement
1652 $$ = $1;
1653 list_move_tail($$, $2);
1654 d3dcompiler_free($2);
1657 statement: declaration_statement
1658 | expr_statement
1659 | compound_statement
1660 | jump_statement
1661 | selection_statement
1662 | loop_statement
1664 /* FIXME: add rule for return with no value */
1665 jump_statement: KW_RETURN expr ';'
1667 struct hlsl_ir_jump *jump = d3dcompiler_alloc(sizeof(*jump));
1668 if (!jump)
1670 ERR("Out of memory\n");
1671 return -1;
1673 jump->node.type = HLSL_IR_JUMP;
1674 set_location(&jump->node.loc, &@1);
1675 jump->type = HLSL_IR_JUMP_RETURN;
1676 jump->node.data_type = $2->data_type;
1677 jump->return_value = $2;
1679 FIXME("Check for valued return on void function.\n");
1680 FIXME("Implicit conversion to the return type if needed, "
1681 "error out if conversion not possible.\n");
1683 $$ = d3dcompiler_alloc(sizeof(*$$));
1684 list_init($$);
1685 list_add_tail($$, &jump->node.entry);
1688 selection_statement: KW_IF '(' expr ')' if_body
1690 struct hlsl_ir_if *instr = d3dcompiler_alloc(sizeof(*instr));
1691 if (!instr)
1693 ERR("Out of memory\n");
1694 return -1;
1696 instr->node.type = HLSL_IR_IF;
1697 set_location(&instr->node.loc, &@1);
1698 instr->condition = $3;
1699 instr->then_instrs = $5.then_instrs;
1700 instr->else_instrs = $5.else_instrs;
1701 if ($3->data_type->dimx > 1 || $3->data_type->dimy > 1)
1703 hlsl_report_message(instr->node.loc.file, instr->node.loc.line,
1704 instr->node.loc.col, HLSL_LEVEL_ERROR,
1705 "if condition requires a scalar");
1707 $$ = d3dcompiler_alloc(sizeof(*$$));
1708 list_init($$);
1709 list_add_head($$, &instr->node.entry);
1712 if_body: statement
1714 $$.then_instrs = $1;
1715 $$.else_instrs = NULL;
1717 | statement KW_ELSE statement
1719 $$.then_instrs = $1;
1720 $$.else_instrs = $3;
1723 loop_statement: KW_WHILE '(' expr ')' statement
1725 struct source_location loc;
1726 struct list *cond = d3dcompiler_alloc(sizeof(*cond));
1728 if (!cond)
1730 ERR("Out of memory.\n");
1731 return -1;
1733 list_init(cond);
1734 list_add_head(cond, &$3->entry);
1735 set_location(&loc, &@1);
1736 $$ = create_loop(LOOP_WHILE, NULL, cond, NULL, $5, &loc);
1738 | KW_DO statement KW_WHILE '(' expr ')' ';'
1740 struct source_location loc;
1741 struct list *cond = d3dcompiler_alloc(sizeof(*cond));
1743 if (!cond)
1745 ERR("Out of memory.\n");
1746 return -1;
1748 list_init(cond);
1749 list_add_head(cond, &$5->entry);
1750 set_location(&loc, &@1);
1751 $$ = create_loop(LOOP_DO_WHILE, NULL, cond, NULL, $2, &loc);
1753 | KW_FOR '(' scope_start expr_statement expr_statement expr ')' statement
1755 struct source_location loc;
1757 set_location(&loc, &@1);
1758 $$ = create_loop(LOOP_FOR, $4, $5, $6, $8, &loc);
1759 pop_scope(&hlsl_ctx);
1761 | KW_FOR '(' scope_start declaration expr_statement expr ')' statement
1763 struct source_location loc;
1765 set_location(&loc, &@1);
1766 if (!$4)
1767 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_WARNING,
1768 "no expressions in for loop initializer");
1769 $$ = create_loop(LOOP_FOR, $4, $5, $6, $8, &loc);
1770 pop_scope(&hlsl_ctx);
1773 expr_statement: ';'
1775 $$ = d3dcompiler_alloc(sizeof(*$$));
1776 list_init($$);
1778 | expr ';'
1780 $$ = d3dcompiler_alloc(sizeof(*$$));
1781 list_init($$);
1782 if ($1)
1783 list_add_head($$, &$1->entry);
1786 primary_expr: C_FLOAT
1788 struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
1789 if (!c)
1791 ERR("Out of memory.\n");
1792 return -1;
1794 c->node.type = HLSL_IR_CONSTANT;
1795 set_location(&c->node.loc, &yylloc);
1796 c->node.data_type = new_hlsl_type(d3dcompiler_strdup("float"), HLSL_CLASS_SCALAR, HLSL_TYPE_FLOAT, 1, 1);
1797 c->v.value.f[0] = $1;
1798 $$ = &c->node;
1800 | C_INTEGER
1802 struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
1803 if (!c)
1805 ERR("Out of memory.\n");
1806 return -1;
1808 c->node.type = HLSL_IR_CONSTANT;
1809 set_location(&c->node.loc, &yylloc);
1810 c->node.data_type = new_hlsl_type(d3dcompiler_strdup("int"), HLSL_CLASS_SCALAR, HLSL_TYPE_INT, 1, 1);
1811 c->v.value.i[0] = $1;
1812 $$ = &c->node;
1814 | boolean
1816 struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
1817 if (!c)
1819 ERR("Out of memory.\n");
1820 return -1;
1822 c->node.type = HLSL_IR_CONSTANT;
1823 set_location(&c->node.loc, &yylloc);
1824 c->node.data_type = new_hlsl_type(d3dcompiler_strdup("bool"), HLSL_CLASS_SCALAR, HLSL_TYPE_BOOL, 1, 1);
1825 c->v.value.b[0] = $1;
1826 $$ = &c->node;
1828 | variable
1830 struct hlsl_ir_deref *deref = new_var_deref($1);
1831 if (deref)
1833 $$ = &deref->node;
1834 set_location(&$$->loc, &@1);
1836 else
1837 $$ = NULL;
1839 | '(' expr ')'
1841 $$ = $2;
1844 variable: VAR_IDENTIFIER
1846 struct hlsl_ir_var *var;
1847 var = get_variable(hlsl_ctx.cur_scope, $1);
1848 if (!var)
1850 hlsl_message("Line %d: variable '%s' not declared\n",
1851 hlsl_ctx.line_no, $1);
1852 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
1853 return 1;
1855 $$ = var;
1858 postfix_expr: primary_expr
1860 $$ = $1;
1862 | postfix_expr OP_INC
1864 struct hlsl_ir_node *operands[3];
1865 struct source_location loc;
1867 set_location(&loc, &@2);
1868 if ($1->data_type->modifiers & HLSL_MODIFIER_CONST)
1870 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
1871 "modifying a const expression");
1872 return 1;
1874 operands[0] = $1;
1875 operands[1] = operands[2] = NULL;
1876 $$ = &new_expr(HLSL_IR_UNOP_POSTINC, operands, &loc)->node;
1877 /* Post increment/decrement expressions are considered const */
1878 $$->data_type = clone_hlsl_type($$->data_type);
1879 $$->data_type->modifiers |= HLSL_MODIFIER_CONST;
1881 | postfix_expr OP_DEC
1883 struct hlsl_ir_node *operands[3];
1884 struct source_location loc;
1886 set_location(&loc, &@2);
1887 if ($1->data_type->modifiers & HLSL_MODIFIER_CONST)
1889 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
1890 "modifying a const expression");
1891 return 1;
1893 operands[0] = $1;
1894 operands[1] = operands[2] = NULL;
1895 $$ = &new_expr(HLSL_IR_UNOP_POSTDEC, operands, &loc)->node;
1896 /* Post increment/decrement expressions are considered const */
1897 $$->data_type = clone_hlsl_type($$->data_type);
1898 $$->data_type->modifiers |= HLSL_MODIFIER_CONST;
1900 | postfix_expr '.' any_identifier
1902 struct source_location loc;
1904 set_location(&loc, &@2);
1905 if ($1->data_type->type == HLSL_CLASS_STRUCT)
1907 struct hlsl_type *type = $1->data_type;
1908 struct hlsl_struct_field *field;
1910 $$ = NULL;
1911 LIST_FOR_EACH_ENTRY(field, type->e.elements, struct hlsl_struct_field, entry)
1913 if (!strcmp($3, field->name))
1915 struct hlsl_ir_deref *deref = new_record_deref($1, field);
1917 if (!deref)
1919 ERR("Out of memory\n");
1920 return -1;
1922 deref->node.loc = loc;
1923 $$ = &deref->node;
1924 break;
1927 if (!$$)
1929 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
1930 "invalid subscript %s", debugstr_a($3));
1931 return 1;
1934 else if ($1->data_type->type <= HLSL_CLASS_LAST_NUMERIC)
1936 struct hlsl_ir_swizzle *swizzle;
1938 swizzle = get_swizzle($1, $3, &loc);
1939 if (!swizzle)
1941 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
1942 "invalid swizzle %s", debugstr_a($3));
1943 return 1;
1945 $$ = &swizzle->node;
1947 else
1949 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
1950 "invalid subscript %s", debugstr_a($3));
1951 return 1;
1954 | postfix_expr '[' expr ']'
1956 /* This may be an array dereference or a vector/matrix
1957 * subcomponent access.
1958 * We store it as an array dereference in any case. */
1959 struct hlsl_ir_deref *deref = d3dcompiler_alloc(sizeof(*deref));
1960 struct hlsl_type *expr_type = $1->data_type;
1961 struct source_location loc;
1963 TRACE("Array dereference from type %s\n", debug_hlsl_type(expr_type));
1964 if (!deref)
1966 ERR("Out of memory\n");
1967 return -1;
1969 deref->node.type = HLSL_IR_DEREF;
1970 set_location(&loc, &@2);
1971 deref->node.loc = loc;
1972 if (expr_type->type == HLSL_CLASS_ARRAY)
1974 deref->node.data_type = expr_type->e.array.type;
1976 else if (expr_type->type == HLSL_CLASS_MATRIX)
1978 deref->node.data_type = new_hlsl_type(NULL, HLSL_CLASS_VECTOR, expr_type->base_type, expr_type->dimx, 1);
1980 else if (expr_type->type == HLSL_CLASS_VECTOR)
1982 deref->node.data_type = new_hlsl_type(NULL, HLSL_CLASS_SCALAR, expr_type->base_type, 1, 1);
1984 else
1986 if (expr_type->type == HLSL_CLASS_SCALAR)
1987 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
1988 "array-indexed expression is scalar");
1989 else
1990 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
1991 "expression is not array-indexable");
1992 d3dcompiler_free(deref);
1993 free_instr($1);
1994 free_instr($3);
1995 return 1;
1997 if ($3->data_type->type != HLSL_CLASS_SCALAR)
1999 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
2000 "array index is not scalar");
2001 d3dcompiler_free(deref);
2002 free_instr($1);
2003 free_instr($3);
2004 return 1;
2006 deref->type = HLSL_IR_DEREF_ARRAY;
2007 deref->v.array.array = $1;
2008 deref->v.array.index = $3;
2010 $$ = &deref->node;
2012 /* "var_modifiers" doesn't make sense in this case, but it's needed
2013 in the grammar to avoid shift/reduce conflicts. */
2014 | var_modifiers type '(' initializer_expr_list ')'
2016 struct hlsl_ir_constructor *constructor;
2018 TRACE("%s constructor.\n", debug_hlsl_type($2));
2019 if ($1)
2021 hlsl_message("Line %u: unexpected modifier in a constructor.\n",
2022 hlsl_ctx.line_no);
2023 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
2024 return -1;
2026 if ($2->type > HLSL_CLASS_LAST_NUMERIC)
2028 hlsl_message("Line %u: constructors are allowed only for numeric data types.\n",
2029 hlsl_ctx.line_no);
2030 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
2031 return -1;
2033 if ($2->dimx * $2->dimy != components_count_expr_list($4))
2035 hlsl_message("Line %u: wrong number of components in constructor.\n",
2036 hlsl_ctx.line_no);
2037 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
2038 return -1;
2041 constructor = d3dcompiler_alloc(sizeof(*constructor));
2042 constructor->node.type = HLSL_IR_CONSTRUCTOR;
2043 set_location(&constructor->node.loc, &@3);
2044 constructor->node.data_type = $2;
2045 constructor->arguments = $4;
2047 $$ = &constructor->node;
2050 unary_expr: postfix_expr
2052 $$ = $1;
2054 | OP_INC unary_expr
2056 struct hlsl_ir_node *operands[3];
2057 struct source_location loc;
2059 set_location(&loc, &@1);
2060 if ($2->data_type->modifiers & HLSL_MODIFIER_CONST)
2062 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
2063 "modifying a const expression");
2064 return 1;
2066 operands[0] = $2;
2067 operands[1] = operands[2] = NULL;
2068 $$ = &new_expr(HLSL_IR_UNOP_PREINC, operands, &loc)->node;
2070 | OP_DEC unary_expr
2072 struct hlsl_ir_node *operands[3];
2073 struct source_location loc;
2075 set_location(&loc, &@1);
2076 if ($2->data_type->modifiers & HLSL_MODIFIER_CONST)
2078 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
2079 "modifying a const expression");
2080 return 1;
2082 operands[0] = $2;
2083 operands[1] = operands[2] = NULL;
2084 $$ = &new_expr(HLSL_IR_UNOP_PREDEC, operands, &loc)->node;
2086 | unary_op unary_expr
2088 enum hlsl_ir_expr_op ops[] = {0, HLSL_IR_UNOP_NEG,
2089 HLSL_IR_UNOP_LOGIC_NOT, HLSL_IR_UNOP_BIT_NOT};
2090 struct hlsl_ir_node *operands[3];
2091 struct source_location loc;
2093 if ($1 == UNARY_OP_PLUS)
2095 $$ = $2;
2097 else
2099 operands[0] = $2;
2100 operands[1] = operands[2] = NULL;
2101 set_location(&loc, &@1);
2102 $$ = &new_expr(ops[$1], operands, &loc)->node;
2105 /* var_modifiers just to avoid shift/reduce conflicts */
2106 | '(' var_modifiers type array ')' unary_expr
2108 struct hlsl_ir_expr *expr;
2109 struct hlsl_type *src_type = $6->data_type;
2110 struct hlsl_type *dst_type;
2111 struct source_location loc;
2113 set_location(&loc, &@3);
2114 if ($2)
2116 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
2117 "unexpected modifier in a cast");
2118 return 1;
2121 if ($4)
2122 dst_type = new_array_type($3, $4);
2123 else
2124 dst_type = $3;
2126 if (!compatible_data_types(src_type, dst_type))
2128 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
2129 "can't cast from %s to %s",
2130 debug_hlsl_type(src_type), debug_hlsl_type(dst_type));
2131 return 1;
2134 expr = new_cast($6, dst_type, &loc);
2135 $$ = expr ? &expr->node : NULL;
2138 unary_op: '+'
2140 $$ = UNARY_OP_PLUS;
2142 | '-'
2144 $$ = UNARY_OP_MINUS;
2146 | '!'
2148 $$ = UNARY_OP_LOGICNOT;
2150 | '~'
2152 $$ = UNARY_OP_BITNOT;
2155 mul_expr: unary_expr
2157 $$ = $1;
2159 | mul_expr '*' unary_expr
2161 struct source_location loc;
2163 set_location(&loc, &@2);
2164 $$ = &hlsl_mul($1, $3, &loc)->node;
2166 | mul_expr '/' unary_expr
2168 struct source_location loc;
2170 set_location(&loc, &@2);
2171 $$ = &hlsl_div($1, $3, &loc)->node;
2173 | mul_expr '%' unary_expr
2175 struct source_location loc;
2177 set_location(&loc, &@2);
2178 $$ = &hlsl_mod($1, $3, &loc)->node;
2181 add_expr: mul_expr
2183 $$ = $1;
2185 | add_expr '+' mul_expr
2187 struct source_location loc;
2189 set_location(&loc, &@2);
2190 $$ = &hlsl_add($1, $3, &loc)->node;
2192 | add_expr '-' mul_expr
2194 struct source_location loc;
2196 set_location(&loc, &@2);
2197 $$ = &hlsl_sub($1, $3, &loc)->node;
2200 shift_expr: add_expr
2202 $$ = $1;
2204 | shift_expr OP_LEFTSHIFT add_expr
2206 FIXME("Left shift\n");
2208 | shift_expr OP_RIGHTSHIFT add_expr
2210 FIXME("Right shift\n");
2213 relational_expr: shift_expr
2215 $$ = $1;
2217 | relational_expr '<' shift_expr
2219 struct source_location loc;
2221 set_location(&loc, &@2);
2222 $$ = &hlsl_lt($1, $3, &loc)->node;
2224 | relational_expr '>' shift_expr
2226 struct source_location loc;
2228 set_location(&loc, &@2);
2229 $$ = &hlsl_gt($1, $3, &loc)->node;
2231 | relational_expr OP_LE shift_expr
2233 struct source_location loc;
2235 set_location(&loc, &@2);
2236 $$ = &hlsl_le($1, $3, &loc)->node;
2238 | relational_expr OP_GE shift_expr
2240 struct source_location loc;
2242 set_location(&loc, &@2);
2243 $$ = &hlsl_ge($1, $3, &loc)->node;
2246 equality_expr: relational_expr
2248 $$ = $1;
2250 | equality_expr OP_EQ relational_expr
2252 struct source_location loc;
2254 set_location(&loc, &@2);
2255 $$ = &hlsl_eq($1, $3, &loc)->node;
2257 | equality_expr OP_NE relational_expr
2259 struct source_location loc;
2261 set_location(&loc, &@2);
2262 $$ = &hlsl_ne($1, $3, &loc)->node;
2265 bitand_expr: equality_expr
2267 $$ = $1;
2269 | bitand_expr '&' equality_expr
2271 FIXME("bitwise AND\n");
2274 bitxor_expr: bitand_expr
2276 $$ = $1;
2278 | bitxor_expr '^' bitand_expr
2280 FIXME("bitwise XOR\n");
2283 bitor_expr: bitxor_expr
2285 $$ = $1;
2287 | bitor_expr '|' bitxor_expr
2289 FIXME("bitwise OR\n");
2292 logicand_expr: bitor_expr
2294 $$ = $1;
2296 | logicand_expr OP_AND bitor_expr
2298 FIXME("logic AND\n");
2301 logicor_expr: logicand_expr
2303 $$ = $1;
2305 | logicor_expr OP_OR logicand_expr
2307 FIXME("logic OR\n");
2310 conditional_expr: logicor_expr
2312 $$ = $1;
2314 | logicor_expr '?' expr ':' assignment_expr
2316 FIXME("ternary operator\n");
2319 assignment_expr: conditional_expr
2321 $$ = $1;
2323 | unary_expr assign_op assignment_expr
2325 struct source_location loc;
2327 set_location(&loc, &@2);
2328 if ($1->data_type->modifiers & HLSL_MODIFIER_CONST)
2330 hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
2331 "l-value is const");
2332 return 1;
2334 $$ = make_assignment($1, $2, BWRITERSP_WRITEMASK_ALL, $3);
2335 if (!$$)
2336 return 1;
2337 $$->loc = loc;
2340 assign_op: '='
2342 $$ = ASSIGN_OP_ASSIGN;
2344 | OP_ADDASSIGN
2346 $$ = ASSIGN_OP_ADD;
2348 | OP_SUBASSIGN
2350 $$ = ASSIGN_OP_SUB;
2352 | OP_MULASSIGN
2354 $$ = ASSIGN_OP_MUL;
2356 | OP_DIVASSIGN
2358 $$ = ASSIGN_OP_DIV;
2360 | OP_MODASSIGN
2362 $$ = ASSIGN_OP_MOD;
2364 | OP_LEFTSHIFTASSIGN
2366 $$ = ASSIGN_OP_LSHIFT;
2368 | OP_RIGHTSHIFTASSIGN
2370 $$ = ASSIGN_OP_RSHIFT;
2372 | OP_ANDASSIGN
2374 $$ = ASSIGN_OP_AND;
2376 | OP_ORASSIGN
2378 $$ = ASSIGN_OP_OR;
2380 | OP_XORASSIGN
2382 $$ = ASSIGN_OP_XOR;
2385 expr: assignment_expr
2387 $$ = $1;
2389 | expr ',' assignment_expr
2391 FIXME("Comma expression\n");
2396 static void set_location(struct source_location *loc, const struct YYLTYPE *l)
2398 loc->file = hlsl_ctx.source_file;
2399 loc->line = l->first_line;
2400 loc->col = l->first_column;
2403 static DWORD add_modifier(DWORD modifiers, DWORD mod, const struct YYLTYPE *loc)
2405 if (modifiers & mod)
2407 hlsl_report_message(hlsl_ctx.source_file, loc->first_line, loc->first_column, HLSL_LEVEL_ERROR,
2408 "modifier '%s' already specified", debug_modifiers(mod));
2409 return modifiers;
2411 if (mod & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR)
2412 && modifiers & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR))
2414 hlsl_report_message(hlsl_ctx.source_file, loc->first_line, loc->first_column, HLSL_LEVEL_ERROR,
2415 "more than one matrix majority keyword");
2416 return modifiers;
2418 return modifiers | mod;
2421 static void dump_function_decl(struct wine_rb_entry *entry, void *context)
2423 struct hlsl_ir_function_decl *func = WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function_decl, entry);
2424 if (func->body)
2425 debug_dump_ir_function_decl(func);
2428 static void dump_function(struct wine_rb_entry *entry, void *context)
2430 struct hlsl_ir_function *func = WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function, entry);
2431 wine_rb_for_each_entry(&func->overloads, dump_function_decl, NULL);
2434 struct bwriter_shader *parse_hlsl(enum shader_type type, DWORD major, DWORD minor,
2435 const char *entrypoint, char **messages)
2437 struct hlsl_scope *scope, *next_scope;
2438 struct hlsl_type *hlsl_type, *next_type;
2439 struct hlsl_ir_var *var, *next_var;
2440 unsigned int i;
2442 hlsl_ctx.status = PARSE_SUCCESS;
2443 hlsl_ctx.messages.size = hlsl_ctx.messages.capacity = 0;
2444 hlsl_ctx.line_no = hlsl_ctx.column = 1;
2445 hlsl_ctx.source_file = d3dcompiler_strdup("");
2446 hlsl_ctx.source_files = d3dcompiler_alloc(sizeof(*hlsl_ctx.source_files));
2447 if (hlsl_ctx.source_files)
2448 hlsl_ctx.source_files[0] = hlsl_ctx.source_file;
2449 hlsl_ctx.source_files_count = 1;
2450 hlsl_ctx.cur_scope = NULL;
2451 hlsl_ctx.matrix_majority = HLSL_COLUMN_MAJOR;
2452 list_init(&hlsl_ctx.scopes);
2453 list_init(&hlsl_ctx.types);
2454 init_functions_tree(&hlsl_ctx.functions);
2456 push_scope(&hlsl_ctx);
2457 hlsl_ctx.globals = hlsl_ctx.cur_scope;
2458 declare_predefined_types(hlsl_ctx.globals);
2460 hlsl_parse();
2462 if (TRACE_ON(hlsl_parser))
2464 TRACE("IR dump.\n");
2465 wine_rb_for_each_entry(&hlsl_ctx.functions, dump_function, NULL);
2468 TRACE("Compilation status = %d\n", hlsl_ctx.status);
2469 if (messages)
2471 if (hlsl_ctx.messages.size)
2472 *messages = hlsl_ctx.messages.string;
2473 else
2474 *messages = NULL;
2476 else
2478 if (hlsl_ctx.messages.capacity)
2479 d3dcompiler_free(hlsl_ctx.messages.string);
2482 for (i = 0; i < hlsl_ctx.source_files_count; ++i)
2483 d3dcompiler_free((void *)hlsl_ctx.source_files[i]);
2484 d3dcompiler_free(hlsl_ctx.source_files);
2486 TRACE("Freeing functions IR.\n");
2487 wine_rb_destroy(&hlsl_ctx.functions, free_function_rb, NULL);
2489 TRACE("Freeing variables.\n");
2490 LIST_FOR_EACH_ENTRY_SAFE(scope, next_scope, &hlsl_ctx.scopes, struct hlsl_scope, entry)
2492 LIST_FOR_EACH_ENTRY_SAFE(var, next_var, &scope->vars, struct hlsl_ir_var, scope_entry)
2494 free_declaration(var);
2496 wine_rb_destroy(&scope->types, NULL, NULL);
2497 d3dcompiler_free(scope);
2500 TRACE("Freeing types.\n");
2501 LIST_FOR_EACH_ENTRY_SAFE(hlsl_type, next_type, &hlsl_ctx.types, struct hlsl_type, entry)
2503 free_hlsl_type(hlsl_type);
2506 return NULL;