hlsl.y 52.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * HLSL parser
 *
 * Copyright 2008 Stefan Dösinger
 * Copyright 2012 Matteo Bruni for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
%{
#include "config.h"
#include "wine/debug.h"

#include <stdio.h>

#include "d3dcompiler_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(hlsl_parser);

31
int hlsl_lex(void);
32 33 34

struct hlsl_parse_ctx hlsl_ctx;

35 36 37
struct YYLTYPE;
static void set_location(struct source_location *loc, const struct YYLTYPE *l);

38 39 40 41 42 43 44 45 46
void hlsl_message(const char *fmt, ...)
{
    va_list args;

    va_start(args, fmt);
    compilation_message(&hlsl_ctx.messages, fmt, args);
    va_end(args);
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
static const char *hlsl_get_error_level_name(enum hlsl_error_level level)
{
    const char *names[] =
    {
        "error",
        "warning",
        "note",
    };
    return names[level];
}

void hlsl_report_message(const char *filename, DWORD line, DWORD column,
        enum hlsl_error_level level, const char *fmt, ...)
{
    va_list args;
    char *string = NULL;
    int rc, size = 0;

    while (1)
    {
        va_start(args, fmt);
        rc = vsnprintf(string, size, fmt, args);
        va_end(args);

        if (rc >= 0 && rc < size)
            break;

        if (rc >= size)
            size = rc + 1;
        else
            size = size ? size * 2 : 32;

        if (!string)
            string = d3dcompiler_alloc(size);
        else
            string = d3dcompiler_realloc(string, size);
        if (!string)
        {
            ERR("Error reallocating memory for a string.\n");
            return;
        }
    }

    hlsl_message("%s:%u:%u: %s: %s\n", filename, line, column, hlsl_get_error_level_name(level), string);
    d3dcompiler_free(string);

    if (level == HLSL_LEVEL_ERROR)
        set_parse_status(&hlsl_ctx.status, PARSE_ERR);
    else if (level == HLSL_LEVEL_WARNING)
        set_parse_status(&hlsl_ctx.status, PARSE_WARN);
}

99 100
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);
102 103
}

104 105 106
static void debug_dump_decl(struct hlsl_type *type, DWORD modifiers, const char *declname, unsigned int line_no)
{
    TRACE("Line %u: ", line_no);
107 108
    if (modifiers)
        TRACE("%s ", debug_modifiers(modifiers));
109 110 111
    TRACE("%s %s;\n", debug_hlsl_type(type), declname);
}

112 113 114 115 116 117 118 119 120
static void check_invalid_matrix_modifiers(DWORD modifiers, struct source_location *loc)
{
    if (modifiers & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR))
    {
        hlsl_report_message(loc->file, loc->line, loc->col, HLSL_LEVEL_ERROR,
                "'row_major' or 'column_major' modifiers are only allowed for matrices");
    }
}

121 122 123 124 125
static BOOL declare_variable(struct hlsl_ir_var *decl, BOOL local)
{
    BOOL ret;

    TRACE("Declaring variable %s.\n", decl->name);
126 127 128 129 130 131 132 133
    if (decl->node.data_type->type == HLSL_CLASS_MATRIX)
    {
        if (!(decl->modifiers & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR)))
        {
            decl->modifiers |= hlsl_ctx.matrix_majority == HLSL_ROW_MAJOR
                    ? HLSL_MODIFIER_ROW_MAJOR : HLSL_MODIFIER_COLUMN_MAJOR;
        }
    }
134 135 136
    else
        check_invalid_matrix_modifiers(decl->modifiers, &decl->node.loc);

137 138 139 140 141 142
    if (local)
    {
        DWORD invalid = decl->modifiers & (HLSL_STORAGE_EXTERN | HLSL_STORAGE_SHARED
                | HLSL_STORAGE_GROUPSHARED | HLSL_STORAGE_UNIFORM);
        if (invalid)
        {
143 144
            hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_ERROR,
                    "modifier '%s' invalid for local variables", debug_modifiers(invalid));
145
        }
146 147 148 149 150 151
        if (decl->semantic)
        {
            hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_ERROR,
                    "semantics are not allowed on local variables");
            return FALSE;
        }
152
    }
153 154 155 156 157 158 159 160 161
    else
    {
        if (find_function(decl->name))
        {
            hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_ERROR,
                    "redefinition of '%s'", decl->name);
            return FALSE;
        }
    }
162
    ret = add_declaration(hlsl_ctx.cur_scope, decl, local);
163
    if (!ret)
164 165 166
    {
        struct hlsl_ir_var *old = get_variable(hlsl_ctx.cur_scope, decl->name);

167 168 169 170
        hlsl_report_message(decl->node.loc.file, decl->node.loc.line, decl->node.loc.col, HLSL_LEVEL_ERROR,
                "\"%s\" already declared", decl->name);
        hlsl_report_message(old->node.loc.file, old->node.loc.line, old->node.loc.col, HLSL_LEVEL_NOTE,
                "\"%s\" was previously declared here", old->name);
171 172 173 174 175
        return FALSE;
    }
    return TRUE;
}

176
static DWORD add_modifier(DWORD modifiers, DWORD mod, const struct YYLTYPE *loc);
177

178 179 180 181 182 183 184 185 186 187 188 189
static unsigned int components_count_expr_list(struct list *list)
{
    struct hlsl_ir_node *node;
    unsigned int count = 0;

    LIST_FOR_EACH_ENTRY(node, list, struct hlsl_ir_node, entry)
    {
        count += components_count_type(node->data_type);
    }
    return count;
}

190 191
%}

192
%locations
193 194 195 196
%error-verbose

