mojoshader_compiler.c
author Ethan Lee <flibitijibibo@flibitijibibo.com>
Tue, 07 Jul 2020 17:44:32 -0400
changeset 1278 be0f548f321f
parent 1188 25000edc0176
permissions -rw-r--r--
spirv: Fix bogus detection of PSIZE0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
835
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     1
/**
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     3
 *  Direct3D shaders.
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     4
 *
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     6
 *
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     7
 *  This file written by Ryan C. Gordon.
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     8
 */
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     9
921
97b879e372c6 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 920
diff changeset
    10
// !!! FIXME: this needs to be split into separate source files:
97b879e372c6 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 920
diff changeset
    11
// !!! FIXME:  parse, AST, IR, etc. The problem is we need to deal with the
97b879e372c6 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 920
diff changeset
    12
// !!! FIXME:  "Context" struct being passed around everywhere.
97b879e372c6 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 920
diff changeset
    13
709
6fbd0e20b40f Removed some ANSI C things from the HLSL grammar that shaders don't do.
Ryan C. Gordon <icculus@icculus.org>
parents: 706
diff changeset
    14
#define __MOJOSHADER_INTERNAL__ 1
6fbd0e20b40f Removed some ANSI C things from the HLSL grammar that shaders don't do.
Ryan C. Gordon <icculus@icculus.org>
parents: 706
diff changeset
    15
#include "mojoshader_internal.h"
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
716
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
    17
#if DEBUG_COMPILER_PARSER
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
    18
#define LEMON_SUPPORT_TRACING 1
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
    19
#endif
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
    20
916
3738cd265707 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 915
diff changeset
    21
// !!! FIXME: I'd like to lose this. It's really inefficient. Just keep a
3738cd265707 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 915
diff changeset
    22
// !!! FIXME:  (tail) on these list structures instead?
855
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    23
#define REVERSE_LINKED_LIST(typ, head) { \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    24
    if ((head) && (head->next)) { \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    25
        typ *tmp = NULL; \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    26
        typ *tmp1 = NULL; \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    27
        while (head != NULL) { \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    28
            tmp = head; \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    29
            head = head->next; \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    30
            tmp->next = tmp1; \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    31
            tmp1 = tmp; \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    32
        } \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    33
        head = tmp; \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    34
    } \
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    35
}
575a443074af Reverse all the linked lists that we generate backwards in the parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 854
diff changeset
    36
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    37
static inline int operator_is_unary(const MOJOSHADER_astNodeType op)
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    38
{
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    39
    return ( (op > MOJOSHADER_AST_OP_START_RANGE_UNARY) &&
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    40
             (op < MOJOSHADER_AST_OP_END_RANGE_UNARY) );
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    41
} // operator_is_unary
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    42
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    43
static inline int operator_is_binary(const MOJOSHADER_astNodeType op)
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    44
{
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    45
    return ( (op > MOJOSHADER_AST_OP_START_RANGE_BINARY) &&
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    46
             (op < MOJOSHADER_AST_OP_END_RANGE_BINARY) );
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    47
} // operator_is_binary
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    48
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    49
static inline int operator_is_ternary(const MOJOSHADER_astNodeType op)
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    50
{
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    51
    return ( (op > MOJOSHADER_AST_OP_START_RANGE_TERNARY) &&
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    52
             (op < MOJOSHADER_AST_OP_END_RANGE_TERNARY) );
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    53
} // operator_is_ternary
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    54
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
    55
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    56
typedef union TokenData
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    57
{
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    58
    int64 i64;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    59
    double dbl;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    60
    const char *string;
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
    61
    const MOJOSHADER_astDataType *datatype;
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    62
} TokenData;
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    63
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    64
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
    65
// This tracks data types and variables, and notes when they enter/leave scope.
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
    66
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
    67
typedef struct SymbolScope
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
    68
{
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
    69
    const char *symbol;
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
    70
    const MOJOSHADER_astDataType *datatype;
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
    71
    int index;  // unique positive value within a function, negative if global.
1005
7ca34382b740 Make note of how many times a symbol is referenced.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
    72
    int referenced;  // non-zero if something looked for this symbol (so we know it's used).
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
    73
    struct SymbolScope *next;
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
    74
} SymbolScope;
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
    75
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
    76
typedef struct SymbolMap
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
    77
{
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
    78
    HashTable *hash;
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
    79
    SymbolScope *scope;
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
    80
} SymbolMap;
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
    81
1011
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
    82
typedef struct LoopLabels
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
    83
{
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
    84
    int start;  // loop's start label during IR build.
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
    85
    int end;    // loop's end label during IR build.
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
    86
    struct LoopLabels *prev;
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
    87
} LoopLabels;
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
    88
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
    89
// Compile state, passed around all over the place.
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
    90
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
    91
typedef struct Context
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
    92
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
    93
    int isfail;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
    94
    int out_of_memory;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
    95
    MOJOSHADER_malloc malloc;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
    96
    MOJOSHADER_free free;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
    97
    void *malloc_data;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
    98
    ErrorList *errors;
935
bef902f8b4d9 More filling in of API details. Framework is mostly complete now.
Ryan C. Gordon <icculus@icculus.org>
parents: 932
diff changeset
    99
    ErrorList *warnings;
858
d51537335896 Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents: 857
diff changeset
   100
    StringCache *strcache;
844
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   101
    const char *sourcefile;  // current source file that we're parsing.
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   102
    unsigned int sourceline; // current line in sourcefile that we're parsing.
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   103
    SymbolMap usertypes;
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   104
    SymbolMap variables;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   105
    MOJOSHADER_astNode *ast;  // Abstract Syntax Tree
932
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   106
    const char *source_profile;
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   107
    int is_func_scope; // non-zero if semantic analysis is in function scope.
1004
650ee7802725 Semantic analysis: make sure break and continue are inside loops and switches.
Ryan C. Gordon <icculus@icculus.org>
parents: 1003
diff changeset
   108
    int loop_count;
650ee7802725 Semantic analysis: make sure break and continue are inside loops and switches.
Ryan C. Gordon <icculus@icculus.org>
parents: 1003
diff changeset
   109
    int switch_count;
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   110
    int var_index;  // next variable index for current function.
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   111
    int global_var_index;  // next variable index for global scope.
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   112
    int user_func_index;  // next function index for user-defined functions.
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   113
    int intrinsic_func_index;  // next function index for intrinsic functions.
923
859fc7151d43 Fill in some default strings in the cache, so we can always compare by pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 922
diff changeset
   114
1015
87c39e720f36 Store the IR in an array.
Ryan C. Gordon <icculus@icculus.org>
parents: 1014
diff changeset
   115
    MOJOSHADER_irStatement **ir;  // intermediate representation.
1011
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   116
    int ir_label_count;  // next unused IR label index.
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   117
    int ir_temp_count;  // next unused IR temporary value index.
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   118
    int ir_end; // current function's end label during IR build.
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   119
    int ir_ret; // temp that holds current function's retval during IR build.
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   120
    LoopLabels *ir_loop;  // nested loop boundary labels during IR build.
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   121
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   122
    // Cache intrinsic types for fast lookup and consistent pointer values.
