/
mojoshader_compiler.c
3581 lines (3122 loc) · 115 KB
1
2
3
4
5
6
7
8
9
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
10
11
12
13
// !!! FIXME: this needs to be split into separate source files:
// !!! FIXME: parse, AST, IR, etc. The problem is we need to deal with the
// !!! FIXME: "Context" struct being passed around everywhere.
14
15
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
16
17
18
19
20
#if DEBUG_COMPILER_PARSER
#define LEMON_SUPPORT_TRACING 1
#endif
21
22
// !!! FIXME: I'd like to lose this. It's really inefficient. Just keep a
// !!! FIXME: (tail) on these list structures instead?
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#define REVERSE_LINKED_LIST(typ, head) { \
if ((head) && (head->next)) { \
typ *tmp = NULL; \
typ *tmp1 = NULL; \
while (head != NULL) { \
tmp = head; \
head = head->next; \
tmp->next = tmp1; \
tmp1 = tmp; \
} \
head = tmp; \
} \
}
37
typedef union TokenData
38
{
39
40
41
int64 i64;
double dbl;
const char *string;
42
43
} TokenData;
44
45
// Structures that make up the parse tree...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
typedef enum ASTNodeType
{
AST_OP_START_RANGE,
AST_OP_START_RANGE_UNARY,
AST_OP_POSTINCREMENT,
AST_OP_POSTDECREMENT,
AST_OP_PREINCREMENT,
AST_OP_PREDECREMENT,
AST_OP_NEGATE,
AST_OP_COMPLEMENT,
AST_OP_NOT,
AST_OP_END_RANGE_UNARY,
AST_OP_START_RANGE_BINARY,
AST_OP_DEREF_ARRAY,
AST_OP_COMMA,
AST_OP_MULTIPLY,
AST_OP_DIVIDE,
AST_OP_MODULO,
AST_OP_ADD,
AST_OP_SUBTRACT,
AST_OP_LSHIFT,
AST_OP_RSHIFT,
AST_OP_LESSTHAN,
AST_OP_GREATERTHAN,
AST_OP_LESSTHANOREQUAL,
AST_OP_GREATERTHANOREQUAL,
AST_OP_EQUAL,
AST_OP_NOTEQUAL,
AST_OP_BINARYAND,
AST_OP_BINARYXOR,
AST_OP_BINARYOR,
AST_OP_LOGICALAND,
AST_OP_LOGICALOR,
AST_OP_ASSIGN,
AST_OP_MULASSIGN,
AST_OP_DIVASSIGN,
AST_OP_MODASSIGN,
AST_OP_ADDASSIGN,
AST_OP_SUBASSIGN,
AST_OP_LSHIFTASSIGN,
AST_OP_RSHIFTASSIGN,
AST_OP_ANDASSIGN,
AST_OP_XORASSIGN,
AST_OP_ORASSIGN,
AST_OP_END_RANGE_BINARY,
AST_OP_START_RANGE_TERNARY,
AST_OP_CONDITIONAL,
AST_OP_END_RANGE_TERNARY,
AST_OP_START_RANGE_DATA,
AST_OP_IDENTIFIER,
AST_OP_INT_LITERAL,
AST_OP_FLOAT_LITERAL,
AST_OP_STRING_LITERAL,
103
AST_OP_BOOLEAN_LITERAL,
104
105
106
AST_OP_END_RANGE_DATA,
AST_OP_START_RANGE_MISC,
107
AST_OP_DEREF_STRUCT,
108
AST_OP_CALLFUNC,
109
110
111
112
113
114
115
116
117
118
119
120
121
AST_OP_CONSTRUCTOR,
AST_OP_CAST,
AST_OP_END_RANGE_MISC,
AST_OP_END_RANGE,
AST_COMPUNIT_START_RANGE,
AST_COMPUNIT_FUNCTION, // function declaration or definition
AST_COMPUNIT_TYPEDEF, // typedef or struct
AST_COMPUNIT_STRUCT, // global struct
AST_COMPUNIT_VARIABLE, // global variable
AST_COMPUNIT_END_RANGE,
AST_STATEMENT_START_RANGE,
122
AST_STATEMENT_BLOCK,
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
AST_STATEMENT_EMPTY,
AST_STATEMENT_EXPRESSION,
AST_STATEMENT_IF,
AST_STATEMENT_SWITCH,
AST_STATEMENT_FOR,
AST_STATEMENT_DO,
AST_STATEMENT_WHILE,
AST_STATEMENT_RETURN,
AST_STATEMENT_BREAK,
AST_STATEMENT_CONTINUE,
AST_STATEMENT_DISCARD,
AST_STATEMENT_TYPEDEF,
AST_STATEMENT_STRUCT,
AST_STATEMENT_VARDECL,
AST_STATEMENT_END_RANGE,
AST_MISC_START_RANGE,
140
AST_FUNCTION_PARAMS,
141
142
143
144
145
146
147
148
149
150
AST_FUNCTION_SIGNATURE,
AST_SCALAR_OR_ARRAY,
AST_TYPEDEF,
AST_PACK_OFFSET,
AST_VARIABLE_LOWLEVEL,
AST_ANNOTATION,
AST_VARIABLE_DECLARATION,
AST_STRUCT_DECLARATION,
AST_STRUCT_MEMBER,
AST_SWITCH_CASE,
151
AST_ARGUMENTS,
152
153
154
155
156
157
158
159
160
161
162
AST_MISC_END_RANGE,
AST_END_RANGE
} ASTNodeType;
typedef struct ASTNode
{
ASTNodeType type;
const char *filename;
uint32 line;
} ASTNode;
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
typedef enum VariableAttributes
{
VARATTR_EXTERN = (1 << 0),
VARATTR_NOINTERPOLATION = (1 << 1),
VARATTR_SHARED = (1 << 2),
VARATTR_STATIC = (1 << 3),
VARATTR_UNIFORM = (1 << 4),
VARATTR_VOLATILE = (1 << 5),
VARATTR_CONST = (1 << 6),
VARATTR_ROWMAJOR = (1 << 7),
VARATTR_COLUMNMAJOR = (1 << 8)
} VariableAttributes;
typedef enum IfAttributes
{
IFATTR_NONE,
IFATTR_BRANCH,
IFATTR_FLATTEN,
IFATTR_IFALL,
IFATTR_IFANY,
IFATTR_PREDICATE,
IFATTR_PREDICATEBLOCK,
} IfAttributes;
typedef enum SwitchAttributes
{
SWITCHATTR_NONE,
SWITCHATTR_FLATTEN,
SWITCHATTR_BRANCH,
SWITCHATTR_FORCECASE,
SWITCHATTR_CALL
} SwitchAttributes;
197
static inline int operator_is_unary(const ASTNodeType op)
198
{
199
return ((op > AST_OP_START_RANGE_UNARY) && (op < AST_OP_END_RANGE_UNARY));
200
201
} // operator_is_unary
202
static inline int operator_is_binary(const ASTNodeType op)
203
{
204
return ((op > AST_OP_START_RANGE_BINARY) && (op < AST_OP_END_RANGE_BINARY));
205
206
} // operator_is_binary
207
static inline int operator_is_ternary(const ASTNodeType op)
208
{
209
return ((op > AST_OP_START_RANGE_TERNARY) && (op < AST_OP_END_RANGE_TERNARY));
210
211
} // operator_is_ternary
212
typedef struct ASTGeneric
213
{
214
215
216
217
ASTNode ast;
} ASTGeneric;
typedef ASTGeneric Expression;
218
219
220
221
222
223
224
225
typedef struct Arguments
{
ASTNode ast; // Always AST_ARGUMENTS
Expression *argument;
struct Arguments *next;
} Arguments;
226
227
typedef struct ExpressionUnary
{
228
ASTNode ast;
229
230
231
232
233
Expression *operand;
} ExpressionUnary;
typedef struct ExpressionBinary
{
234
ASTNode ast;
235
236
237
238
239
240
Expression *left;
Expression *right;
} ExpressionBinary;
typedef struct ExpressionTernary
{
241
ASTNode ast;
242
243
244
245
246
247
248
Expression *left;
Expression *center;
Expression *right;
} ExpressionTernary;
typedef struct ExpressionIdentifier
{
249
ASTNode ast; // Always AST_OP_IDENTIFIER
250
251
252
253
254
const char *identifier;
} ExpressionIdentifier;
typedef struct ExpressionIntLiteral
{
255
ASTNode ast; // Always AST_OP_INT_LITERAL
256
257
258
259
260
int64 value;
} ExpressionIntLiteral;
typedef struct ExpressionFloatLiteral
{
261
ASTNode ast; // Always AST_OP_FLOAT_LITERAL
262
263
264
265
266
double value;
} ExpressionFloatLiteral;
typedef struct ExpressionStringLiteral
{
267
ASTNode ast; // Always AST_OP_STRING_LITERAL
268
269
270
const char *string;
} ExpressionStringLiteral;
271
272
273
274
275
276
typedef struct ExpressionBooleanLiteral
{
ASTNode ast; // Always AST_OP_BOOLEAN_LITERAL
int value; // Always 1 or 0.
} ExpressionBooleanLiteral;
277
278
typedef struct ExpressionConstructor
{
279
ASTNode ast; // Always AST_OP_CONSTRUCTOR
280
const char *datatype;
281
Arguments *args;
282
283
} ExpressionConstructor;
284
285
286
287
288
289
290
typedef struct ExpressionDerefStruct
{
ASTNode ast; // Always AST_OP_DEREF_STRUCT
Expression *identifier;
const char *member;
} ExpressionDerefStruct;
291
292
293
294
295
296
297
typedef struct ExpressionCallFunction
{
ASTNode ast; // Always AST_OP_CALLFUNC
Expression *identifier;
Arguments *args;
} ExpressionCallFunction;
298
299
typedef struct ExpressionCast
{
300
ASTNode ast; // Always AST_OP_CAST
301
302
303
304
const char *datatype;
Expression *operand;
} ExpressionCast;
305
306
typedef struct CompilationUnit
{
307
ASTNode ast;
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
struct CompilationUnit *next;
} CompilationUnit;
typedef enum FunctionStorageClass
{
FNSTORECLS_NONE,
FNSTORECLS_INLINE
} FunctionStorageClass;
typedef enum InputModifier
{
INPUTMOD_NONE,
INPUTMOD_IN,
INPUTMOD_OUT,
INPUTMOD_INOUT,
INPUTMOD_UNIFORM
} InputModifier;
typedef enum InterpolationModifier
{
INTERPMOD_NONE,
INTERPMOD_LINEAR,
INTERPMOD_CENTROID,
INTERPMOD_NOINTERPOLATION,
INTERPMOD_NOPERSPECTIVE,
INTERPMOD_SAMPLE
} InterpolationModifier;
336
typedef struct FunctionParameters
337
{
338
ASTNode ast;
339
340
341
342
343
344
InputModifier input_modifier;
const char *datatype;
const char *identifier;
const char *semantic;
InterpolationModifier interpolation_modifier;
Expression *initializer;
345
346
struct FunctionParameters *next;
} FunctionParameters;
347
348
349
typedef struct FunctionSignature
{
350
ASTNode ast;
351
352
const char *datatype;
const char *identifier;
353
FunctionParameters *params;
354
355
356
357
358
359
FunctionStorageClass storage_class;
const char *semantic;
} FunctionSignature;
typedef struct ScalarOrArray
{
360
ASTNode ast;
361
362
363
364
365
366
367
const char *identifier;
int isarray;
Expression *dimension;
} ScalarOrArray;
typedef struct Annotations
{
368
ASTNode ast;
369
370
371
372
373
374
375
const char *datatype;
Expression *initializer;
struct Annotations *next;
} Annotations;
typedef struct PackOffset
{
376
ASTNode ast;
377
378
379
380
381
382
const char *ident1; // !!! FIXME: rename this.
const char *ident2;
} PackOffset;
typedef struct VariableLowLevel
{
383
ASTNode ast;
384
385
386
387
388
389
PackOffset *packoffset;
const char *register_name;
} VariableLowLevel;
typedef struct StructMembers
{
390
ASTNode ast;
391
392
393
394
395
396
397
398
399
const char *datatype;
const char *semantic;
ScalarOrArray *details;
InterpolationModifier interpolation_mod;
struct StructMembers *next;
} StructMembers;
typedef struct StructDeclaration
{
400
ASTNode ast;
401
402
403
404
405
406
const char *name;
StructMembers *members;
} StructDeclaration;
typedef struct VariableDeclaration
{
407
ASTNode ast;
408
409
410
411
412
413
414
415
416
417
418
419
420
int attributes;
const char *datatype;
StructDeclaration *anonymous_datatype;
ScalarOrArray *details;
const char *semantic;
Annotations *annotations;
Expression *initializer;
VariableLowLevel *lowlevel;
struct VariableDeclaration *next;
} VariableDeclaration;
typedef struct Statement
{
421
ASTNode ast;
422
423
424
425
426
427
428
429
struct Statement *next;
} Statement;
typedef Statement EmptyStatement;
typedef Statement BreakStatement;
typedef Statement ContinueStatement;
typedef Statement DiscardStatement;
430
431
432
433
434
435
436
typedef struct BlockStatement // something enclosed in "{}" braces.
{
ASTNode ast;
struct Statement *next;
Statement *statements; // list of statements enclosed by this block.
} BlockStatement;
437
438
typedef struct ReturnStatement
{
439
ASTNode ast;
440
441
442
443
444
445
struct Statement *next;
Expression *expr;
} ReturnStatement;
typedef struct ExpressionStatement
{
446
ASTNode ast;
447
448
449
450
451
452
struct Statement *next;
Expression *expr;
} ExpressionStatement;
typedef struct IfStatement
{
453
ASTNode ast;
454
455
456
457
458
459
460
461
462
struct Statement *next;
int attributes;
Expression *expr;
Statement *statement;
Statement *else_statement;
} IfStatement;
typedef struct SwitchCases
{
463
ASTNode ast;
464
465
466
467
468
469
470
Expression *expr;
Statement *statement;
struct SwitchCases *next;
} SwitchCases;
typedef struct SwitchStatement
{
471
ASTNode ast;
472
473
474
475
476
477
478
479
struct Statement *next;
int attributes;
Expression *expr;
SwitchCases *cases;
} SwitchStatement;
typedef struct WhileStatement
{
480
ASTNode ast;
481
struct Statement *next;
482
int64 unroll; // # times to unroll, 0 to loop, negative for compiler's choice.
483
484
485
486
487
488
489
490
Expression *expr;
Statement *statement;
} WhileStatement;
typedef WhileStatement DoStatement;
typedef struct ForStatement
{
491
ASTNode ast;
492
struct Statement *next;
493
int64 unroll; // # times to unroll, 0 to loop, negative for compiler's choice.
494
495
496
497
498
499
500
501
502
VariableDeclaration *var_decl;
Expression *initializer;
Expression *looptest;
Expression *counter;
Statement *statement;
} ForStatement;
typedef struct Typedef
{
503
ASTNode ast;
504
505
506
507
508
509
510
int isconst;
const char *datatype;
ScalarOrArray *details;
} Typedef;
typedef struct TypedefStatement
{
511
ASTNode ast;
512
513
514
515
516
517
struct Statement *next;
Typedef *type_info;
} TypedefStatement;
typedef struct VarDeclStatement
{
518
ASTNode ast;
519
struct Statement *next;
520
VariableDeclaration *declaration;
521
522
523
524
} VarDeclStatement;
typedef struct StructStatement
{
525
ASTNode ast;
526
527
528
529
530
531
struct Statement *next;
StructDeclaration *struct_info;
} StructStatement;
typedef struct CompilationUnitFunction
{
532
ASTNode ast;
533
534
535
536
537
538
539
struct CompilationUnit *next;
FunctionSignature *declaration;
Statement *definition;
} CompilationUnitFunction;
typedef struct CompilationUnitTypedef
{
540
ASTNode ast;
541
542
543
544
545
546
struct CompilationUnit *next;
Typedef *type_info;
} CompilationUnitTypedef;
typedef struct CompilationUnitStruct
{
547
ASTNode ast;
548
549
550
551
552
553
struct CompilationUnit *next;
StructDeclaration *struct_info;
} CompilationUnitStruct;
typedef struct CompilationUnitVariable
{
554
ASTNode ast;
555
556
557
558
559
struct CompilationUnit *next;
VariableDeclaration *declaration;
} CompilationUnitVariable;
560
// This tracks data types and variables, and notes when they enter/leave scope.
561
562
typedef struct SymbolScope
563
564
565
{
const char *symbol;
const char *datatype;
566
567
struct SymbolScope *next;
} SymbolScope;
568
569
typedef struct SymbolMap
570
{
571
572
573
HashTable *hash;
SymbolScope *scope;
} SymbolMap;
574
575
576
577
578
579
580
581
582
583
584
585
586
// Compile state, passed around all over the place.
typedef struct Context
{
int isfail;
int out_of_memory;
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
int error_count;
ErrorList *errors;
587
StringCache *strcache;
588
589
const char *sourcefile; // current source file that we're parsing.
unsigned int sourceline; // current line in sourcefile that we're parsing.
590
591
SymbolMap usertypes;
SymbolMap variables;
592
CompilationUnit *ast; // Abstract Syntax Tree
593
594
595
596
597
598
// These are entries allocated in the strcache, so these pointers are
// valid from shortly after we create the cache until we destroy it
// with the rest of the context. This makes it so we can compare common
// strings by pointer without having to hash them every time, so long as
// we're comparing a string pointer we know came from this string cache.
599
// The first batch are simplifed datatype strings ("b" == bool, etc).
600
601
602
const char *str_b; // "b"
const char *str_f; // "f"
const char *str_i; // "i"
603
604
605
606
const char *str_u; // "u"
const char *str_h; // "h"
const char *str_d; // "d"
const char *str_s; // "s"
607
608
609
610
611
612
613
614
615
616
617
const char *str_S; // "S"
const char *str_s1; // "s1"
const char *str_s2; // "s2"
const char *str_s3; // "s3"
const char *str_sc; // "sc"
const char *str_ss; // "ss"
const char *str_sS; // "sS"
const char *str_Fs; // "Fs"
const char *str_Fu; // "Fu"
const char *str_ns; // "ns"
const char *str_nu; // "nu"
618
619
620
621
622
623
624
} Context;
// Convenience functions for allocators...
static inline void out_of_memory(Context *ctx)
{
625
if (!ctx->out_of_memory) printf("out of memory\n"); // !!! FIXME: placeholder.
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
ctx->isfail = ctx->out_of_memory = 1;
} // out_of_memory
static inline void *Malloc(Context *ctx, const size_t len)
{
void *retval = ctx->malloc((int) len, ctx->malloc_data);
if (retval == NULL)
out_of_memory(ctx);
return retval;
} // Malloc
static inline char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
if (retval != NULL)
strcpy(retval, str);
return retval;
} // StrDup
static inline void Free(Context *ctx, void *ptr)
{
if (ptr != NULL) // check for NULL in case of dumb free() impl.
ctx->free(ptr, ctx->malloc_data);
} // Free
651
652
653
654
static void fail(Context *ctx, const char *str)
{
// !!! FIXME: placeholder.
(void) ctx;
655
printf("%s:%u: %s\n", ctx->sourcefile, ctx->sourceline, str);
656
} // fail
657
658
659
static void symbolmap_nuke(const void *k, const void *v, void *d) {/*no-op*/}
660
661
static int create_symbolmap(Context *ctx, SymbolMap *map)
662
{
663
// !!! FIXME: should compare string pointer, with string in cache.
664
map->scope = NULL;
665
666
667
668
map->hash = hash_create(ctx, hash_hash_string, hash_keymatch_string,
symbolmap_nuke, 1, ctx->malloc, ctx->free,
ctx->malloc_data);
if (!map->hash)
669
670
671
672
673
674
{
out_of_memory(ctx);
return 0;
} // if
return 1;
675
} // create_symbolmap
676
677
678
679
680
static void push_symbol(Context *ctx, SymbolMap *map,
const char *sym, const char *datatype)
{
681
682
683
// !!! FIXME: decide if this symbol is defined, and if so, if it's in
// !!! FIXME: the current scope.
684
SymbolScope *item = (SymbolScope *) Malloc(ctx, sizeof (SymbolScope));
685
686
687
688
689
if (item == NULL)
return;
if (sym != NULL)
{
690
if (hash_insert(map->hash, sym, datatype) == -1)
691
692
693
{
Free(ctx, item);
return;
694
} // if
695
696
697
698
699
700
} // if
item->symbol = sym; // cached strings, don't copy.
item->datatype = datatype;
item->next = map->scope;
map->scope = item;
701
702
703
704
705
} // push_symbol
static inline void push_usertype(Context *ctx, const char *sym, const char *dt)
{
push_symbol(ctx, &ctx->usertypes, sym, dt);
706
707
} // push_usertype
708
709
710
711
712
713
714
715
716
717
718
719
720
static inline void push_variable(Context *ctx, const char *sym, const char *dt)
{
push_symbol(ctx, &ctx->variables, sym, dt);
} // push_variable
static inline void push_scope(Context *ctx)
{
push_usertype(ctx, NULL, NULL);
push_variable(ctx, NULL, NULL);
} // push_scope
static void pop_symbol(Context *ctx, SymbolMap *map)
721
{
722
SymbolScope *item = map->scope;
723
724
725
if (!item)
return;
if (item->symbol)
726
hash_remove(map->hash, item->symbol);
727
728
map->scope = item->next;
Free(ctx, item);
729
} // pop_symbol
730
731
static void pop_symbol_scope(Context *ctx, SymbolMap *map)
732
733
{
while ((map->scope) && (map->scope->symbol))
734
pop_symbol(ctx, map);
735
736
737
assert(map->scope != NULL);
assert(map->scope->symbol == NULL);
738
739
740
741
742
743
744
pop_symbol(ctx, map);
} // pop_symbol_scope
static inline void pop_scope(Context *ctx)
{
pop_symbol_scope(ctx, &ctx->usertypes);
pop_symbol_scope(ctx, &ctx->variables);
745
746
} // push_scope
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
static const char *find_symbol(Context *ctx, SymbolMap *map, const char *sym)
{
const void *value = NULL;
hash_find(map->hash, sym, &value);
return (const char *) value;
} // find_symbol
static inline const char *find_usertype(Context *ctx, const char *sym)
{
return find_symbol(ctx, &ctx->usertypes, sym);
} // find_usertype
static inline const char *find_variable(Context *ctx, const char *sym)
{
return find_symbol(ctx, &ctx->variables, sym);
} // find_variable
764
static void destroy_symbolmap(Context *ctx, SymbolMap *map)
765
766
{
while (map->scope)
767
768
769
pop_symbol_scope(ctx, map);
hash_destroy(map->hash);
} // destroy_symbolmap
770
771
772
773
774
775
776
// These functions are mostly for construction and cleanup of nodes in the
// parse tree. Mostly this is simple allocation and initialization, so we
// can do as little in the lemon code as possible, and then sort it all out
// afterwards.
777
#define NEW_AST_NODE(retval, cls, typ) \
778
cls *retval = (cls *) Malloc(ctx, sizeof (cls)); \
779
780
781
782
783
784
785
786
do { \
if (retval == NULL) { return NULL; } \
retval->ast.type = typ; \
retval->ast.filename = ctx->sourcefile; \
retval->ast.line = ctx->sourceline; \
} while (0)
#define DELETE_AST_NODE(cls) do { \
787
788
789
790
791
792
if (!cls) return; \
} while (0)
static void delete_compilation_unit(Context *ctx, CompilationUnit *unit);
static void delete_statement(Context *ctx, Statement *stmt);
793
794
795
796
797
798
799
800
801
static Expression *new_callfunc_expr(Context *ctx, Expression *identifier,
Arguments *args)
{
NEW_AST_NODE(retval, ExpressionCallFunction, AST_OP_CALLFUNC);
retval->identifier = identifier;
retval->args = args;
return (Expression *) retval;
} // new_callfunc_expr
802
static Expression *new_constructor_expr(Context *ctx, const char *datatype,
803
Arguments *args)
804
{
805
NEW_AST_NODE(retval, ExpressionConstructor, AST_OP_CONSTRUCTOR);
806
807
808
809
810
811
812
813
retval->datatype = datatype;
retval->args = args;
return (Expression *) retval;
} // new_constructor_expr
static Expression *new_cast_expr(Context *ctx, const char *datatype,
Expression *operand)
{
814
NEW_AST_NODE(retval, ExpressionCast, AST_OP_CAST);
815
816
817
818
819
retval->datatype = datatype;
retval->operand = operand;
return (Expression *) retval;
} // new_cast_expr
820
static Expression *new_unary_expr(Context *ctx, const ASTNodeType op,
821
822
Expression *operand)
{
823
NEW_AST_NODE(retval, ExpressionUnary, op);
824
825
826
827
828
assert(operator_is_unary(op));
retval->operand = operand;
return (Expression *) retval;
} // new_unary_expr
829
static Expression *new_binary_expr(Context *ctx, const ASTNodeType op,
830
831
Expression *left, Expression *right)
{
832
NEW_AST_NODE(retval, ExpressionBinary, op);
833
834
835
836
837
838
assert(operator_is_binary(op));
retval->left = left;
retval->right = right;
return (Expression *) retval;
} // new_binary_expr
839
static Expression *new_ternary_expr(Context *ctx, const ASTNodeType op,
840
841
842
Expression *left, Expression *center,
Expression *right)
{
843
NEW_AST_NODE(retval, ExpressionTernary, op);
844
845
846
847
848
849
850
assert(operator_is_ternary(op));
retval->left = left;
retval->center = center;
retval->right = right;
return (Expression *) retval;
} // new_ternary_expr
851
852
853
854
855
856
857
858
859
static Expression *new_deref_struct_expr(Context *ctx, Expression *identifier,
const char *member)
{
NEW_AST_NODE(retval, ExpressionDerefStruct, AST_OP_DEREF_STRUCT);
retval->identifier = identifier;
retval->member = member; // cached; don't copy string.
return (Expression *) retval;
} // new_deref_struct_expr
860
861
static Expression *new_identifier_expr(Context *ctx, const char *string)
{
862
NEW_AST_NODE(retval, ExpressionIdentifier, AST_OP_IDENTIFIER);
863
864
865
866
867
868
retval->identifier = string; // cached; don't copy string.
return (Expression *) retval;
} // new_identifier_expr
static Expression *new_literal_int_expr(Context *ctx, const int64 value)
{
869
NEW_AST_NODE(retval, ExpressionIntLiteral, AST_OP_INT_LITERAL);
870
871
872
873
874
875
retval->value = value;
return (Expression *) retval;
} // new_literal_int_expr
static Expression *new_literal_float_expr(Context *ctx, const double dbl)
{
876
NEW_AST_NODE(retval, ExpressionFloatLiteral, AST_OP_FLOAT_LITERAL);
877
878
879
880
881
882
retval->value = dbl;
return (Expression *) retval;
} // new_literal_float_expr
static Expression *new_literal_string_expr(Context *ctx, const char *string)
{
883
NEW_AST_NODE(retval, ExpressionStringLiteral, AST_OP_STRING_LITERAL);
884
885
retval->string = string; // cached; don't copy string.
return (Expression *) retval;
886
887
888
889
890
891
892
893
} // new_literal_string_expr
static Expression *new_literal_boolean_expr(Context *ctx, const int value)
{
NEW_AST_NODE(retval, ExpressionBooleanLiteral, AST_OP_BOOLEAN_LITERAL);
retval->value = value;
return (Expression *) retval;
} // new_literal_boolean_expr
894
895
896
static void delete_arguments(Context *ctx, Arguments *args);
897
898
899
static void delete_expr(Context *ctx, Expression *expr)
{
DELETE_AST_NODE(expr);
900
if (operator_is_unary(expr->ast.type))
901
902
903
904
{
const ExpressionUnary *unary = (const ExpressionUnary *) expr;
delete_expr(ctx, unary->operand);
} // if
905
else if (operator_is_binary(expr->ast.type))
906
907
908
909
910
{
const ExpressionBinary *binary = (const ExpressionBinary *) expr;
delete_expr(ctx, binary->left);
delete_expr(ctx, binary->right);
} // else if
911
else if (operator_is_ternary(expr->ast.type))
912
913
914
915
916
917
{
const ExpressionTernary *ternary = (const ExpressionTernary *) expr;
delete_expr(ctx, ternary->left);
delete_expr(ctx, ternary->center);
delete_expr(ctx, ternary->right);
} // else if
918
else if (expr->ast.type == AST_OP_CAST)
919
920
921
{
delete_expr(ctx, ((ExpressionCast *) expr)->operand);
} // else if
922
else if (expr->ast.type == AST_OP_CONSTRUCTOR)
923
{
924
925
delete_arguments(ctx, ((ExpressionConstructor *) expr)->args);
} // else if
926
927
928
929
else if (expr->ast.type == AST_OP_DEREF_STRUCT)
{
delete_expr(ctx, ((ExpressionDerefStruct *) expr)->identifier);
} // else if
930
931
932
933
else if (expr->ast.type == AST_OP_CALLFUNC)
{
delete_expr(ctx, ((ExpressionCallFunction *) expr)->identifier);
delete_arguments(ctx, ((ExpressionCallFunction *) expr)->args);
934
} // else if
935
936
937
// rest of operators don't have extra data to free.
938
939
940
Free(ctx, expr);
} // delete_expr
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
static Arguments *new_argument(Context *ctx, Expression *argument)
{
NEW_AST_NODE(retval, Arguments, AST_ARGUMENTS);
retval->argument = argument;
retval->next = NULL;
return retval;
} // new_argument
static void delete_arguments(Context *ctx, Arguments *args)
{
DELETE_AST_NODE(args);
delete_arguments(ctx, args->next);
delete_expr(ctx, args->argument);
Free(ctx, args);
} // delete_arguments
957
static FunctionParameters *new_function_param(Context *ctx,
958
959
960
961
962
963
964
const InputModifier inputmod,
const char *datatype,
const char *identifier,
const char *semantic,
const InterpolationModifier interpmod,
Expression *initializer)
{
965
NEW_AST_NODE(retval, FunctionParameters, AST_FUNCTION_PARAMS);
966
967
968
969
970
971
972
973
retval->input_modifier = inputmod;
retval->datatype = datatype;
retval->identifier = identifier;
retval->semantic = semantic;
retval->interpolation_modifier = interpmod;
retval->initializer = initializer;
retval->next = NULL;
return retval;
974
} // new_function_param
975
976
static void delete_function_params(Context *ctx, FunctionParameters *params)
977
{
978
979
980
981
982
DELETE_AST_NODE(params);
delete_function_params(ctx, params->next);
delete_expr(ctx, params->initializer);
Free(ctx, params);
} // delete_function_params
983
984
985
986
static FunctionSignature *new_function_signature(Context *ctx,
const char *datatype,
const char *identifier,
987
FunctionParameters *params)
988
{
989
NEW_AST_NODE(retval, FunctionSignature, AST_FUNCTION_SIGNATURE);
990
991
retval->datatype = datatype;
retval->identifier = identifier;
992
retval->params = params;
993
994
995
996
997
998
999
1000
retval->storage_class = FNSTORECLS_NONE;
retval->semantic = NULL;
return retval;
} // new_function_signature
static void delete_function_signature(Context *ctx, FunctionSignature *sig)
{
DELETE_AST_NODE(sig);