%union
{
197
    struct hlsl_type *type;
198
    INT intval;
199
    FLOAT floatval;
200
    BOOL boolval;
201 202
    char *name;
    DWORD modifiers;
203
    struct hlsl_ir_var *var;
204
    struct hlsl_ir_node *instr;
205
    struct list *list;
206 207
    struct hlsl_ir_function_decl *function;
    struct parse_parameter parameter;
208
    struct parse_variable_def *variable_def;
209
    enum parse_unary_op unary_op;
210
    enum parse_assign_op assign_op;
211 212
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
%token KW_BLENDSTATE
%token KW_BREAK
%token KW_BUFFER
%token KW_CBUFFER
%token KW_COLUMN_MAJOR
%token KW_COMPILE
%token KW_CONST
%token KW_CONTINUE
%token KW_DEPTHSTENCILSTATE
%token KW_DEPTHSTENCILVIEW
%token KW_DISCARD
%token KW_DO
%token KW_DOUBLE
%token KW_ELSE
%token KW_EXTERN
%token KW_FALSE
%token KW_FOR
%token KW_GEOMETRYSHADER
%token KW_GROUPSHARED
%token KW_IF
%token KW_IN
%token KW_INLINE
%token KW_INOUT
%token KW_MATRIX
%token KW_NAMESPACE
%token KW_NOINTERPOLATION
%token KW_OUT
%token KW_PASS
%token KW_PIXELSHADER
%token KW_PRECISE
%token KW_RASTERIZERSTATE
%token KW_RENDERTARGETVIEW
%token KW_RETURN
%token KW_REGISTER
%token KW_ROW_MAJOR
%token KW_SAMPLER
%token KW_SAMPLER1D
%token KW_SAMPLER2D
%token KW_SAMPLER3D
%token KW_SAMPLERCUBE
%token KW_SAMPLER_STATE
%token KW_SAMPLERCOMPARISONSTATE
%token KW_SHARED
%token KW_STATEBLOCK
%token KW_STATEBLOCK_STATE
%token KW_STATIC
%token KW_STRING
%token KW_STRUCT
%token KW_SWITCH
%token KW_TBUFFER
%token KW_TECHNIQUE
%token KW_TECHNIQUE10
%token KW_TEXTURE
%token KW_TEXTURE1D
%token KW_TEXTURE1DARRAY
%token KW_TEXTURE2D
%token KW_TEXTURE2DARRAY
%token KW_TEXTURE2DMS
%token KW_TEXTURE2DMSARRAY
%token KW_TEXTURE3D
%token KW_TEXTURE3DARRAY
%token KW_TEXTURECUBE
%token KW_TRUE
%token KW_TYPEDEF
%token KW_UNIFORM
%token KW_VECTOR
%token KW_VERTEXSHADER
%token KW_VOID
%token KW_VOLATILE
%token KW_WHILE

%token OP_INC
%token OP_DEC
%token OP_AND
%token OP_OR
%token OP_EQ
%token OP_LEFTSHIFT
%token OP_LEFTSHIFTASSIGN
%token OP_RIGHTSHIFT
%token OP_RIGHTSHIFTASSIGN
%token OP_ELLIPSIS
%token OP_LE
%token OP_GE
%token OP_NE
%token OP_ADDASSIGN
%token OP_SUBASSIGN
%token OP_MULASSIGN
%token OP_DIVASSIGN
%token OP_MODASSIGN
%token OP_ANDASSIGN
%token OP_ORASSIGN
%token OP_XORASSIGN
%token OP_UNKNOWN1
%token OP_UNKNOWN2
%token OP_UNKNOWN3
%token OP_UNKNOWN4

310 311
%token <intval> PRE_LINE

312
%token <name> VAR_IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
313
%type <name> any_identifier var_identifier
314
%token <name> STRING
315 316
%token <floatval> C_FLOAT
%token <intval> C_INTEGER
317
%type <boolval> boolean
318 319
%type <type> base_type
%type <type> type
320
%type <list> declaration_statement
321 322 323
%type <list> complex_initializer
%type <list> initializer_expr_list
%type <instr> initializer_expr
324
%type <modifiers> var_modifiers
325 326
%type <list> parameters
%type <list> param_list
327
%type <instr> expr
328
%type <var> variable
329
%type <intval> array
330 331 332
%type <list> statement
%type <list> statement_list
%type <list> compound_statement
333 334 335
%type <function> func_declaration
%type <function> func_prototype
%type <parameter> parameter
336 337 338
%type <name> semantic
%type <variable_def> variable_def
%type <list> variables_def
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
%type <instr> primary_expr
%type <instr> postfix_expr
%type <instr> unary_expr
%type <instr> mul_expr
%type <instr> add_expr
%type <instr> shift_expr
%type <instr> relational_expr
%type <instr> equality_expr
%type <instr> bitand_expr
%type <instr> bitxor_expr
%type <instr> bitor_expr
%type <instr> logicand_expr
%type <instr> logicor_expr
%type <instr> conditional_expr
%type <instr> assignment_expr
354
%type <list> expr_statement
355
%type <unary_op> unary_op
356
%type <assign_op> assign_op
357
%type <modifiers> input_mod
358 359 360 361 362
%%

hlsl_prog:                /* empty */
                            {
                            }
363 364 365 366 367
                        | hlsl_prog func_declaration
                            {
                                FIXME("Check that the function doesn't conflict with an already declared one.\n");
                                list_add_tail(&hlsl_ctx.functions, &$2->node.entry);
                            }
368 369 370 371
                        | hlsl_prog declaration_statement
                            {
                                TRACE("Declaration statement parsed.\n");
                            }
372 373 374 375 376 377
                        | hlsl_prog preproc_directive
                            {
                            }

preproc_directive:        PRE_LINE STRING
                            {
378
                                TRACE("Updating line information to file %s, line %u\n", debugstr_a($2), $1);
379
                                hlsl_ctx.line_no = $1;
380 381 382 383 384 385 386 387 388 389 390 391 392
                                if (strcmp($2, hlsl_ctx.source_file))
                                {
                                    const char **new_array;

                                    hlsl_ctx.source_file = $2;
                                    new_array = d3dcompiler_realloc(hlsl_ctx.source_files,
                                            sizeof(*hlsl_ctx.source_files) * hlsl_ctx.source_files_count + 1);
                                    if (new_array)
                                    {
                                        hlsl_ctx.source_files = new_array;
                                        hlsl_ctx.source_files[hlsl_ctx.source_files_count++] = $2;
                                    }
                                }
393 394
                            }

395 396 397 398
any_identifier:           VAR_IDENTIFIER
                        | TYPE_IDENTIFIER
                        | NEW_IDENTIFIER

399 400 401 402 403 404 405 406
func_declaration:         func_prototype compound_statement
                            {
                                TRACE("Function %s parsed.\n", $1->name);
                                $$ = $1;
                                $$->body = $2;
                                pop_scope(&hlsl_ctx);
                            }
                        | func_prototype ';'
407 408 409 410 411 412 413 414
                            {
                                TRACE("Function prototype for %s.\n", $1->name);
                                $$ = $1;
                                pop_scope(&hlsl_ctx);
                            }

func_prototype:           var_modifiers type var_identifier '(' parameters ')' semantic
                            {
415 416 417 418 419 420
                                if (get_variable(hlsl_ctx.globals, $3))
                                {
                                    hlsl_report_message(hlsl_ctx.source_file, @3.first_line, @3.first_column,
                                            HLSL_LEVEL_ERROR, "redefinition of '%s'\n", $3);
                                    return 1;
                                }
421 422 423 424 425
                                if ($2->base_type == HLSL_TYPE_VOID && $7)
                                {
                                    hlsl_report_message(hlsl_ctx.source_file, @7.first_line, @7.first_column,
                                            HLSL_LEVEL_ERROR, "void function with a semantic");
                                }
426

427 428 429 430 431 432 433 434 435
                                $$ = new_func_decl($3, $2, $5);
                                if (!$$)
                                {
                                    ERR("Out of memory.\n");
                                    return -1;
                                }
                                $$->semantic = $7;
                            }

436 437 438 439 440 441 442 443 444 445 446
compound_statement:       '{' '}'
                            {
                                $$ = d3dcompiler_alloc(sizeof(*$$));
                                list_init($$);
                            }
                        | '{' scope_start statement_list '}'
                            {
                                pop_scope(&hlsl_ctx);
                                $$ = $3;
                            }

447 448 449 450 451 452 453 454
scope_start:              /* Empty */
                            {
                                push_scope(&hlsl_ctx);
                            }

var_identifier:           VAR_IDENTIFIER
                        | NEW_IDENTIFIER

455 456 457 458 459 460 461 462 463
semantic:                 /* Empty */
                            {
                                $$ = NULL;
                            }
                        | ':' any_identifier
                            {
                                $$ = $2;
                            }

464 465 466 467 468 469 470 471 472 473 474 475
parameters:               scope_start
                            {
                                $$ = d3dcompiler_alloc(sizeof(*$$));
                                list_init($$);
                            }
                        | scope_start param_list
                            {
                                $$ = $2;
                            }

param_list:               parameter
                            {
476 477
                                struct source_location loc;

478 479
                                $$ = d3dcompiler_alloc(sizeof(*$$));
                                list_init($$);
480 481
                                set_location(&loc, &@1);
                                if (!add_func_parameter($$, &$1, &loc))
482 483 484 485 486 487 488 489
                                {
                                    ERR("Error adding function parameter %s.\n", $1.name);
                                    set_parse_status(&hlsl_ctx.status, PARSE_ERR);
                                    return -1;
                                }
                            }
                        | param_list ',' parameter
                            {
490 491
                                struct source_location loc;

492
                                $$ = $1;
493 494
                                set_location(&loc, &@3);
                                if (!add_func_parameter($$, &$3, &loc))
495
                                {
496 497
                                    hlsl_report_message(loc.file, loc.line, loc.col, HLSL_LEVEL_ERROR,
                                            "duplicate parameter %s", $3.name);
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
                                    return 1;
                                }
                            }

parameter:                input_mod var_modifiers type any_identifier semantic
                            {
                                $$.modifiers = $1;
                                $$.modifiers |= $2;
                                $$.type = $3;
                                $$.name = $4;
                                $$.semantic = $5;
                            }

input_mod:                /* Empty */
                            {
                                $$ = HLSL_MODIFIER_IN;
                            }
                        | KW_IN
                            {
                                $$ = HLSL_MODIFIER_IN;
                            }
                        | KW_OUT
                            {
                                $$ = HLSL_MODIFIER_OUT;
                            }
                        | KW_INOUT
                            {
                                $$ = HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT;
                            }

528 529 530 531
type:                     base_type
                            {
                                $$ = $1;
                            }
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
                        | KW_VECTOR '<' base_type ',' C_INTEGER '>'
                            {
                                if ($3->type != HLSL_CLASS_SCALAR)
                                {
                                    hlsl_message("Line %u: vectors of non-scalar types are not allowed.\n",
                                            hlsl_ctx.line_no);
                                    set_parse_status(&hlsl_ctx.status, PARSE_ERR);
                                    return 1;
                                }
                                if ($5 < 1 || $5 > 4)
                                {
                                    hlsl_message("Line %u: vector size must be between 1 and 4.\n",
                                            hlsl_ctx.line_no);
                                    set_parse_status(&hlsl_ctx.status, PARSE_ERR);
                                    return 1;
                                }

                                $$ = new_hlsl_type(NULL, HLSL_CLASS_VECTOR, $3->base_type, $5, 1);
                            }
                        | KW_MATRIX '<' base_type ',' C_INTEGER ',' C_INTEGER '>'
                            {
                                if ($3->type != HLSL_CLASS_SCALAR)
                                {
                                    hlsl_message("Line %u: matrices of non-scalar types are not allowed.\n",
                                            hlsl_ctx.line_no);
                                    set_parse_status(&hlsl_ctx.status, PARSE_ERR);
                                    return 1;
                                }
                                if ($5 < 1 || $5 > 4 || $7 < 1 || $7 > 4)
                                {
                                    hlsl_message("Line %u: matrix dimensions must be between 1 and 4.\n",
                                            hlsl_ctx.line_no);
                                    set_parse_status(&hlsl_ctx.status, PARSE_ERR);
                                    return 1;
                                }

                                $$ = new_hlsl_type(NULL, HLSL_CLASS_MATRIX, $3->base_type, $5, $7);
                            }
570 571 572

base_type:                KW_VOID
                            {
573
                                $$ = new_hlsl_type(d3dcompiler_strdup("void"), HLSL_CLASS_OBJECT, HLSL_TYPE_VOID, 1, 1);
574
                            }
575 576
                        | KW_SAMPLER
                            {
577
                                $$ = new_hlsl_type(d3dcompiler_strdup("sampler"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
578 579 580 581
                                $$->sampler_dim = HLSL_SAMPLER_DIM_GENERIC;
                            }
                        | KW_SAMPLER1D
                            {
582
                                $$ = new_hlsl_type(d3dcompiler_strdup("sampler1D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
583 584 585 586
                                $$->sampler_dim = HLSL_SAMPLER_DIM_1D;
                            }
                        | KW_SAMPLER2D
                            {
587
                                $$ = new_hlsl_type(d3dcompiler_strdup("sampler2D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
588 589 590 591
                                $$->sampler_dim = HLSL_SAMPLER_DIM_2D;
                            }
                        | KW_SAMPLER3D
                            {
592
                                $$ = new_hlsl_type(d3dcompiler_strdup("sampler3D"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
593 594 595 596
                                $$->sampler_dim = HLSL_SAMPLER_DIM_3D;
                            }
                        | KW_SAMPLERCUBE
                            {
597
                                $$ = new_hlsl_type(d3dcompiler_strdup("samplerCUBE"), HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
598 599
                                $$->sampler_dim = HLSL_SAMPLER_DIM_CUBE;
                            }
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
                        | TYPE_IDENTIFIER
                            {
                                struct hlsl_type *type;

                                TRACE("Type %s.\n", $1);
                                type = get_type(hlsl_ctx.cur_scope, $1, TRUE);
                                $$ = type;
                                d3dcompiler_free($1);
                            }
                        | KW_STRUCT TYPE_IDENTIFIER
                            {
                                struct hlsl_type *type;

                                TRACE("Struct type %s.\n", $2);
                                type = get_type(hlsl_ctx.cur_scope, $2, TRUE);
615
                                if (type->type != HLSL_CLASS_STRUCT)
616
                                {
617
                                    hlsl_message("Line %u: redefining %s as a structure.\n",
618 619 620 621 622 623 624 625 626 627 628 629
                                            hlsl_ctx.line_no, $2);
                                    set_parse_status(&hlsl_ctx.status, PARSE_ERR);
                                }
                                else
                                {
                                    $$ = type;
                                }
                                d3dcompiler_free($2);
                            }

declaration_statement:    declaration
                            {
630 631
                                $$ = d3dcompiler_alloc(sizeof(*$$));
                                list_init($$);
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
                            }

declaration:              var_modifiers type variables_def ';'
                            {
                                struct parse_variable_def *v, *v_next;
                                struct hlsl_ir_var *var;
                                BOOL ret, local = TRUE;

                                LIST_FOR_EACH_ENTRY_SAFE(v, v_next, $3, struct parse_variable_def, entry)
                                {
                                    debug_dump_decl($2, $1, v->name, hlsl_ctx.line_no);
                                    var = d3dcompiler_alloc(sizeof(*var));
                                    var->node.type = HLSL_IR_VAR;
                                    if (v->array_size)
                                        var->node.data_type = new_array_type($2, v->array_size);
                                    else
                                        var->node.data_type = $2;
649
                                    var->node.loc = v->loc;
650 651 652 653
                                    var->name = v->name;
                                    var->modifiers = $1;
                                    var->semantic = v->semantic;

654 655 656 657 658 659
                                    if (hlsl_ctx.cur_scope == hlsl_ctx.globals)
                                    {
                                        var->modifiers |= HLSL_STORAGE_UNIFORM;
                                        local = FALSE;
                                    }

660 661 662 663 664 665 666 667 668
                                    if (var->modifiers & HLSL_MODIFIER_CONST && !v->initializer)
                                    {
                                        hlsl_report_message(v->loc.file, v->loc.line, v->loc.col,
                                                HLSL_LEVEL_ERROR, "const variable without initializer");
                                        free_declaration(var);
                                        d3dcompiler_free(v);
                                        continue;
                                    }

669
                                    ret = declare_variable(var, local);
670
                                    if (!ret)
671 672 673
                                        free_declaration(var);
                                    else
                                        TRACE("Declared variable %s.\n", var->name);
674 675 676 677 678 679 680

                                    if (v->initializer)
                                    {
                                        FIXME("Variable with an initializer.\n");
                                        free_instr_list(v->initializer);
                                    }

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
                                    d3dcompiler_free(v);
                                }
                                d3dcompiler_free($3);
                            }

variables_def:            variable_def
                            {
                                $$ = d3dcompiler_alloc(sizeof(*$$));
                                list_init($$);
                                list_add_head($$, &$1->entry);
                            }
                        | variables_def ',' variable_def
                            {
                                $$ = $1;
                                list_add_tail($$, &$3->entry);
                            }

variable_def:             any_identifier array semantic
                            {
                                $$ = d3dcompiler_alloc(sizeof(*$$));
701
                                set_location(&$$->loc, &@1);
702 703 704 705
                                $$->name = $1;
                                $$->array_size = $2;
                                $$->semantic = $3;
                            }
706 707 708 709
                        | any_identifier array semantic '=' complex_initializer
                            {
                                TRACE("Declaration with initializer.\n");
                                $$ = d3dcompiler_alloc(sizeof(*$$));
710
                                set_location(&$$->loc, &@1);
711 712 713 714 715
                                $$->name = $1;
                                $$->array_size = $2;
                                $$->semantic = $3;
                                $$->initializer = $5;
                            }
716 717 718 719 720

array:                    /* Empty */
                            {
                                $$ = 0;
                            }
721 722 723 724 725 726
                        | '[' expr ']'
                            {
                                FIXME("Array.\n");
                                $$ = 0;
                                free_instr($2);
                            }
727 728 729 730 731

var_modifiers:            /* Empty */
                            {
                                $$ = 0;
                            }
732 733
                        | KW_EXTERN var_modifiers
                            {
734
                                $$ = add_modifier($2, HLSL_STORAGE_EXTERN, &@1);
735 736 737
                            }
                        | KW_NOINTERPOLATION var_modifiers
                            {
738
                                $$ = add_modifier($2, HLSL_STORAGE_NOINTERPOLATION, &@1);
739 740 741
                            }
                        | KW_PRECISE var_modifiers
                            {
742
                                $$ = add_modifier($2, HLSL_MODIFIER_PRECISE, &@1);
743 744 745
                            }
                        | KW_SHARED var_modifiers
                            {
746
                                $$ = add_modifier($2, HLSL_STORAGE_SHARED, &@1);
747 748 749
                            }
                        | KW_GROUPSHARED var_modifiers
                            {
750
                                $$ = add_modifier($2, HLSL_STORAGE_GROUPSHARED, &@1);
751 752 753
                            }
                        | KW_STATIC var_modifiers
                            {
754
                                $$ = add_modifier($2, HLSL_STORAGE_STATIC, &@1);
755 756 757
                            }
                        | KW_UNIFORM var_modifiers
                            {
758
                                $$ = add_modifier($2, HLSL_STORAGE_UNIFORM, &@1);
759 760 761
                            }
                        | KW_VOLATILE var_modifiers
                            {
762
                                $$ = add_modifier($2, HLSL_STORAGE_VOLATILE, &@1);
763 764 765
                            }
                        | KW_CONST var_modifiers
                            {
766
                                $$ = add_modifier($2, HLSL_MODIFIER_CONST, &@1);
767 768 769
                            }
                        | KW_ROW_MAJOR var_modifiers
                            {
770
                                $$ = add_modifier($2, HLSL_MODIFIER_ROW_MAJOR, &@1);
771 772 773
                            }
                        | KW_COLUMN_MAJOR var_modifiers
                            {
774
                                $$ = add_modifier($2, HLSL_MODIFIER_COLUMN_MAJOR, &@1);
775
                            }
776

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
complex_initializer:      initializer_expr
                            {
                                $$ = d3dcompiler_alloc(sizeof(*$$));
                                list_init($$);
                                list_add_head($$, &$1->entry);
                            }
                        | '{' initializer_expr_list '}'
                            {
                                $$ = $2;
                            }

initializer_expr:         assignment_expr
                            {
                                $$ = $1;
                            }

initializer_expr_list:    initializer_expr
                            {
                                $$ = d3dcompiler_alloc(sizeof(*$$));
                                list_init($$);
                                list_add_head($$, &$1->entry);
                            }
                        | initializer_expr_list ',' initializer_expr
                            {
                                $$ = $1;
                                list_add_tail($$, &$3->entry);
                            }

boolean:                  KW_TRUE
                            {
                                $$ = TRUE;
                            }
                        | KW_FALSE
                            {
                                $$ = FALSE;
                            }

814 815 816 817 818 819 820 821 822 823 824 825 826
statement_list:           statement
                            {
                                $$ = $1;
                            }
                        | statement_list statement
                            {
                                $$ = $1;
                                list_move_tail($$, $2);
                                d3dcompiler_free($2);
                            }

statement:                declaration_statement
                            {
827
                                $$ = $1;
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
                            }
                        | expr_statement
                            {
                                $$ = $1;
                            }
                        | compound_statement
                            {
                                $$ = $1;
                            }

expr_statement:           ';'
                            {
                                $$ = d3dcompiler_alloc(sizeof(*$$));
                                list_init($$);
                            }
                        | expr ';'
                            {
                                $$ = d3dcompiler_alloc(sizeof(*$$));
                                list_init($$);
                                if ($1)
                                    list_add_head($$, &$1->entry);
                            }

851 852 853 854 855 856 857 858 859
primary_expr:             C_FLOAT
                            {
                                struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
                                if (!c)
                                {
                                    ERR("Out of memory.\n");
                                    return -1;
                                }
                                c->node.type = HLSL_IR_CONSTANT;
860
                                set_location(&c->node.loc, &yylloc);
861 862 863 864 865 866 867 868 869 870 871 872 873
                                c->node.data_type = new_hlsl_type("float", HLSL_CLASS_SCALAR, HLSL_TYPE_FLOAT, 1, 1);
                                c->v.value.f[0] = $1;
                                $$ = &c->node;
                            }
                        | C_INTEGER
                            {
                                struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
                                if (!c)
                                {
                                    ERR("Out of memory.\n");
                                    return -1;
                                }
                                c->node.type = HLSL_IR_CONSTANT;
874
                                set_location(&c->node.loc, &yylloc);
875 876 877 878 879 880 881 882 883 884 885 886 887
                                c->node.data_type = new_hlsl_type("int", HLSL_CLASS_SCALAR, HLSL_TYPE_INT, 1, 1);
                                c->v.value.i[0] = $1;
                                $$ = &c->node;
                            }
                        | boolean
                            {
                                struct hlsl_ir_constant *c = d3dcompiler_alloc(sizeof(*c));
                                if (!c)
                                {
                                    ERR("Out of memory.\n");
                                    return -1;
                                }
                                c->node.type = HLSL_IR_CONSTANT;
888
                                set_location(&c->node.loc, &yylloc);
889 890 891 892
                                c->node.data_type = new_hlsl_type("bool", HLSL_CLASS_SCALAR, HLSL_TYPE_BOOL, 1, 1);
                                c->v.value.b[0] = $1;
                                $$ = &c->node;
                            }
893 894 895
                        | variable
                            {
                                struct hlsl_ir_deref *deref = new_var_deref($1);
896 897 898 899 900 901 902
                                if (deref)
                                {
                                    $$ = &deref->node;
                                    set_location(&$$->loc, &@1);
                                }
                                else
                                    $$ = NULL;
903
                            }
904 905 906 907 908
                        | '(' expr ')'
                            {
                                $$ = $2;
                            }

909 910 911 912 913 914 915 916 917 918 919 920 921 922
variable:                 VAR_IDENTIFIER
                            {
                                struct hlsl_ir_var *var;
                                var = get_variable(hlsl_ctx.cur_scope, $1);
                                if (!var)
                                {
                                    hlsl_message("Line %d: variable '%s' not declared\n",
                                            hlsl_ctx.line_no, $1);
                                    set_parse_status(&hlsl_ctx.status, PARSE_ERR);
                                    return 1;
                                }
                                $$ = var;
                            }

923 924 925 926
postfix_expr:             primary_expr
                            {
                                $$ = $1;
                            }
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
                        | postfix_expr OP_INC
                            {
                                struct hlsl_ir_node *operands[3];
                                struct source_location loc;

                                operands[0] = $1;
                                operands[1] = operands[2] = NULL;
                                set_location(&loc, &@2);
                                $$ = &new_expr(HLSL_IR_BINOP_POSTINC, operands, &loc)->node;
                            }
                        | postfix_expr OP_DEC
                            {
                                struct hlsl_ir_node *operands[3];
                                struct source_location loc;

                                operands[0] = $1;
                                operands[1] = operands[2] = NULL;
                                set_location(&loc, &@2);
                                $$ = &new_expr(HLSL_IR_BINOP_POSTDEC, operands, &loc)->node;
                            }
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
                          /* "var_modifiers" doesn't make sense in this case, but it's needed
                             in the grammar to avoid shift/reduce conflicts. */
                        | var_modifiers type '(' initializer_expr_list ')'
                            {
                                struct hlsl_ir_constructor *constructor;

                                TRACE("%s constructor.\n", debug_hlsl_type($2));
                                if ($1)
                                {
                                    hlsl_message("Line %u: unexpected modifier in a constructor.\n",
                                            hlsl_ctx.line_no);
                                    set_parse_status(&hlsl_ctx.status, PARSE_ERR);
                                    return -1;
                                }
                                if ($2->type > HLSL_CLASS_LAST_NUMERIC)
                                {
                                    hlsl_message("Line %u: constructors are allowed only for numeric data types.\n",
                                            hlsl_ctx.line_no);
                                    set_parse_status(&hlsl_ctx.status, PARSE_ERR);
                                    return -1;
                                }
                                if ($2->dimx * $2->dimy != components_count_expr_list($4))
                                {
                                    hlsl_message("Line %u: wrong number of components in constructor.\n",
                                            hlsl_ctx.line_no);
                                    set_parse_status(&hlsl_ctx.status, PARSE_ERR);
                                    return -1;
                                }

                                constructor = d3dcompiler_alloc(sizeof(*constructor));
                                constructor->node.type = HLSL_IR_CONSTRUCTOR;
978
                                set_location(&constructor->node.loc, &@3);
979 980 981 982 983
                                constructor->node.data_type = $2;
                                constructor->arguments = $4;

                                $$ = &constructor->node;
                            }
984 985 986 987 988

unary_expr:               postfix_expr
                            {
                                $$ = $1;
                            }
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
                        | OP_INC unary_expr
                            {
                                struct hlsl_ir_node *operands[3];
                                struct source_location loc;

                                operands[0] = $2;
                                operands[1] = operands[2] = NULL;
                                set_location(&loc, &@1);
                                $$ = &new_expr(HLSL_IR_BINOP_PREINC, operands, &loc)->node;
                            }
                        | OP_DEC unary_expr
                            {
                                struct hlsl_ir_node *operands[3];
                                struct source_location loc;

                                operands[0] = $2;
                                operands[1] = operands[2] = NULL;
                                set_location(&loc, &@1);
                                $$ = &new_expr(HLSL_IR_BINOP_PREDEC, operands, &loc)->node;
                            }
                        | unary_op unary_expr
                            {
                                enum hlsl_ir_expr_op ops[] = {0, HLSL_IR_UNOP_NEG,
                                        HLSL_IR_UNOP_LOGIC_NOT, HLSL_IR_UNOP_BIT_NOT};
                                struct hlsl_ir_node *operands[3];
                                struct source_location loc;

                                if ($1 == UNARY_OP_PLUS)
                                {
                                    $$ = $2;
                                }
                                else
                                {
                                    operands[0] = $2;
                                    operands[1] = operands[2] = NULL;
                                    set_location(&loc, &@1);
                                    $$ = &new_expr(ops[$1], operands, &loc)->node;
                                }
                            }

unary_op:                 '+'
                            {
                                $$ = UNARY_OP_PLUS;
                            }
                        | '-'
                            {
                                $$ = UNARY_OP_MINUS;
                            }
                        | '!'
                            {
                                $$ = UNARY_OP_LOGICNOT;
                            }
                        | '~'
                            {
                                $$ = UNARY_OP_BITNOT;
                            }
1045 1046 1047 1048 1049

mul_expr:                 unary_expr
                            {
                                $$ = $1;
                            }
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
                        | mul_expr '*' unary_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_mul($1, $3, &loc)->node;
                            }
                        | mul_expr '/' unary_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_div($1, $3, &loc)->node;
                            }
                        | mul_expr '%' unary_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_mod($1, $3, &loc)->node;
                            }
1071 1072 1073 1074 1075

add_expr:                 mul_expr
                            {
                                $$ = $1;
                            }
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
                        | add_expr '+' mul_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_add($1, $3, &loc)->node;
                            }
                        | add_expr '-' mul_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_sub($1, $3, &loc)->node;
                            }
1090 1091 1092 1093 1094

shift_expr:               add_expr
                            {
                                $$ = $1;
                            }
1095 1096 1097 1098 1099 1100 1101 1102
                        | shift_expr OP_LEFTSHIFT add_expr
                            {
                                FIXME("Left shift\n");
                            }
                        | shift_expr OP_RIGHTSHIFT add_expr
                            {
                                FIXME("Right shift\n");
                            }
1103 1104 1105 1106 1107

relational_expr:          shift_expr
                            {
                                $$ = $1;
                            }
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
                        | relational_expr '<' shift_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_lt($1, $3, &loc)->node;
                            }
                        | relational_expr '>' shift_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_gt($1, $3, &loc)->node;
                            }
                        | relational_expr OP_LE shift_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_le($1, $3, &loc)->node;
                            }
                        | relational_expr OP_GE shift_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_ge($1, $3, &loc)->node;
                            }
1136 1137 1138 1139 1140

equality_expr:            relational_expr
                            {
                                $$ = $1;
                            }
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
                        | equality_expr OP_EQ relational_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_eq($1, $3, &loc)->node;
                            }
                        | equality_expr OP_NE relational_expr
                            {
                                struct source_location loc;

                                set_location(&loc, &@2);
                                $$ = &hlsl_ne($1, $3, &loc)->node;
                            }