976
72f121a58548 Clean up bogus usertypes leftover from parse phase, during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 975
diff changeset
   123
    MOJOSHADER_astDataType dt_none;
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   124
    MOJOSHADER_astDataType dt_bool;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   125
    MOJOSHADER_astDataType dt_int;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   126
    MOJOSHADER_astDataType dt_uint;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   127
    MOJOSHADER_astDataType dt_float;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   128
    MOJOSHADER_astDataType dt_float_snorm;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   129
    MOJOSHADER_astDataType dt_float_unorm;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   130
    MOJOSHADER_astDataType dt_half;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   131
    MOJOSHADER_astDataType dt_double;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   132
    MOJOSHADER_astDataType dt_string;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   133
    MOJOSHADER_astDataType dt_sampler1d;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   134
    MOJOSHADER_astDataType dt_sampler2d;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   135
    MOJOSHADER_astDataType dt_sampler3d;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   136
    MOJOSHADER_astDataType dt_samplercube;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   137
    MOJOSHADER_astDataType dt_samplerstate;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   138
    MOJOSHADER_astDataType dt_samplercompstate;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   139
    MOJOSHADER_astDataType dt_buf_bool;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   140
    MOJOSHADER_astDataType dt_buf_int;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   141
    MOJOSHADER_astDataType dt_buf_uint;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   142
    MOJOSHADER_astDataType dt_buf_half;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   143
    MOJOSHADER_astDataType dt_buf_float;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   144
    MOJOSHADER_astDataType dt_buf_double;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   145
    MOJOSHADER_astDataType dt_buf_float_snorm;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   146
    MOJOSHADER_astDataType dt_buf_float_unorm;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   147
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   148
    Buffer *garbage;  // this is sort of hacky.
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   149
} Context;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   150
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   151
940
bc2a5efade5e Added a bunch of FIXMEs to accurately portray current technical debt.
Ryan C. Gordon <icculus@icculus.org>
parents: 939
diff changeset
   152
// !!! FIXME: cut and paste between every damned source file follows...
bc2a5efade5e Added a bunch of FIXMEs to accurately portray current technical debt.
Ryan C. Gordon <icculus@icculus.org>
parents: 939
diff changeset
   153
// !!! FIXME: We need to make some sort of ContextBase that applies to all
bc2a5efade5e Added a bunch of FIXMEs to accurately portray current technical debt.
Ryan C. Gordon <icculus@icculus.org>
parents: 939
diff changeset
   154
