9d997cd92404b9b01d294810c2f10dd1357f91aa
[mesa.git] / src / compiler / glsl / glcpp / glcpp.h
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #ifndef GLCPP_H
25 #define GLCPP_H
26
27 #include <stdint.h>
28 #include <stdbool.h>
29
30 #include "main/mtypes.h"
31
32 #include "util/ralloc.h"
33
34 #include "util/hash_table.h"
35
36 #include "util/string_buffer.h"
37
38 #define yyscan_t void*
39
40 /* Some data types used for parser values. */
41
42 typedef struct expression_value {
43 intmax_t value;
44 char *undefined_macro;
45 } expression_value_t;
46
47
48 typedef struct string_node {
49 const char *str;
50 struct string_node *next;
51 } string_node_t;
52
53 typedef struct string_list {
54 string_node_t *head;
55 string_node_t *tail;
56 } string_list_t;
57
58 typedef struct token token_t;
59 typedef struct token_list token_list_t;
60
61 typedef union YYSTYPE
62 {
63 intmax_t ival;
64 expression_value_t expression_value;
65 char *str;
66 string_list_t *string_list;
67 token_t *token;
68 token_list_t *token_list;
69 } YYSTYPE;
70
71 # define YYSTYPE_IS_TRIVIAL 1
72 # define YYSTYPE_IS_DECLARED 1
73
74 typedef struct YYLTYPE {
75 int first_line;
76 int first_column;
77 int last_line;
78 int last_column;
79 unsigned source;
80 } YYLTYPE;
81 # define YYLTYPE_IS_DECLARED 1
82 # define YYLTYPE_IS_TRIVIAL 1
83
84 # define YYLLOC_DEFAULT(Current, Rhs, N) \
85 do { \
86 if (N) \
87 { \
88 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
89 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
90 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
91 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
92 } \
93 else \
94 { \
95 (Current).first_line = (Current).last_line = \
96 YYRHSLOC(Rhs, 0).last_line; \
97 (Current).first_column = (Current).last_column = \
98 YYRHSLOC(Rhs, 0).last_column; \
99 } \
100 (Current).source = 0; \
101 } while (0)
102
103 struct token {
104 int type;
105 YYSTYPE value;
106 YYLTYPE location;
107 };
108
109 typedef struct token_node {
110 token_t *token;
111 struct token_node *next;
112 } token_node_t;
113
114 struct token_list {
115 token_node_t *head;
116 token_node_t *tail;
117 token_node_t *non_space_tail;
118 };
119
120 typedef struct argument_node {
121 token_list_t *argument;
122 struct argument_node *next;
123 } argument_node_t;
124
125 typedef struct argument_list {
126 argument_node_t *head;
127 argument_node_t *tail;
128 } argument_list_t;
129
130 typedef struct glcpp_parser glcpp_parser_t;
131
132 typedef enum {
133 TOKEN_CLASS_IDENTIFIER,
134 TOKEN_CLASS_IDENTIFIER_FINALIZED,
135 TOKEN_CLASS_FUNC_MACRO,
136 TOKEN_CLASS_OBJ_MACRO
137 } token_class_t;
138
139 token_class_t
140 glcpp_parser_classify_token (glcpp_parser_t *parser,
141 const char *identifier,
142 int *parameter_index);
143
144 typedef struct {
145 int is_function;
146 string_list_t *parameters;
147 const char *identifier;
148 token_list_t *replacements;
149 } macro_t;
150
151 typedef struct expansion_node {
152 macro_t *macro;
153 token_node_t *replacements;
154 struct expansion_node *next;
155 } expansion_node_t;
156
157 typedef enum skip_type {
158 SKIP_NO_SKIP,
159 SKIP_TO_ELSE,
160 SKIP_TO_ENDIF
161 } skip_type_t;
162
163 typedef struct skip_node {
164 skip_type_t type;
165 bool has_else;
166 YYLTYPE loc; /* location of the initial #if/#elif/... */
167 struct skip_node *next;
168 } skip_node_t;
169
170 typedef struct active_list {
171 const char *identifier;
172 token_node_t *marker;
173 struct active_list *next;
174 } active_list_t;
175
176 struct _mesa_glsl_parse_state;
177
178 typedef void (*glcpp_extension_iterator)(
179 struct _mesa_glsl_parse_state *state,
180 void (*add_builtin_define)(glcpp_parser_t *, const char *, int),
181 glcpp_parser_t *data,
182 unsigned version,
183 bool es);
184
185 struct glcpp_parser {
186 void *linalloc;
187 yyscan_t scanner;
188 struct hash_table *defines;
189 active_list_t *active;
190 int lexing_directive;
191 int lexing_version_directive;
192 int space_tokens;
193 int last_token_was_newline;
194 int last_token_was_space;
195 int first_non_space_token_this_line;
196 int newline_as_space;
197 int in_control_line;
198 int paren_count;
199 int commented_newlines;
200 skip_node_t *skip_stack;
201 int skipping;
202 token_list_t *lex_from_list;
203 token_node_t *lex_from_node;
204 struct _mesa_string_buffer *output;
205 struct _mesa_string_buffer *info_log;
206 int error;
207 glcpp_extension_iterator extensions;
208 const struct gl_extensions *extension_list;
209 void *state;
210 gl_api api;
211 unsigned version;
212
213 /**
214 * Has the #version been set?
215 *
216 * A separate flag is used because any possible sentinel value in
217 * \c ::version could also be set by a #version line.
218 */
219 bool version_set;
220
221 bool has_new_line_number;
222 int new_line_number;
223 bool has_new_source_number;
224 int new_source_number;
225 bool is_gles;
226 };
227
228 glcpp_parser_t *
229 glcpp_parser_create(const struct gl_extensions *extension_list,
230 glcpp_extension_iterator extensions, void *state, gl_api api);
231
232 int
233 glcpp_parser_parse (glcpp_parser_t *parser);
234
235 void
236 glcpp_parser_destroy (glcpp_parser_t *parser);
237
238 void
239 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser);
240
241 int
242 glcpp_preprocess(void *ralloc_ctx, const char **shader, char **info_log,
243 glcpp_extension_iterator extensions, void *state,
244 struct gl_context *g_ctx);
245
246 /* Functions for writing to the info log */
247
248 void
249 glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
250
251 void
252 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
253
254 /* Generated by glcpp-lex.l to glcpp-lex.c */
255
256 int
257 glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
258
259 void
260 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader);
261
262 int
263 glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner);
264
265 int
266 glcpp_lex_destroy (yyscan_t scanner);
267
268 /* Generated by glcpp-parse.y to glcpp-parse.c */
269
270 int
271 yyparse (glcpp_parser_t *parser);
272
273 #endif