1155 1156 1157 1158 1159

bitand_expr:              equality_expr
                            {
                                $$ = $1;
                            }
1160 1161 1162 1163
                        | bitand_expr '&' equality_expr
                            {
                                FIXME("bitwise AND\n");
                            }
1164 1165 1166 1167 1168

bitxor_expr:              bitand_expr
                            {
                                $$ = $1;
                            }
1169 1170 1171 1172
                        | bitxor_expr '^' bitand_expr
                            {
                                FIXME("bitwise XOR\n");
                            }
1173 1174 1175 1176 1177

bitor_expr:               bitxor_expr
                            {
                                $$ = $1;
                            }
1178 1179 1180 1181
                        | bitor_expr '|' bitxor_expr
                            {
                                FIXME("bitwise OR\n");
                            }
1182 1183 1184 1185 1186

logicand_expr:            bitor_expr
                            {
                                $$ = $1;
                            }
1187 1188 1189 1190
                        | logicand_expr OP_AND bitor_expr
                            {
                                FIXME("logic AND\n");
                            }
1191 1192 1193 1194 1195

logicor_expr:             logicand_expr
                            {
                                $$ = $1;
                            }
1196 1197 1198 1199
                        | logicor_expr OP_OR logicand_expr
                            {
                                FIXME("logic OR\n");
                            }