// !!! FIXME:  files and move this stuff to mojoshader_common.c ...
bc2a5efade5e Added a bunch of FIXMEs to accurately portray current technical debt.
Ryan C. Gordon <icculus@icculus.org>
parents: 939
diff changeset
   155
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   156
// Convenience functions for allocators...
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   157
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   158
static inline void out_of_memory(Context *ctx)
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   159
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   160
    ctx->isfail = ctx->out_of_memory = 1;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   161
} // out_of_memory
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   162
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   163
static inline void *Malloc(Context *ctx, const size_t len)
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   164
{
1188
25000edc0176 Move zeromalloc trickery to internal malloc/free functions
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1182
diff changeset
   165
    void *retval = ctx->malloc((int) len, ctx->malloc_data);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   166
    if (retval == NULL)
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   167
        out_of_memory(ctx);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   168
    return retval;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   169
} // Malloc
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   170
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   171
static inline char *StrDup(Context *ctx, const char *str)
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   172
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   173
    char *retval = (char *) Malloc(ctx, strlen(str) + 1);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   174
    if (retval != NULL)
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   175
        strcpy(retval, str);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   176
    return retval;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   177
} // StrDup
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   178
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   179
static inline void Free(Context *ctx, void *ptr)
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   180
{
1188
25000edc0176 Move zeromalloc trickery to internal malloc/free functions
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1182
diff changeset
   181
    ctx->free(ptr, ctx->malloc_data);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   182
} // Free
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   183
939
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   184
static void *MallocBridge(int bytes, void *data)
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   185
{
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   186
    return Malloc((Context *) data, (size_t) bytes);
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   187
} // MallocBridge
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   188
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   189
static void FreeBridge(void *ptr, void *data)
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   190
{
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   191
    Free((Context *) data, ptr);
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   192
} // FreeBridge
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   193
932
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   194
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   195
static void failf(Context *ctx, const char *fmt, ...)
842
1ea7f51250fb A little work on error handling in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 839
diff changeset
   196
{
932
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   197
    ctx->isfail = 1;
939
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   198
    if (ctx->out_of_memory)
932
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   199
        return;
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   200
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   201
    va_list ap;
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   202
    va_start(ap, fmt);
939
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   203
    errorlist_add_va(ctx->errors, ctx->sourcefile, ctx->sourceline, fmt, ap);
932
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   204
    va_end(ap);
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   205
} // failf
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   206
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   207
static inline void fail(Context *ctx, const char *reason)
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   208
{
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   209
    failf(ctx, "%s", reason);
842
1ea7f51250fb A little work on error handling in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 839
diff changeset
   210
} // fail
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   211
939
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   212
static void warnf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   213
static void warnf(Context *ctx, const char *fmt, ...)
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   214
{
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   215
    if (ctx->out_of_memory)
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   216
        return;
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   217
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   218
    va_list ap;
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   219
    va_start(ap, fmt);
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   220
    errorlist_add_va(ctx->warnings, ctx->sourcefile, ctx->sourceline, fmt, ap);
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   221
    va_end(ap);
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   222
} // warnf
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   223
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   224
static inline void warn(Context *ctx, const char *reason)
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   225
{
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   226
    warnf(ctx, "%s", reason);
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   227
} // warn
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   228
932
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   229
static inline int isfail(const Context *ctx)
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   230
{
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   231
    return ctx->isfail;
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   232
} // isfail
079c62f868eb Filled in some stubs to flesh out the AST API.
Ryan C. Gordon <icculus@icculus.org>
parents: 931
diff changeset
   233
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   234
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   235
static void symbolmap_nuke(const void *k, const void *v, void *d) {/*no-op*/}
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   236
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   237
static int create_symbolmap(Context *ctx, SymbolMap *map)
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   238
{
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   239
    // !!! FIXME: should compare string pointer, with string in cache.
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   240
    map->scope = NULL;
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   241
    map->hash = hash_create(ctx, hash_hash_string, hash_keymatch_string,
939
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   242
                            symbolmap_nuke, 1, MallocBridge, FreeBridge, ctx);
64cc93ee5a56 Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents: 937
diff changeset
   243
    return (map->hash != NULL);
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   244
} // create_symbolmap
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   245
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   246
static int datatypes_match(const MOJOSHADER_astDataType *a,
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   247
                           const MOJOSHADER_astDataType *b)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   248
{
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   249
    int i;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   250
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   251
    if (a == b)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   252
        return 1;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   253
    else if (a->type != b->type)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   254
        return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   255
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   256
    switch (a->type)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   257
    {
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   258
        case MOJOSHADER_AST_DATATYPE_STRUCT:
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   259
            if (a->structure.member_count != b->structure.member_count)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   260
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   261
            for (i = 0; i < a->structure.member_count; i++)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   262
            {
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   263
                if (!datatypes_match(a->structure.members[i].datatype,
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   264
                                     b->structure.members[i].datatype))
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   265
                    return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   266
                // stringcache'd, pointer compare is safe.
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   267
                else if (a->structure.members[i].identifier !=
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   268
                         b->structure.members[i].identifier)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   269
                    return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   270
            } // for
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   271
            return 1;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   272
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   273
        case MOJOSHADER_AST_DATATYPE_ARRAY:
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   274
            if (a->array.elements != b->array.elements)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   275
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   276
            else if (!datatypes_match(a->array.base, b->array.base))
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   277
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   278
            return 1;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   279
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   280
        case MOJOSHADER_AST_DATATYPE_VECTOR:
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   281
            if (a->vector.elements != b->vector.elements)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   282
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   283
            else if (!datatypes_match(a->vector.base, b->vector.base))
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   284
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   285
            return 1;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   286
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   287
        case MOJOSHADER_AST_DATATYPE_MATRIX:
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   288
            if (a->matrix.rows != b->matrix.rows)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   289
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   290
            else if (a->matrix.columns != b->matrix.columns)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   291
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   292
            else if (!datatypes_match(a->matrix.base, b->matrix.base))
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   293
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   294
            return 1;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   295
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   296
        case MOJOSHADER_AST_DATATYPE_BUFFER:
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   297
            return datatypes_match(a->buffer.base, b->buffer.base);
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   298
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   299
        case MOJOSHADER_AST_DATATYPE_FUNCTION:
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   300
            if (a->function.num_params != b->function.num_params)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   301
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   302
            else if (a->function.intrinsic != b->function.intrinsic)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   303
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   304
            else if (!datatypes_match(a->function.retval, b->function.retval))
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   305
                return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   306
            for (i = 0; i < a->function.num_params; i++)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   307
            {
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   308
                if (!datatypes_match(a->function.params[i], b->function.params[i]))
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   309
                    return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   310
            } // for
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   311
            return 1;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   312
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   313
        case MOJOSHADER_AST_DATATYPE_USER:
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   314
            return 0;  // pointers must match, this clearly didn't.
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   315
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   316
        default:
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   317
            assert(0 && "unexpected case");
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   318
            return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   319
    } // switch
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   320
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   321
    return 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   322
} // datatypes_match
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   323
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   324
static void push_symbol(Context *ctx, SymbolMap *map, const char *sym,
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   325
                        const MOJOSHADER_astDataType *dt, const int index,
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   326
                        const int check_dupes)
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   327
{
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   328
    if (ctx->out_of_memory)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   329
        return;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   330
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   331
    // Decide if this symbol is defined, and if it's in the current scope.
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   332
    SymbolScope *item = NULL;
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   333
    const void *value = NULL;
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   334
    if ((check_dupes) && (sym != NULL) && (hash_find(map->hash, sym, &value)))
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   335
    {
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   336
        // check the current scope for a dupe.
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   337
        // !!! FIXME: note current scope's starting index, see if found
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   338
        // !!! FIXME:  item is < index (and thus, a previous scope).
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   339
        item = map->scope;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   340
        while ((item) && (item->symbol))
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   341
        {
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   342
            if ( ((const void *) item) == value )
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   343
            {
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   344
                failf(ctx, "Symbol '%s' already defined", sym);
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   345
                return;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   346
            } // if
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   347
            item = item->next;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   348
        } // while
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   349
    } // if
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   350
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   351
    // Add the symbol to our map and scope stack.
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   352
    item = (SymbolScope *) Malloc(ctx, sizeof (SymbolScope));
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   353
    if (item == NULL)
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   354
        return;
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   355
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   356
    if (sym != NULL)  // sym can be NULL if we're pushing a new scope.
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   357
    {
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   358
        if (hash_insert(map->hash, sym, item) == -1)
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   359
        {
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   360
            Free(ctx, item);
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   361
            return;
929
5d2d66bd35e7 First shot at semantic analysis (take the AST and type check, make sane, etc).
Ryan C. Gordon <icculus@icculus.org>
parents: 928
diff changeset
   362
        } // if
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   363
    } // if
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   364
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   365
    item->symbol = sym;  // cached strings, don't copy.
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   366
    item->index = index;
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   367
    item->datatype = dt;
1005
7ca34382b740 Make note of how many times a symbol is referenced.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
   368
    item->referenced = 0;
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   369
    item->next = map->scope;
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   370
    map->scope = item;
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   371
} // push_symbol
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   372
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   373
static void push_usertype(Context *ctx, const char *sym, const MOJOSHADER_astDataType *dt)
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   374
{
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   375
    if (sym != NULL)
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   376
    {
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   377
        MOJOSHADER_astDataType *userdt;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   378
        userdt = (MOJOSHADER_astDataType *) Malloc(ctx, sizeof (*userdt));
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   379
        if (userdt != NULL)
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   380
        {
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   381
            // !!! FIXME: this is hacky.
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   382
            if (!buffer_append(ctx->garbage, &userdt, sizeof (userdt)))
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   383
            {
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   384
                Free(ctx, userdt);
1060
4cdf5a3ceb03 Fixed a few things clang's static analysis reported.
Ryan C. Gordon <icculus@icculus.org>
parents: 1028
diff changeset
   385
                return;
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   386
            } // if
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   387
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   388
            userdt->type = MOJOSHADER_AST_DATATYPE_USER;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   389
            userdt->user.details = dt;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   390
            userdt->user.name = sym;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   391
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   392
            dt = userdt;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   393
        } // if
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   394
    } // if
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   395
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   396
    push_symbol(ctx, &ctx->usertypes, sym, dt, 0, 1);
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   397
} // push_usertype
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   398
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   399
static inline void push_variable(Context *ctx, const char *sym, const MOJOSHADER_astDataType *dt)
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   400
{
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   401
    int idx = 0;
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   402
    if (sym != NULL)
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   403
    {
1011
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   404
        // leave space for individual member indexes. The IR will need this.
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   405
        int additional = 0;
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   406
        if (dt->type == MOJOSHADER_AST_DATATYPE_STRUCT)
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   407
            additional = dt->structure.member_count;
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   408
        if (ctx->is_func_scope)
1011
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   409
        {
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   410
            idx = ++ctx->var_index;  // these are positive.
1011
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   411
            ctx->var_index += additional;
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   412
        } // if
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   413
        else
1011
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   414
        {
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   415
            idx = --ctx->global_var_index;  // these are negative.
1011
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   416
            ctx->global_var_index -= additional;
0758e03a79d9 Implemented the Intermediate Representation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1010
diff changeset
   417
        } // else
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   418
    } // if
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   419
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   420
    push_symbol(ctx, &ctx->variables, sym, dt, idx, 1);
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   421
} // push_variable
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   422
1014
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   423
static int push_function(Context *ctx, const char *sym,
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   424
                          const MOJOSHADER_astDataType *dt,
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   425
                          const int just_declare)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   426
{
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   427
    // we don't have any reason to support nested functions at the moment,
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   428
    //  so this would be a bug.
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   429
    assert(!ctx->is_func_scope);
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   430
    assert(dt->type == MOJOSHADER_AST_DATATYPE_FUNCTION);
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   431
1014
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   432
    // Functions are always global, so no need to search scopes.
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   433
    //  Functions overload, though, so we have to continue iterating to
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   434
    //  see if it matches anything.
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   435
    const void *value = NULL;
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   436
    void *iter = NULL;
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   437
    while (hash_iter(ctx->variables.hash, sym, &value, &iter))
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   438
    {
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   439
        // !!! FIXME: this breaks if you predeclare a function.
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   440
        // !!! FIXME:  (a declare AFTER defining works, though.)
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   441
        // there's already something called this.
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   442
        SymbolScope *item = (SymbolScope *) value;
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   443
        if (datatypes_match(dt, item->datatype))
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   444
        {
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   445
            if (!just_declare)
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   446
                failf(ctx, "Function '%s' already defined.", sym);
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   447
            return item->index;
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   448
        } // if
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   449
    } // while
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   450
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   451
    int idx = 0;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   452
    if ((sym != NULL) && (dt != NULL))
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   453
    {
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   454
        if (!dt->function.intrinsic)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   455
            idx = ++ctx->user_func_index;  // these are positive.
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   456
        else
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   457
            idx = --ctx->intrinsic_func_index;  // these are negative.
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   458
    } // if
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   459
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   460
    // push_symbol() doesn't check dupes, because we just did.
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   461
    push_symbol(ctx, &ctx->variables, sym, dt, idx, 0);
1014
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   462
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   463
    return idx;
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   464
} // push_function
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   465
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   466
static inline void push_scope(Context *ctx)
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   467
{
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   468
    push_usertype(ctx, NULL, NULL);
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   469
    push_variable(ctx, NULL, NULL);
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   470
} // push_scope
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   471
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   472
static void pop_symbol(Context *ctx, SymbolMap *map)
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   473
{
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   474
    SymbolScope *item = map->scope;
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   475
    if (!item)
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   476
        return;
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   477
    if (item->symbol)
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   478
        hash_remove(map->hash, item->symbol);
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   479
    map->scope = item->next;
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   480
    Free(ctx, item);
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   481
} // pop_symbol
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   482
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   483
static void pop_symbol_scope(Context *ctx, SymbolMap *map)
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   484
{
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   485
    while ((map->scope) && (map->scope->symbol))
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   486
        pop_symbol(ctx, map);
850
473108b0366d Fixed pop_scope().
Ryan C. Gordon <icculus@icculus.org>
parents: 849
diff changeset
   487
473108b0366d Fixed pop_scope().
Ryan C. Gordon <icculus@icculus.org>
parents: 849
diff changeset
   488
    assert(map->scope != NULL);
473108b0366d Fixed pop_scope().
Ryan C. Gordon <icculus@icculus.org>
parents: 849
diff changeset
   489
    assert(map->scope->symbol == NULL);
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   490
    pop_symbol(ctx, map);
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   491
} // pop_symbol_scope
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   492
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   493
static inline void pop_scope(Context *ctx)
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   494
{
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   495
    pop_symbol_scope(ctx, &ctx->usertypes);
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   496
    pop_symbol_scope(ctx, &ctx->variables);
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   497
} // push_scope
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   498
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   499
static const MOJOSHADER_astDataType *find_symbol(Context *ctx, SymbolMap *map, const char *sym, int *_index)
929
5d2d66bd35e7 First shot at semantic analysis (take the AST and type check, make sane, etc).
Ryan C. Gordon <icculus@icculus.org>
parents: 928
diff changeset
   500
{
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   501
    const void *_item = NULL;
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   502
    hash_find(map->hash, sym, &_item);
967
51ce9d8a7533 Fixed incorrect variable reference.
Ryan C. Gordon <icculus@icculus.org>
parents: 966
diff changeset
   503
    SymbolScope *item = (SymbolScope *) _item;
1005
7ca34382b740 Make note of how many times a symbol is referenced.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
   504
    if (item != NULL)
7ca34382b740 Make note of how many times a symbol is referenced.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
   505
    {
7ca34382b740 Make note of how many times a symbol is referenced.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
   506
        item->referenced++;
7ca34382b740 Make note of how many times a symbol is referenced.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
   507
        if (_index != NULL)
7ca34382b740 Make note of how many times a symbol is referenced.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
   508
            *_index = item->index;
7ca34382b740 Make note of how many times a symbol is referenced.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
   509
    } // if
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   510
    return item ? item->datatype : NULL;
929
5d2d66bd35e7 First shot at semantic analysis (take the AST and type check, make sane, etc).
Ryan C. Gordon <icculus@icculus.org>
parents: 928
diff changeset
   511
} // find_symbol
5d2d66bd35e7 First shot at semantic analysis (take the AST and type check, make sane, etc).
Ryan C. Gordon <icculus@icculus.org>
parents: 928
diff changeset
   512
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   513
static inline const MOJOSHADER_astDataType *find_usertype(Context *ctx, const char *sym)
929
5d2d66bd35e7 First shot at semantic analysis (take the AST and type check, make sane, etc).
Ryan C. Gordon <icculus@icculus.org>
parents: 928
diff changeset
   514
{
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   515
    return find_symbol(ctx, &ctx->usertypes, sym, NULL);
929
5d2d66bd35e7 First shot at semantic analysis (take the AST and type check, make sane, etc).
Ryan C. Gordon <icculus@icculus.org>
parents: 928
diff changeset
   516
} // find_usertype
5d2d66bd35e7 First shot at semantic analysis (take the AST and type check, make sane, etc).
Ryan C. Gordon <icculus@icculus.org>
parents: 928
diff changeset
   517
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   518
static inline const MOJOSHADER_astDataType *find_variable(Context *ctx, const char *sym, int *_index)
929
5d2d66bd35e7 First shot at semantic analysis (take the AST and type check, make sane, etc).
Ryan C. Gordon <icculus@icculus.org>
parents: 928
diff changeset
   519
{
966
9dd4107eed42 Semantic analysis now tries to assign a unique id to every variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 964
diff changeset
   520
    return find_symbol(ctx, &ctx->variables, sym, _index);
929
5d2d66bd35e7 First shot at semantic analysis (take the AST and type check, make sane, etc).
Ryan C. Gordon <icculus@icculus.org>
parents: 928
diff changeset
   521
} // find_variable
5d2d66bd35e7 First shot at semantic analysis (take the AST and type check, make sane, etc).
Ryan C. Gordon <icculus@icculus.org>
parents: 928
diff changeset
   522
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   523
static void destroy_symbolmap(Context *ctx, SymbolMap *map)
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   524
{
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   525
    while (map->scope)
937
94e50ba3f956 Workaround for failing parse for now.
Ryan C. Gordon <icculus@icculus.org>
parents: 936
diff changeset
   526
        pop_symbol(ctx, map);
922
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   527
    hash_destroy(map->hash);
9b5879d36f36 Cleaned up UserTypeMap into a generic symbol tracker.
Ryan C. Gordon <icculus@icculus.org>
parents: 921
diff changeset
   528
} // destroy_symbolmap
846
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   529
10eb8be2c919 Added better (?) USERTYPE management.
Ryan C. Gordon <icculus@icculus.org>
parents: 845
diff changeset
   530
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   531
static const MOJOSHADER_astDataType *new_datatype_vector(Context *ctx,
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   532
                                            const MOJOSHADER_astDataType *dt,
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   533
                                            const int columns)
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   534
{
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   535
    MOJOSHADER_astDataType *retval;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   536
    retval = (MOJOSHADER_astDataType *) Malloc(ctx, sizeof (*retval));
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   537
    if (retval == NULL)
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   538
        return NULL;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   539
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   540
    // !!! FIXME: this is hacky.
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   541
    // !!! FIXME:  I'd like to cache these anyhow and reuse types.
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   542
    if (!buffer_append(ctx->garbage, &retval, sizeof (retval)))
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   543
    {
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   544
        Free(ctx, retval);
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   545
        return NULL;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   546
    } // if
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   547
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   548
    if ((columns < 1) || (columns > 4))
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   549
        fail(ctx, "Vector must have between 1 and 4 elements");
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   550
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   551
    retval->type = MOJOSHADER_AST_DATATYPE_VECTOR;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   552
    retval->vector.base = dt;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   553
    retval->vector.elements = columns;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   554
    return retval;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   555
} // new_datatype_vector
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   556
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   557
static const MOJOSHADER_astDataType *new_datatype_matrix(Context *ctx,
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   558
                                            const MOJOSHADER_astDataType *dt,
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   559
                                            const int rows, const int columns)
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   560
{
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   561
    MOJOSHADER_astDataType *retval;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   562
    // !!! FIXME: allocate enough for a matrix, but we need to cleanup things that copy without checking for subsize.
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   563
    retval = (MOJOSHADER_astDataType *) Malloc(ctx, sizeof (*retval));
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   564
    if (retval == NULL)
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   565
        return NULL;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   566
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   567
    // !!! FIXME: this is hacky.
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   568
    // !!! FIXME:  I'd like to cache these anyhow and reuse types.
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   569
    if (!buffer_append(ctx->garbage, &retval, sizeof (retval)))
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   570
    {
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   571
        Free(ctx, retval);
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   572
        return NULL;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   573
    } // if
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   574
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   575
    if ((rows < 1) || (rows > 4))
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   576
        fail(ctx, "Matrix must have between 1 and 4 rows");
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   577
    if ((columns < 1) || (columns > 4))
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   578
        fail(ctx, "Matrix must have between 1 and 4 columns");
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   579
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   580
    retval->type = MOJOSHADER_AST_DATATYPE_MATRIX;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   581
    retval->matrix.base = dt;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   582
    retval->matrix.rows = rows;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   583
    retval->matrix.columns = columns;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   584
    return retval;
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   585
} // new_datatype_matrix
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   586
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   587
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   588
// !!! FIXME: move this to mojoshader_ast.c
940
bc2a5efade5e Added a bunch of FIXMEs to accurately portray current technical debt.
Ryan C. Gordon <icculus@icculus.org>
parents: 939
diff changeset
   589
