]> git.xonotic.org Git - xonotic/gmqcc.git/blob - parser.h
Merge pull request #134 from Sicness/hyphen
[xonotic/gmqcc.git] / parser.h
1 /*
2  * Copyright (C) 2012, 2013
3  *     Wolfgang Bumiller
4  *     Dale Weiler
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is furnished to do
11  * so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef GMQCC_PARSER_HDR
25 #define GMQCC_PARSER_HDR
26 #include "gmqcc.h"
27 #include "lexer.h"
28 #include "ast.h"
29
30 typedef struct intrin_s intrin_t;
31 typedef struct parser_s parser_t;
32
33 typedef struct {
34     struct parser_s *parser;
35     ast_value      **imm_float;              /* vector<ast_value*> */
36     ast_value      **imm_vector;             /* vector<ast_value*> */
37     ast_value      **imm_string;             /* vector<ast_value*> */
38     hash_table_t    *imm_string_untranslate; /* map<string, ast_value*> */
39     hash_table_t    *imm_string_dotranslate; /* map<string, ast_value*> */
40 } fold_t;
41
42 typedef struct {
43     ast_expression *(*intrin)(intrin_t *);
44     const char       *name;
45     const char       *alias;
46 } intrin_func_t;
47
48 struct intrin_s {
49     intrin_func_t  *intrinsics;              /* vector<intrin_func_t> */
50     parser_t       *parser;
51     fold_t         *fold;
52 };
53
54 #define parser_ctx(p) ((p)->lex->tok.ctx)
55
56 struct parser_s {
57     lex_file *lex;
58     int      tok;
59
60     bool     ast_cleaned;
61
62     ast_expression **globals;
63     ast_expression **fields;
64     ast_function **functions;
65     size_t         translated;
66
67     /* must be deleted first, they reference immediates and values */
68     ast_value    **accessors;
69
70     ast_value *nil;
71     ast_value *reserved_version;
72
73     size_t crc_globals;
74     size_t crc_fields;
75
76     ast_function *function;
77     ht            aliases;
78
79     /* All the labels the function defined...
80      * Should they be in ast_function instead?
81      */
82     ast_label  **labels;
83     ast_goto   **gotos;
84     const char **breaks;
85     const char **continues;
86
87     /* A list of hashtables for each scope */
88     ht *variables;
89     ht htfields;
90     ht htglobals;
91     ht *typedefs;
92
93     /* same as above but for the spelling corrector */
94     correct_trie_t  **correct_variables;
95     size_t         ***correct_variables_score;  /* vector of vector of size_t* */
96
97     /* not to be used directly, we use the hash table */
98     ast_expression **_locals;
99     size_t          *_blocklocals;
100     ast_value      **_typedefs;
101     size_t          *_blocktypedefs;
102     lex_ctx_t         *_block_ctx;
103
104     /* we store the '=' operator info */
105     const oper_info *assign_op;
106
107     /* magic values */
108     ast_value *const_vec[3];
109
110     /* pragma flags */
111     bool noref;
112
113     /* collected information */
114     size_t     max_param_count;
115
116     fold_t   *fold;
117     intrin_t *intrin;
118 };
119
120
121 /* parser.c */
122 char           *parser_strdup     (const char *str);
123 ast_expression *parser_find_global(parser_t *parser, const char *name);
124
125 /* fold.c */
126 fold_t         *fold_init           (parser_t *);
127 void            fold_cleanup        (fold_t *);
128 ast_expression *fold_constgen_float (fold_t *, qcfloat_t);
129 ast_expression *fold_constgen_vector(fold_t *, vec3_t);
130 ast_expression *fold_constgen_string(fold_t *, const char *, bool);
131 bool            fold_generate       (fold_t *, ir_builder *);
132 ast_expression *fold_op             (fold_t *, const oper_info *, ast_expression **);
133 ast_expression *fold_intrin         (fold_t *, const char      *, ast_expression **);
134
135 int             fold_cond           (ir_value *, ast_function *, ast_ifthen *);
136
137 /* intrin.c */
138 intrin_t       *intrin_init            (parser_t *parser);
139 void            intrin_cleanup         (intrin_t *intrin);
140 ast_expression *intrin_fold            (intrin_t *intrin, ast_value *, ast_expression **);
141 ast_expression *intrin_func            (intrin_t *intrin, const char *name);
142 ast_expression *intrin_debug_typestring(intrin_t *intrin);
143
144 #endif