1200 1201 1202 1203 1204

conditional_expr:         logicor_expr
                            {
                                $$ = $1;
                            }
1205 1206 1207 1208
                        | logicor_expr '?' expr ':' assignment_expr
                            {
                                FIXME("ternary operator\n");
                            }
1209 1210 1211 1212 1213

assignment_expr:          conditional_expr
                            {
                                $$ = $1;
                            }
1214 1215
                        | unary_expr assign_op assignment_expr
                            {
1216 1217 1218 1219
                                $$ = make_assignment($1, $2, BWRITERSP_WRITEMASK_ALL, $3);
                                if (!$$)
                                    return 1;
                                set_location(&$$->loc, &@2);
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
                            }

assign_op:                '='
                            {
                                $$ = ASSIGN_OP_ASSIGN;
                            }
                        | OP_ADDASSIGN
                            {
                                $$ = ASSIGN_OP_ADD;
                            }
                        | OP_SUBASSIGN
                            {
                                $$ = ASSIGN_OP_SUB;
                            }
                        | OP_MULASSIGN
                            {
                                $$ = ASSIGN_OP_MUL;
                            }
                        | OP_DIVASSIGN
                            {
                                $$ = ASSIGN_OP_DIV;
                            }
                        | OP_MODASSIGN
                            {
                                $$ = ASSIGN_OP_MOD;
                            }
                        | OP_LEFTSHIFTASSIGN
                            {
                                $$ = ASSIGN_OP_LSHIFT;
                            }
                        | OP_RIGHTSHIFTASSIGN
                            {
                                $$ = ASSIGN_OP_RSHIFT;
                            }
                        | OP_ANDASSIGN
                            {
                                $$ = ASSIGN_OP_AND;
                            }
                        | OP_ORASSIGN
                            {
                                $$ = ASSIGN_OP_OR;
                            }
                        | OP_XORASSIGN
                            {
                                $$ = ASSIGN_OP_XOR;
                            }
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275