// !!! FIXME: new_* and delete_* should take an allocator, not a context.
bc2a5efade5e Added a bunch of FIXMEs to accurately portray current technical debt.
Ryan C. Gordon <icculus@icculus.org>
parents: 939
diff changeset
   590
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   591
// These functions are mostly for construction and cleanup of nodes in the
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   592
//  parse tree. Mostly this is simple allocation and initialization, so we
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   593
//  can do as little in the lemon code as possible, and then sort it all out
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   594
//  afterwards.
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   595
844
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   596
#define NEW_AST_NODE(retval, cls, typ) \
883
df6265a993bc Fixed C++ compiler error.
Ryan C. Gordon <icculus@icculus.org>
parents: 868
diff changeset
   597
    cls *retval = (cls *) Malloc(ctx, sizeof (cls)); \
844
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   598
    do { \
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   599
        if (retval == NULL) { return NULL; } \
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   600
        retval->ast.type = typ; \
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   601
        retval->ast.filename = ctx->sourcefile; \
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   602
        retval->ast.line = ctx->sourceline; \
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   603
    } while (0)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   604
844
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   605
#define DELETE_AST_NODE(cls) do { \
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   606
    if (!cls) return; \
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   607
} while (0)
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   608
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   609
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   610
static void delete_compilation_unit(Context*, MOJOSHADER_astCompilationUnit*);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   611
static void delete_statement(Context *ctx, MOJOSHADER_astStatement *stmt);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   612
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   613
static MOJOSHADER_astExpression *new_identifier_expr(Context *ctx,
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   614
                                                     const char *string)
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   615
{
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   616
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionIdentifier,
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   617
                 MOJOSHADER_AST_OP_IDENTIFIER);
