glsl/pp: Do processing inline with tokenisation.
[mesa.git] / src / glsl / pp / sl_pp_macro.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include "sl_pp_public.h"
32 #include "sl_pp_macro.h"
33 #include "sl_pp_process.h"
34
35
36 static void
37 _macro_init(struct sl_pp_macro *macro)
38 {
39 macro->name = -1;
40 macro->num_args = -1;
41 macro->arg = NULL;
42 macro->body = NULL;
43 }
44
45 struct sl_pp_macro *
46 sl_pp_macro_new(void)
47 {
48 struct sl_pp_macro *macro;
49
50 macro = calloc(1, sizeof(struct sl_pp_macro));
51 if (macro) {
52 _macro_init(macro);
53 }
54 return macro;
55 }
56
57 static void
58 _macro_destroy(struct sl_pp_macro *macro)
59 {
60 struct sl_pp_macro_formal_arg *arg = macro->arg;
61
62 while (arg) {
63 struct sl_pp_macro_formal_arg *next_arg = arg->next;
64
65 free(arg);
66 arg = next_arg;
67 }
68
69 free(macro->body);
70 }
71
72 void
73 sl_pp_macro_free(struct sl_pp_macro *macro)
74 {
75 while (macro) {
76 struct sl_pp_macro *next_macro = macro->next;
77
78 _macro_destroy(macro);
79 free(macro);
80 macro = next_macro;
81 }
82 }
83
84 void
85 sl_pp_macro_reset(struct sl_pp_macro *macro)
86 {
87 _macro_destroy(macro);
88 _macro_init(macro);
89 }
90
91 static int
92 _out_number(struct sl_pp_context *context,
93 struct sl_pp_process_state *state,
94 unsigned int number)
95 {
96 char buf[32];
97 struct sl_pp_token_info ti;
98
99 sprintf(buf, "%u", number);
100
101 ti.token = SL_PP_UINT;
102 ti.data._uint = sl_pp_context_add_unique_str(context, buf);
103 if (sl_pp_process_out(state, &ti)) {
104 strcpy(context->error_msg, "out of memory");
105 return -1;
106 }
107
108 return 0;
109 }
110
111 int
112 sl_pp_macro_expand(struct sl_pp_context *context,
113 struct sl_pp_token_buffer *tokens,
114 struct sl_pp_macro *local,
115 struct sl_pp_process_state *state,
116 enum sl_pp_macro_expand_behaviour behaviour)
117 {
118 int mute = (behaviour == sl_pp_macro_expand_mute);
119 struct sl_pp_token_info input;
120 int macro_name;
121 struct sl_pp_macro *macro = NULL;
122 struct sl_pp_macro *actual_arg = NULL;
123 unsigned int j;
124
125 if (sl_pp_token_buffer_get(tokens, &input)) {
126 return -1;
127 }
128
129 if (input.token != SL_PP_IDENTIFIER) {
130 strcpy(context->error_msg, "expected an identifier");
131 return -1;
132 }
133
134 macro_name = input.data.identifier;
135
136 /* First look for predefined macros.
137 */
138
139 if (macro_name == context->dict.___LINE__) {
140 if (!mute && _out_number(context, state, context->line)) {
141 return -1;
142 }
143 return 0;
144 }
145 if (macro_name == context->dict.___FILE__) {
146 if (!mute && _out_number(context, state, context->file)) {
147 return -1;
148 }
149 return 0;
150 }
151 if (macro_name == context->dict.___VERSION__) {
152 if (!mute && _out_number(context, state, 110)) {
153 return -1;
154 }
155 return 0;
156 }
157
158 for (j = 0; j < context->num_predefined; j++) {
159 if (macro_name == context->predefined[j].name) {
160 if (!mute) {
161 struct sl_pp_token_info ti;
162
163 ti.token = SL_PP_UINT;
164 ti.data._uint = context->predefined[j].value;
165 if (sl_pp_process_out(state, &ti)) {
166 strcpy(context->error_msg, "out of memory");
167 return -1;
168 }
169 }
170 return 0;
171 }
172 }
173
174 /* Replace extension names with 1.
175 */
176 for (j = 0; j < context->num_extensions; j++) {
177 if (macro_name == context->extensions[j].name) {
178 if (!mute && _out_number(context, state, 1)) {
179 return -1;
180 }
181 return 0;
182 }
183 }
184
185 if (local) {
186 for (macro = local; macro; macro = macro->next) {
187 if (macro->name == macro_name) {
188 break;
189 }
190 }
191 }
192
193 if (!macro) {
194 for (macro = context->macro; macro; macro = macro->next) {
195 if (macro->name == macro_name) {
196 break;
197 }
198 }
199 }
200
201 if (!macro) {
202 if (behaviour == sl_pp_macro_expand_unknown_to_0) {
203 if (_out_number(context, state, 0)) {
204 strcpy(context->error_msg, "out of memory");
205 return -1;
206 }
207 } else if (!mute) {
208 if (sl_pp_process_out(state, &input)) {
209 strcpy(context->error_msg, "out of memory");
210 return -1;
211 }
212 }
213 return 0;
214 }
215
216 if (macro->num_args >= 0) {
217 if (sl_pp_token_buffer_skip_white(tokens, &input)) {
218 return -1;
219 }
220 if (input.token != SL_PP_LPAREN) {
221 strcpy(context->error_msg, "expected `('");
222 return -1;
223 }
224 if (sl_pp_token_buffer_skip_white(tokens, &input)) {
225 return -1;
226 }
227 sl_pp_token_buffer_unget(tokens, &input);
228 }
229
230 if (macro->num_args > 0) {
231 struct sl_pp_macro_formal_arg *formal_arg = macro->arg;
232 struct sl_pp_macro **pmacro = &actual_arg;
233
234 for (j = 0; j < (unsigned int)macro->num_args; j++) {
235 struct sl_pp_process_state arg_state;
236 int done = 0;
237 unsigned int paren_nesting = 0;
238 struct sl_pp_token_info eof;
239
240 memset(&arg_state, 0, sizeof(arg_state));
241
242 while (!done) {
243 if (sl_pp_token_buffer_get(tokens, &input)) {
244 goto fail_arg;
245 }
246 switch (input.token) {
247 case SL_PP_WHITESPACE:
248 break;
249
250 case SL_PP_COMMA:
251 if (!paren_nesting) {
252 if (j < (unsigned int)macro->num_args - 1) {
253 done = 1;
254 } else {
255 strcpy(context->error_msg, "too many actual macro arguments");
256 goto fail_arg;
257 }
258 } else {
259 if (sl_pp_process_out(&arg_state, &input)) {
260 strcpy(context->error_msg, "out of memory");
261 goto fail_arg;
262 }
263 }
264 break;
265
266 case SL_PP_LPAREN:
267 paren_nesting++;
268 if (sl_pp_process_out(&arg_state, &input)) {
269 goto oom_arg;
270 }
271 break;
272
273 case SL_PP_RPAREN:
274 if (!paren_nesting) {
275 if (j == (unsigned int)macro->num_args - 1) {
276 done = 1;
277 } else {
278 strcpy(context->error_msg, "too few actual macro arguments");
279 goto fail_arg;
280 }
281 } else {
282 paren_nesting--;
283 if (sl_pp_process_out(&arg_state, &input)) {
284 goto oom_arg;
285 }
286 }
287 break;
288
289 case SL_PP_IDENTIFIER:
290 sl_pp_token_buffer_unget(tokens, &input);
291 if (sl_pp_macro_expand(context, tokens, local, &arg_state, sl_pp_macro_expand_normal)) {
292 goto fail_arg;
293 }
294 break;
295
296 case SL_PP_EOF:
297 strcpy(context->error_msg, "too few actual macro arguments");
298 goto fail_arg;
299
300 default:
301 if (sl_pp_process_out(&arg_state, &input)) {
302 goto oom_arg;
303 }
304 }
305 }
306
307 eof.token = SL_PP_EOF;
308 if (sl_pp_process_out(&arg_state, &eof)) {
309 goto oom_arg;
310 }
311
312 *pmacro = sl_pp_macro_new();
313 if (!*pmacro) {
314 goto oom_arg;
315 }
316
317 (**pmacro).name = formal_arg->name;
318 (**pmacro).body = arg_state.out;
319
320 formal_arg = formal_arg->next;
321 pmacro = &(**pmacro).next;
322
323 continue;
324
325 oom_arg:
326 strcpy(context->error_msg, "out of memory");
327 fail_arg:
328 free(arg_state.out);
329 goto fail;
330 }
331 }
332
333 /* Right paren for non-empty argument list has already been eaten. */
334 if (macro->num_args == 0) {
335 if (sl_pp_token_buffer_skip_white(tokens, &input)) {
336 goto fail;
337 }
338 if (input.token != SL_PP_RPAREN) {
339 strcpy(context->error_msg, "expected `)'");
340 goto fail;
341 }
342 }
343
344 /* XXX: This is all wrong, we should be ungetting all tokens
345 * back to the main token buffer.
346 */
347 {
348 struct sl_pp_token_buffer buffer;
349
350 /* Seek to the end.
351 */
352 for (j = 0; macro->body[j].token != SL_PP_EOF; j++) {
353 }
354 j++;
355
356 /* Create a context-less token buffer since we are not going to underrun
357 * its internal buffer.
358 */
359 if (sl_pp_token_buffer_init(&buffer, NULL)) {
360 strcpy(context->error_msg, "out of memory");
361 goto fail;
362 }
363
364 /* Unget the tokens in reverse order so later they will be fetched correctly.
365 */
366 for (; j > 0; j--) {
367 sl_pp_token_buffer_unget(&buffer, &macro->body[j - 1]);
368 }
369
370 /* Expand.
371 */
372 for (;;) {
373 struct sl_pp_token_info input;
374
375 sl_pp_token_buffer_get(&buffer, &input);
376 switch (input.token) {
377 case SL_PP_NEWLINE:
378 if (sl_pp_process_out(state, &input)) {
379 strcpy(context->error_msg, "out of memory");
380 sl_pp_token_buffer_destroy(&buffer);
381 goto fail;
382 }
383 break;
384
385 case SL_PP_IDENTIFIER:
386 sl_pp_token_buffer_unget(&buffer, &input);
387 if (sl_pp_macro_expand(context, &buffer, actual_arg, state, behaviour)) {
388 sl_pp_token_buffer_destroy(&buffer);
389 goto fail;
390 }
391 break;
392
393 case SL_PP_EOF:
394 sl_pp_token_buffer_destroy(&buffer);
395 sl_pp_macro_free(actual_arg);
396 return 0;
397
398 default:
399 if (!mute) {
400 if (sl_pp_process_out(state, &input)) {
401 strcpy(context->error_msg, "out of memory");
402 sl_pp_token_buffer_destroy(&buffer);
403 goto fail;
404 }
405 }
406 }
407 }
408 }
409
410 fail:
411 sl_pp_macro_free(actual_arg);
412 return -1;
413 }