expr:                     assignment_expr
                            {
                                $$ = $1;
                            }
                        | expr ',' assignment_expr
                            {
                                FIXME("Comma expression\n");
                            }

1276 1277
%%

1278 1279 1280 1281 1282 1283 1284
static void set_location(struct source_location *loc, const struct YYLTYPE *l)
{
    loc->file = hlsl_ctx.source_file;
    loc->line = l->first_line;
    loc->col = l->first_column;
}

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
static DWORD add_modifier(DWORD modifiers, DWORD mod, const struct YYLTYPE *loc)
{
    if (modifiers & mod)
    {
        hlsl_report_message(hlsl_ctx.source_file, loc->first_line, loc->first_column, HLSL_LEVEL_ERROR,
                "modifier '%s' already specified", debug_modifiers(mod));
        return modifiers;
    }
    if (mod & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR)
            && modifiers & (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR))
    {
        hlsl_report_message(hlsl_ctx.source_file, loc->first_line, loc->first_column, HLSL_LEVEL_ERROR,
                "more than one matrix majority keyword");
        return modifiers;
    }
    return modifiers | mod;
}

1303 1304
struct bwriter_shader *parse_hlsl(enum shader_type type, DWORD major, DWORD minor,
        const char *entrypoint, char **messages)
1305
{
1306
    struct hlsl_ir_function_decl *function;
1307 1308 1309
    struct hlsl_scope *scope, *next_scope;
    struct hlsl_type *hlsl_type, *next_type;
    struct hlsl_ir_var *var, *next_var;
1310
    unsigned int i;
1311

1312 1313
    hlsl_ctx.status = PARSE_SUCCESS;
    hlsl_ctx.messages.size = hlsl_ctx.messages.capacity = 0;
1314
    hlsl_ctx.line_no = hlsl_ctx.column = 1;
1315
    hlsl_ctx.source_file = d3dcompiler_strdup("");
1316 1317 1318 1319
    hlsl_ctx.source_files = d3dcompiler_alloc(sizeof(*hlsl_ctx.source_files));
    if (hlsl_ctx.source_files)
        hlsl_ctx.source_files[0] = hlsl_ctx.source_file;
    hlsl_ctx.source_files_count = 1;
1320
    hlsl_ctx.cur_scope = NULL;
1321
    hlsl_ctx.matrix_majority = HLSL_COLUMN_MAJOR;
1322 1323 1324 1325 1326 1327
    list_init(&hlsl_ctx.scopes);
    list_init(&hlsl_ctx.types);
    list_init(&hlsl_ctx.functions);

    push_scope(&hlsl_ctx);
    hlsl_ctx.globals = hlsl_ctx.cur_scope;
1328 1329 1330

    hlsl_parse();

1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
    if (TRACE_ON(hlsl_parser))
    {
        struct hlsl_ir_function_decl *func;

        TRACE("IR dump.\n");
        LIST_FOR_EACH_ENTRY(func, &hlsl_ctx.functions, struct hlsl_ir_function_decl, node.entry)
        {
            if (func->body)
                debug_dump_ir_function(func);
        }
    }

1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    TRACE("Compilation status = %d\n", hlsl_ctx.status);
    if (messages)
    {
        if (hlsl_ctx.messages.size)
            *messages = hlsl_ctx.messages.string;
        else
            *messages = NULL;
    }
    else
    {
        if (hlsl_ctx.messages.capacity)
            d3dcompiler_free(hlsl_ctx.messages.string);
    }

1357 1358 1359 1360
    for (i = 0; i < hlsl_ctx.source_files_count; ++i)
        d3dcompiler_free((void *)hlsl_ctx.source_files[i]);
    d3dcompiler_free(hlsl_ctx.source_files);

1361 1362 1363
    TRACE("Freeing functions IR.\n");
    LIST_FOR_EACH_ENTRY(function, &hlsl_ctx.functions, struct hlsl_ir_function_decl, node.entry)
        free_function(function);
1364

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
    TRACE("Freeing variables.\n");
    LIST_FOR_EACH_ENTRY_SAFE(scope, next_scope, &hlsl_ctx.scopes, struct hlsl_scope, entry)
    {
        LIST_FOR_EACH_ENTRY_SAFE(var, next_var, &scope->vars, struct hlsl_ir_var, scope_entry)
        {
            free_declaration(var);
        }
        d3dcompiler_free(scope);
    }

    TRACE("Freeing types.\n");
    LIST_FOR_EACH_ENTRY_SAFE(hlsl_type, next_type, &hlsl_ctx.types, struct hlsl_type, entry)
    {
        free_hlsl_type(hlsl_type);
    }

1381 1382
    return NULL;
}