986
805aa6eb75af Fixed some uninitialized memory reads, compliments of valgrind.
Ryan C. Gordon <icculus@icculus.org>
parents: 985
diff changeset
   618
    retval->datatype = NULL;
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   619
    retval->identifier = string;  // cached; don't copy string.
986
805aa6eb75af Fixed some uninitialized memory reads, compliments of valgrind.
Ryan C. Gordon <icculus@icculus.org>
parents: 985
diff changeset
   620
    retval->index = 0;
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   621
    return (MOJOSHADER_astExpression *) retval;
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   622
} // new_identifier_expr
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   623
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   624
static MOJOSHADER_astExpression *new_callfunc_expr(Context *ctx,
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   625
                                        const char *identifier,
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   626
                                        MOJOSHADER_astArguments *args)
927
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   627
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   628
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionCallFunction,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   629
                 MOJOSHADER_AST_OP_CALLFUNC);
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   630
    MOJOSHADER_astExpression *expr = new_identifier_expr(ctx, identifier);
986
805aa6eb75af Fixed some uninitialized memory reads, compliments of valgrind.
Ryan C. Gordon <icculus@icculus.org>
parents: 985
diff changeset
   631
    retval->datatype = NULL;
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   632
    retval->identifier = (MOJOSHADER_astExpressionIdentifier *) expr;
927
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   633
    retval->args = args;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   634
    return (MOJOSHADER_astExpression *) retval;
927
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   635
} // new_callfunc_expr
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   636
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   637
static MOJOSHADER_astExpression *new_constructor_expr(Context *ctx,
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   638
                                            const MOJOSHADER_astDataType *dt,
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   639
                                            MOJOSHADER_astArguments *args)
837
5f6528602658 Uncommented some grammar bits that got masked out in the calculator experiment.
Ryan C. Gordon <icculus@icculus.org>
parents: 836
diff changeset
   640
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   641
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionConstructor,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   642
                 MOJOSHADER_AST_OP_CONSTRUCTOR);
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   643
    retval->datatype = dt;
837
5f6528602658 Uncommented some grammar bits that got masked out in the calculator experiment.
Ryan C. Gordon <icculus@icculus.org>
parents: 836
diff changeset
   644
    retval->args = args;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   645
    return (MOJOSHADER_astExpression *) retval;
837
5f6528602658 Uncommented some grammar bits that got masked out in the calculator experiment.
Ryan C. Gordon <icculus@icculus.org>
parents: 836
diff changeset
   646
} // new_constructor_expr
5f6528602658 Uncommented some grammar bits that got masked out in the calculator experiment.
Ryan C. Gordon <icculus@icculus.org>
parents: 836
diff changeset
   647
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   648
static MOJOSHADER_astExpression *new_cast_expr(Context *ctx,
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   649
                                            const MOJOSHADER_astDataType *dt,
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   650
                                            MOJOSHADER_astExpression *operand)
837
5f6528602658 Uncommented some grammar bits that got masked out in the calculator experiment.
Ryan C. Gordon <icculus@icculus.org>
parents: 836
diff changeset
   651
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   652
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionCast, MOJOSHADER_AST_OP_CAST);
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   653
    retval->datatype = dt;
837
5f6528602658 Uncommented some grammar bits that got masked out in the calculator experiment.
Ryan C. Gordon <icculus@icculus.org>
parents: 836
diff changeset
   654
    retval->operand = operand;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   655
    return (MOJOSHADER_astExpression *) retval;
837
5f6528602658 Uncommented some grammar bits that got masked out in the calculator experiment.
Ryan C. Gordon <icculus@icculus.org>
parents: 836
diff changeset
   656
} // new_cast_expr
5f6528602658 Uncommented some grammar bits that got masked out in the calculator experiment.
Ryan C. Gordon <icculus@icculus.org>
parents: 836
diff changeset
   657
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   658
static MOJOSHADER_astExpression *new_unary_expr(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   659
                                            const MOJOSHADER_astNodeType op,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   660
                                            MOJOSHADER_astExpression *operand)
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   661
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   662
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionUnary, op);
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   663
    assert(operator_is_unary(op));
986
805aa6eb75af Fixed some uninitialized memory reads, compliments of valgrind.
Ryan C. Gordon <icculus@icculus.org>
parents: 985
diff changeset
   664
    retval->datatype = NULL;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   665
    retval->operand = operand;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   666
    return (MOJOSHADER_astExpression *) retval;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   667
} // new_unary_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   668
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   669
static MOJOSHADER_astExpression *new_binary_expr(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   670
                                            const MOJOSHADER_astNodeType op,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   671
                                            MOJOSHADER_astExpression *left,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   672
                                            MOJOSHADER_astExpression *right)
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   673
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   674
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionBinary, op);
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   675
    assert(operator_is_binary(op));
