]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - parser.h
0a303e0e220055c11e4247d6e763b434cbd5d551
[xonotic/gmqcc.git] / parser.h
1 #ifndef GMQCC_PARSER_HDR
2 #define GMQCC_PARSER_HDR
3 #include "gmqcc.h"
4 #include "lexer.h"
5 #include "ast.h"
6
7 typedef struct intrin_s intrin_t;
8 typedef struct parser_s parser_t;
9
10 typedef struct {
11     struct parser_s *parser;
12     ast_value      **imm_float;              /* vector<ast_value*> */
13     ast_value      **imm_vector;             /* vector<ast_value*> */
14     ast_value      **imm_string;             /* vector<ast_value*> */
15     hash_table_t    *imm_string_untranslate; /* map<string, ast_value*> */
16     hash_table_t    *imm_string_dotranslate; /* map<string, ast_value*> */
17 } fold_t;
18
19 typedef struct {
20     ast_expression *(*intrin)(intrin_t *);
21     const char       *name;
22     const char       *alias;
23     size_t            args;
24 } intrin_func_t;
25
26 struct intrin_s {
27     intrin_func_t  *intrinsics;              /* vector<intrin_func_t>   */
28     ast_expression **generated;              /* vector<ast_expression*> */
29     parser_t       *parser;
30     fold_t         *fold;
31 };
32
33 #define parser_ctx(p) ((p)->lex->tok.ctx)
34
35 struct parser_s {
36     lex_file *lex;
37     int      tok;
38
39     bool     ast_cleaned;
40
41     ast_expression **globals;
42     ast_expression **fields;
43     ast_function **functions;
44     size_t         translated;
45
46     /* must be deleted first, they reference immediates and values */
47     ast_value    **accessors;
48
49     ast_value *nil;
50     ast_value *reserved_version;
51
52     size_t crc_globals;
53     size_t crc_fields;
54
55     ast_function *function;
56     ht            aliases;
57
58     /* All the labels the function defined...
59      * Should they be in ast_function instead?
60      */
61     ast_label  **labels;
62     ast_goto   **gotos;
63     const char **breaks;
64     const char **continues;
65
66     /* A list of hashtables for each scope */
67     ht *variables;
68     ht htfields;
69     ht htglobals;
70     ht *typedefs;
71
72     /* not to be used directly, we use the hash table */
73     ast_expression **_locals;
74     size_t          *_blocklocals;
75     ast_value      **_typedefs;
76     size_t          *_blocktypedefs;
77     lex_ctx_t         *_block_ctx;
78
79     /* we store the '=' operator info */
80     const oper_info *assign_op;
81
82     /* magic values */
83     ast_value *const_vec[3];
84
85     /* pragma flags */
86     bool noref;
87
88     /* collected information */
89     size_t     max_param_count;
90
91     fold_t   *fold;
92     intrin_t *intrin;
93 };
94
95
96 /* parser.c */
97 char           *parser_strdup     (const char *str);
98 ast_expression *parser_find_global(parser_t *parser, const char *name);
99
100 /* fold.c */
101 fold_t         *fold_init           (parser_t *);
102 void            fold_cleanup        (fold_t *);
103 ast_expression *fold_constgen_float (fold_t *, qcfloat_t, bool);
104 ast_expression *fold_constgen_vector(fold_t *, vec3_t);
105 ast_expression *fold_constgen_string(fold_t *, const char *, bool);
106 bool            fold_generate       (fold_t *, ir_builder *);
107 ast_expression *fold_op             (fold_t *, const oper_info *, ast_expression **);
108 ast_expression *fold_intrin         (fold_t *, const char      *, ast_expression **);
109
110 ast_expression *fold_binary         (lex_ctx_t ctx, int, ast_expression *, ast_expression *);
111 int             fold_cond_ifthen    (ir_value *, ast_function *, ast_ifthen  *);
112 int             fold_cond_ternary   (ir_value *, ast_function *, ast_ternary *);
113
114 /* intrin.c */
115 intrin_t       *intrin_init            (parser_t *parser);
116 void            intrin_cleanup         (intrin_t *intrin);
117 ast_expression *intrin_fold            (intrin_t *intrin, ast_value *, ast_expression **);
118 ast_expression *intrin_func            (intrin_t *intrin, const char *name);
119 ast_expression *intrin_debug_typestring(intrin_t *intrin);
120
121 #endif