986
805aa6eb75af Fixed some uninitialized memory reads, compliments of valgrind.
Ryan C. Gordon <icculus@icculus.org>
parents: 985
diff changeset
   676
    retval->datatype = NULL;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   677
    retval->left = left;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   678
    retval->right = right;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   679
    return (MOJOSHADER_astExpression *) retval;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   680
} // new_binary_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   681
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   682
static MOJOSHADER_astExpression *new_ternary_expr(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   683
                                            const MOJOSHADER_astNodeType op,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   684
                                            MOJOSHADER_astExpression *left,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   685
                                            MOJOSHADER_astExpression *center,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   686
                                            MOJOSHADER_astExpression *right)
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   687
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   688
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionTernary, op);
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   689
    assert(operator_is_ternary(op));
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   690
    assert(op == MOJOSHADER_AST_OP_CONDITIONAL);
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   691
    retval->datatype = &ctx->dt_bool;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   692
    retval->left = left;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   693
    retval->center = center;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   694
    retval->right = right;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   695
    return (MOJOSHADER_astExpression *) retval;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   696
} // new_ternary_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   697
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   698
static MOJOSHADER_astExpression *new_deref_struct_expr(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   699
                                        MOJOSHADER_astExpression *identifier,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   700
                                        const char *member)
928
c9b0235e9d23 Made struct dereference a separate expression type, not a binary expression.
Ryan C. Gordon <icculus@icculus.org>
parents: 927
diff changeset
   701
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   702
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionDerefStruct,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   703
                 MOJOSHADER_AST_OP_DEREF_STRUCT);
986
805aa6eb75af Fixed some uninitialized memory reads, compliments of valgrind.
Ryan C. Gordon <icculus@icculus.org>
parents: 985
diff changeset
   704
    retval->datatype = NULL;
928
c9b0235e9d23 Made struct dereference a separate expression type, not a binary expression.
Ryan C. Gordon <icculus@icculus.org>
parents: 927
diff changeset
   705
    retval->identifier = identifier;
c9b0235e9d23 Made struct dereference a separate expression type, not a binary expression.
Ryan C. Gordon <icculus@icculus.org>
parents: 927
diff changeset
   706
    retval->member = member;  // cached; don't copy string.
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   707
    retval->isswizzle = 0;  // may change during semantic analysis.
1009
ba0ed22a4bac Note the member index in struct deref, so we don't have to calculate it again.
Ryan C. Gordon <icculus@icculus.org>
parents: 1007
diff changeset
   708
    retval->member_index = 0;  // set during semantic analysis.
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   709
    return (MOJOSHADER_astExpression *) retval;
928
c9b0235e9d23 Made struct dereference a separate expression type, not a binary expression.
Ryan C. Gordon <icculus@icculus.org>
parents: 927
diff changeset
   710
} // new_deref_struct_expr
c9b0235e9d23 Made struct dereference a separate expression type, not a binary expression.
Ryan C. Gordon <icculus@icculus.org>
parents: 927
diff changeset
   711
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   712
static MOJOSHADER_astExpression *new_literal_int_expr(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   713
                                                       const int value)
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   714
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   715
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionIntLiteral,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   716
                 MOJOSHADER_AST_OP_INT_LITERAL);
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   717
    retval->datatype = &ctx->dt_int;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   718
    retval->value = value;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   719
    return (MOJOSHADER_astExpression *) retval;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   720
} // new_literal_int_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   721
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   722
static MOJOSHADER_astExpression *new_literal_float_expr(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   723
                                                        const double dbl)
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   724
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   725
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionFloatLiteral,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   726
                 MOJOSHADER_AST_OP_FLOAT_LITERAL);
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   727
    retval->datatype = &ctx->dt_float;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   728
    retval->value = dbl;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   729
    return (MOJOSHADER_astExpression *) retval;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   730
} // new_literal_float_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   731
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   732
static MOJOSHADER_astExpression *new_literal_string_expr(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   733
                                                         const char *string)
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   734
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   735
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionStringLiteral,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   736
                 MOJOSHADER_AST_OP_STRING_LITERAL);
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   737
    retval->datatype = &ctx->dt_string;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   738
    retval->string = string;  // cached; don't copy string.
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   739
    return (MOJOSHADER_astExpression *) retval;
924
c99418745e1b Added boolean literals to parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 923
diff changeset
   740
} // new_literal_string_expr
c99418745e1b Added boolean literals to parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 923
diff changeset
   741
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   742
static MOJOSHADER_astExpression *new_literal_boolean_expr(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   743
                                                          const int value)
924
c99418745e1b Added boolean literals to parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 923
diff changeset
   744
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   745
    NEW_AST_NODE(retval, MOJOSHADER_astExpressionBooleanLiteral,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   746
                 MOJOSHADER_AST_OP_BOOLEAN_LITERAL);
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   747
    retval->datatype = &ctx->dt_bool;
924
c99418745e1b Added boolean literals to parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 923
diff changeset
   748
    retval->value = value;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   749
    return (MOJOSHADER_astExpression *) retval;
924
c99418745e1b Added boolean literals to parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 923
diff changeset
   750
} // new_literal_boolean_expr
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   751
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   752
static void delete_arguments(Context *ctx, MOJOSHADER_astArguments *args);
927
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   753
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   754
static void delete_expr(Context *ctx, MOJOSHADER_astExpression *_expr)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   755
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   756
    MOJOSHADER_astNode *expr = (MOJOSHADER_astNode *) _expr;
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   757
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   758
    DELETE_AST_NODE(expr);
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   759
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   760
    if (expr->ast.type == MOJOSHADER_AST_OP_CAST)
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   761
        delete_expr(ctx, expr->cast.operand);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   762
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   763
    else if (expr->ast.type == MOJOSHADER_AST_OP_CONSTRUCTOR)
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   764
        delete_arguments(ctx, expr->constructor.args);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   765
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   766
    else if (expr->ast.type == MOJOSHADER_AST_OP_DEREF_STRUCT)
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   767
        delete_expr(ctx, expr->derefstruct.identifier);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   768
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   769
    else if (operator_is_unary(expr->ast.type))
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   770
        delete_expr(ctx, expr->unary.operand);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   771
844
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   772
    else if (operator_is_binary(expr->ast.type))
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   773
    {
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   774
        delete_expr(ctx, expr->binary.left);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   775
        delete_expr(ctx, expr->binary.right);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   776
    } // else if
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   777
844
06494572317b Make AST nodes a little more generic, add source position info to them.
Ryan C. Gordon <icculus@icculus.org>
parents: 843
diff changeset
   778
    else if (operator_is_ternary(expr->ast.type))
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   779
    {
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   780
        delete_expr(ctx, expr->ternary.left);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   781
        delete_expr(ctx, expr->ternary.center);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   782
        delete_expr(ctx, expr->ternary.right);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   783
    } // else if
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   784
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   785
    else if (expr->ast.type == MOJOSHADER_AST_OP_CALLFUNC)
837
5f6528602658 Uncommented some grammar bits that got masked out in the calculator experiment.
Ryan C. Gordon <icculus@icculus.org>
parents: 836
diff changeset
   786
    {
973
6d4cdbc21301 Cleaned up the mess of function call processing.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   787
        delete_expr(ctx, (MOJOSHADER_astExpression*)expr->callfunc.identifier);
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   788
        delete_arguments(ctx, expr->callfunc.args);
837
5f6528602658 Uncommented some grammar bits that got masked out in the calculator experiment.
Ryan C. Gordon <icculus@icculus.org>
parents: 836
diff changeset
   789
    } // else if
845
d10d31b9b695 Removed incorrect assertion.
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   790
d10d31b9b695 Removed incorrect assertion.
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   791
    // rest of operators don't have extra data to free.
d10d31b9b695 Removed incorrect assertion.
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   792
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   793
    Free(ctx, expr);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   794
} // delete_expr
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   795
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   796
static MOJOSHADER_astArguments *new_argument(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   797
                                             MOJOSHADER_astExpression *arg)
927
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   798
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   799
    NEW_AST_NODE(retval, MOJOSHADER_astArguments, MOJOSHADER_AST_ARGUMENTS);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   800
    retval->argument = arg;
927
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   801
    retval->next = NULL;
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   802
    return retval;
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   803
} // new_argument
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   804
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   805
static void delete_arguments(Context *ctx, MOJOSHADER_astArguments *args)
927
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   806
{
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   807
    DELETE_AST_NODE(args);
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   808
    delete_arguments(ctx, args->next);
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   809
    delete_expr(ctx, args->argument);
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   810
    Free(ctx, args);
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   811
} // delete_arguments
0e8b7f053a45 Reworked AST to not use AST_OP_COMMA for function/constructor argument lists.
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   812
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   813
static MOJOSHADER_astFunctionParameters *new_function_param(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   814
                        const MOJOSHADER_astInputModifier inputmod,
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   815
                        const MOJOSHADER_astDataType *dt,
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   816
                        const char *identifier, const char *semantic,
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   817
                        const MOJOSHADER_astInterpolationModifier interpmod,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   818
                        MOJOSHADER_astExpression *initializer)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   819
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   820
    NEW_AST_NODE(retval, MOJOSHADER_astFunctionParameters,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   821
                 MOJOSHADER_AST_FUNCTION_PARAMS);
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   822
    retval->datatype = dt;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   823
    retval->input_modifier = inputmod;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   824
    retval->identifier = identifier;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   825
    retval->semantic = semantic;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   826
    retval->interpolation_modifier = interpmod;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   827
    retval->initializer = initializer;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   828
    retval->next = NULL;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   829
    return retval;
926
8ac335735b37 Renamed function "arguments" to function "parameters" in the AST.
Ryan C. Gordon <icculus@icculus.org>
parents: 925
diff changeset
   830
} // new_function_param
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   831
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   832
static void delete_function_params(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   833
                                   MOJOSHADER_astFunctionParameters *params)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   834
{
926
8ac335735b37 Renamed function "arguments" to function "parameters" in the AST.
Ryan C. Gordon <icculus@icculus.org>
parents: 925
diff changeset
   835
    DELETE_AST_NODE(params);
8ac335735b37 Renamed function "arguments" to function "parameters" in the AST.
Ryan C. Gordon <icculus@icculus.org>
parents: 925
diff changeset
   836
    delete_function_params(ctx, params->next);
8ac335735b37 Renamed function "arguments" to function "parameters" in the AST.
Ryan C. Gordon <icculus@icculus.org>
parents: 925
diff changeset
   837
    delete_expr(ctx, params->initializer);
8ac335735b37 Renamed function "arguments" to function "parameters" in the AST.
Ryan C. Gordon <icculus@icculus.org>
parents: 925
diff changeset
   838
    Free(ctx, params);
8ac335735b37 Renamed function "arguments" to function "parameters" in the AST.
Ryan C. Gordon <icculus@icculus.org>
parents: 925
diff changeset
   839
} // delete_function_params
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   840
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   841
static MOJOSHADER_astFunctionSignature *new_function_signature(Context *ctx,
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   842
                                    const MOJOSHADER_astDataType *dt,
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   843
                                    const char *identifier,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   844
                                    MOJOSHADER_astFunctionParameters *params)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   845
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   846
    NEW_AST_NODE(retval, MOJOSHADER_astFunctionSignature,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   847
                 MOJOSHADER_AST_FUNCTION_SIGNATURE);
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   848
    retval->datatype = dt;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   849
    retval->identifier = identifier;
926
8ac335735b37 Renamed function "arguments" to function "parameters" in the AST.
Ryan C. Gordon <icculus@icculus.org>
parents: 925
diff changeset
   850
    retval->params = params;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   851
    retval->storage_class = MOJOSHADER_AST_FNSTORECLS_NONE;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   852
    retval->semantic = NULL;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   853
    return retval;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   854
} // new_function_signature
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   855
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   856
static void delete_function_signature(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   857
                                      MOJOSHADER_astFunctionSignature *sig)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   858
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   859
    DELETE_AST_NODE(sig);
926
8ac335735b37 Renamed function "arguments" to function "parameters" in the AST.
Ryan C. Gordon <icculus@icculus.org>
parents: 925
diff changeset
   860
    delete_function_params(ctx, sig->params);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   861
    Free(ctx, sig);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   862
} // delete_function_signature
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   863
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   864
static MOJOSHADER_astCompilationUnit *new_function(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   865
                                MOJOSHADER_astFunctionSignature *declaration,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   866
                                MOJOSHADER_astStatement *definition)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   867
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   868
    NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitFunction,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   869
                 MOJOSHADER_AST_COMPUNIT_FUNCTION);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   870
    retval->next = NULL;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   871
    retval->declaration = declaration;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   872
    retval->definition = definition;
1014
7db1c2dfead6 Store the unique id from push_function() in the AST during semantic analysis.
Ryan C. Gordon <icculus@icculus.org>
parents: 1013
diff changeset
   873
    retval->index = 0;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   874
    return (MOJOSHADER_astCompilationUnit *) retval;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   875
} // new_function
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   876
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   877
static void delete_function(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   878
                            MOJOSHADER_astCompilationUnitFunction *unitfn)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   879
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   880
    DELETE_AST_NODE(unitfn);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   881
    delete_compilation_unit(ctx, unitfn->next);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   882
    delete_function_signature(ctx, unitfn->declaration);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   883
    delete_statement(ctx, unitfn->definition);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   884
    Free(ctx, unitfn);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   885
} // delete_function
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   886
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   887
static MOJOSHADER_astScalarOrArray *new_scalar_or_array(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   888
                                          const char *ident, const int isvec,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   889
                                          MOJOSHADER_astExpression *dim)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   890
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   891
    NEW_AST_NODE(retval, MOJOSHADER_astScalarOrArray,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   892
                 MOJOSHADER_AST_SCALAR_OR_ARRAY);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   893
    retval->identifier = ident;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   894
    retval->isarray = isvec;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   895
    retval->dimension = dim;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   896
    return retval;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   897
} // new_scalar_or_array
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   898
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   899
static void delete_scalar_or_array(Context *ctx,MOJOSHADER_astScalarOrArray *s)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   900
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   901
    DELETE_AST_NODE(s);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   902
    delete_expr(ctx, s->dimension);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   903
    Free(ctx, s);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   904
} // delete_scalar_or_array
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   905
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   906
static MOJOSHADER_astTypedef *new_typedef(Context *ctx, const int isconst,
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   907
                                          const MOJOSHADER_astDataType *dt,
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   908
                                          MOJOSHADER_astScalarOrArray *soa)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   909
{
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   910
    // we correct this datatype to the final version during semantic analysis.
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   911
    NEW_AST_NODE(retval, MOJOSHADER_astTypedef, MOJOSHADER_AST_TYPEDEF);
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   912
    retval->datatype = dt;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   913
    retval->isconst = isconst;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   914
    retval->details = soa;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   915
    return retval;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   916
} // new_typedef
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   917
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   918
static void delete_typedef(Context *ctx, MOJOSHADER_astTypedef *td)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   919
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   920
    DELETE_AST_NODE(td);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   921
    delete_scalar_or_array(ctx, td->details);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   922
    Free(ctx, td);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   923
} // delete_typedef
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   924
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   925
static MOJOSHADER_astPackOffset *new_pack_offset(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   926
                                                 const char *a, const char *b)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   927
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   928
    NEW_AST_NODE(retval, MOJOSHADER_astPackOffset, MOJOSHADER_AST_PACK_OFFSET);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   929
    retval->ident1 = a;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   930
    retval->ident2 = b;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   931
    return retval;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   932
} // new_pack_offset
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   933
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   934
static void delete_pack_offset(Context *ctx, MOJOSHADER_astPackOffset *o)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   935
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   936
    DELETE_AST_NODE(o);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   937
    Free(ctx, o);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   938
} // delete_pack_offset
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   939
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   940
static MOJOSHADER_astVariableLowLevel *new_variable_lowlevel(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   941
                                               MOJOSHADER_astPackOffset *po,
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   942
                                               const char *reg)
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   943
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   944
    NEW_AST_NODE(retval, MOJOSHADER_astVariableLowLevel,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   945
                 MOJOSHADER_AST_VARIABLE_LOWLEVEL);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   946
    retval->packoffset = po;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   947
    retval->register_name = reg;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   948
    return retval;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   949
} // new_variable_lowlevel
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   950
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   951
static void delete_variable_lowlevel(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   952
                                     MOJOSHADER_astVariableLowLevel *vll)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   953
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   954
    DELETE_AST_NODE(vll);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   955
    delete_pack_offset(ctx, vll->packoffset);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   956
    Free(ctx, vll);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   957
} // delete_variable_lowlevel
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   958
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   959
static MOJOSHADER_astAnnotations *new_annotation(Context *ctx,
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   960
                                        const MOJOSHADER_astDataType *dt,
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   961
                                        MOJOSHADER_astExpression *initializer)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   962
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   963
    NEW_AST_NODE(retval, MOJOSHADER_astAnnotations, MOJOSHADER_AST_ANNOTATION);
964
e8c09c28162e Reworked datatype processing in the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 958
diff changeset
   964
    retval->datatype = dt;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   965
    retval->initializer = initializer;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   966
    retval->next = NULL;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   967
    return retval;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   968
} // new_annotation
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   969
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   970
static void delete_annotation(Context *ctx, MOJOSHADER_astAnnotations *annos)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   971
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   972
    DELETE_AST_NODE(annos);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   973
    delete_annotation(ctx, annos->next);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   974
    delete_expr(ctx, annos->initializer);
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   975
    Free(ctx, annos);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   976
} // delete_annotation
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   977
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   978
static MOJOSHADER_astVariableDeclaration *new_variable_declaration(
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   979
                            Context *ctx, MOJOSHADER_astScalarOrArray *soa,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   980
                            const char *semantic,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   981
                            MOJOSHADER_astAnnotations *annotations,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   982
                            MOJOSHADER_astExpression *init,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   983
                            MOJOSHADER_astVariableLowLevel *vll)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   984
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   985
    NEW_AST_NODE(retval, MOJOSHADER_astVariableDeclaration,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   986
                 MOJOSHADER_AST_VARIABLE_DECLARATION);
986
805aa6eb75af Fixed some uninitialized memory reads, compliments of valgrind.
Ryan C. Gordon <icculus@icculus.org>
parents: 985
diff changeset
   987
    retval->datatype = NULL;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   988
    retval->attributes = 0;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   989
    retval->anonymous_datatype = NULL;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   990
    retval->details = soa;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   991
    retval->semantic = semantic;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   992
    retval->annotations = annotations;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   993
    retval->initializer = init;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   994
    retval->lowlevel = vll;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   995
    retval->next = NULL;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   996
    return retval;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   997
} // new_variable_declaration
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
   998
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
   999
static void delete_variable_declaration(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1000
                                        MOJOSHADER_astVariableDeclaration *dcl)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1001
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1002
    DELETE_AST_NODE(dcl);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1003
    delete_variable_declaration(ctx, dcl->next);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1004
    delete_scalar_or_array(ctx, dcl->details);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1005
    delete_annotation(ctx, dcl->annotations);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1006
    delete_expr(ctx, dcl->initializer);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1007
    delete_variable_lowlevel(ctx, dcl->lowlevel);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1008
    Free(ctx, dcl);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1009
} // delete_variable_declaration
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1010
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1011
static MOJOSHADER_astCompilationUnit *new_global_variable(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1012
                                      MOJOSHADER_astVariableDeclaration *decl)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1013
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1014
    NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitVariable,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1015
                 MOJOSHADER_AST_COMPUNIT_VARIABLE);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1016
    retval->next = NULL;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1017
    retval->declaration = decl;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1018
    return (MOJOSHADER_astCompilationUnit *) retval;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1019
} // new_global_variable
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1020
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1021
static void delete_global_variable(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1022
                                   MOJOSHADER_astCompilationUnitVariable *var)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1023
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1024
    DELETE_AST_NODE(var);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1025
    delete_compilation_unit(ctx, var->next);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1026
    delete_variable_declaration(ctx, var->declaration);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1027
    Free(ctx, var);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1028
} // delete_global_variable
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1029
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1030
static MOJOSHADER_astCompilationUnit *new_global_typedef(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1031
                                                     MOJOSHADER_astTypedef *td)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1032
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1033
    NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitTypedef,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1034
                 MOJOSHADER_AST_COMPUNIT_TYPEDEF);
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1035
    retval->next = NULL;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1036
    retval->type_info = td;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1037
    return (MOJOSHADER_astCompilationUnit *) retval;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1038
} // new_global_typedef
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1039
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1040
static void delete_global_typedef(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1041
                                  MOJOSHADER_astCompilationUnitTypedef *unit)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1042
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1043
    DELETE_AST_NODE(unit);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1044
    delete_compilation_unit(ctx, unit->next);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1045
    delete_typedef(ctx, unit->type_info);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1046
    Free(ctx, unit);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1047
} // delete_global_typedef
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1048
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1049
static MOJOSHADER_astStructMembers *new_struct_member(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1050
                                            MOJOSHADER_astScalarOrArray *soa,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1051
                                            const char *semantic)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1052
{
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1053
    NEW_AST_NODE(retval, MOJOSHADER_astStructMembers,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1054
                 MOJOSHADER_AST_STRUCT_MEMBER);
986
805aa6eb75af Fixed some uninitialized memory reads, compliments of valgrind.
Ryan C. Gordon <icculus@icculus.org>
parents: 985
diff changeset
  1055
    retval->datatype = NULL;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1056
    retval->semantic = semantic;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1057
    retval->details = soa;
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1058
    retval->interpolation_mod = MOJOSHADER_AST_INTERPMOD_NONE;
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1059
    retval->next = NULL;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1060
    return retval;
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1061
} // new_struct_member
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1062
931
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1063
static void delete_struct_member(Context *ctx,
4aa1f68d8292 Heavy rework of the AST code.
Ryan C. Gordon <icculus@icculus.org>
parents: 930
diff changeset
  1064
                                 MOJOSHADER_astStructMembers *member)
836
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1065
{
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1066
    DELETE_AST_NODE(member);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1067
    delete_struct_member(ctx, member->next);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1068
    delete_scalar_or_array(ctx, member->details);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1069
    Free(ctx, member);
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1070
} // delete_struct_member
d975fa785f1e Bunch More Work on HLSL parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 835
diff changeset
  1071
931
4aa1f68d8292 Heavy rework of the A