]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ftepp.c
ugh todo: seed per ht, not per hash
[xonotic/gmqcc.git] / ftepp.c
1 /*
2  * Copyright (C) 2012
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 #include "gmqcc.h"
25 #include "lexer.h"
26
27 typedef struct {
28     bool on;
29     bool was_on;
30     bool had_else;
31 } ppcondition;
32
33 typedef struct {
34     int   token;
35     char *value;
36     /* a copy from the lexer */
37     union {
38         vector v;
39         int    i;
40         double f;
41         int    t; /* type */
42     } constval;
43 } pptoken;
44
45 typedef struct {
46     lex_ctx ctx;
47
48     char   *name;
49     char  **params;
50     /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
51     bool    has_params;
52
53     pptoken **output;
54 } ppmacro;
55
56 typedef struct {
57     lex_file    *lex;
58     int          token;
59     unsigned int errors;
60
61     bool         output_on;
62     ppcondition *conditions;
63     ppmacro    **macros;
64
65     char        *output_string;
66
67     char        *itemname;
68     char        *includename;
69 } ftepp_t;
70
71 #define ftepp_tokval(f) ((f)->lex->tok.value)
72 #define ftepp_ctx(f)    ((f)->lex->tok.ctx)
73
74 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
75 {
76     va_list ap;
77
78     ftepp->errors++;
79
80     va_start(ap, fmt);
81     con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
82     va_end(ap);
83 }
84
85 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
86 {
87     va_list ap;
88
89     ftepp->errors++;
90
91     va_start(ap, fmt);
92     con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
93     va_end(ap);
94 }
95
96 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
97 {
98     bool    r;
99     va_list ap;
100
101     va_start(ap, fmt);
102     r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
103     va_end(ap);
104     return r;
105 }
106
107 static pptoken *pptoken_make(ftepp_t *ftepp)
108 {
109     pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
110     token->token = ftepp->token;
111 #if 0
112     if (token->token == TOKEN_WHITE)
113         token->value = util_strdup(" ");
114     else
115 #else
116         token->value = util_strdup(ftepp_tokval(ftepp));
117 #endif
118     memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
119     return token;
120 }
121
122 static void pptoken_delete(pptoken *self)
123 {
124     mem_d(self->value);
125     mem_d(self);
126 }
127
128 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
129 {
130     ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
131
132     (void)ctx;
133     memset(macro, 0, sizeof(*macro));
134     macro->name = util_strdup(name);
135     return macro;
136 }
137
138 static void ppmacro_delete(ppmacro *self)
139 {
140     size_t i;
141     for (i = 0; i < vec_size(self->params); ++i)
142         mem_d(self->params[i]);
143     vec_free(self->params);
144     for (i = 0; i < vec_size(self->output); ++i)
145         pptoken_delete(self->output[i]);
146     vec_free(self->output);
147     mem_d(self->name);
148     mem_d(self);
149 }
150
151 static ftepp_t* ftepp_new()
152 {
153     ftepp_t *ftepp;
154
155     ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
156     memset(ftepp, 0, sizeof(*ftepp));
157
158     ftepp->output_on = true;
159
160     return ftepp;
161 }
162
163 static void ftepp_delete(ftepp_t *self)
164 {
165     size_t i;
166     if (self->itemname)
167         mem_d(self->itemname);
168     if (self->includename)
169         vec_free(self->includename);
170     for (i = 0; i < vec_size(self->macros); ++i)
171         ppmacro_delete(self->macros[i]);
172     vec_free(self->macros);
173     vec_free(self->conditions);
174     if (self->lex)
175         lex_close(self->lex);
176     mem_d(self);
177 }
178
179 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
180 {
181     if (ignore_cond || ftepp->output_on)
182     {
183         size_t len;
184         char  *data;
185         len = strlen(str);
186         data = vec_add(ftepp->output_string, len);
187         memcpy(data, str, len);
188     }
189 }
190
191 static void ftepp_update_output_condition(ftepp_t *ftepp)
192 {
193     size_t i;
194     ftepp->output_on = true;
195     for (i = 0; i < vec_size(ftepp->conditions); ++i)
196         ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
197 }
198
199 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
200 {
201     size_t i;
202     for (i = 0; i < vec_size(ftepp->macros); ++i) {
203         if (!strcmp(name, ftepp->macros[i]->name))
204             return ftepp->macros[i];
205     }
206     return NULL;
207 }
208
209 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
210 {
211     size_t i;
212     for (i = 0; i < vec_size(ftepp->macros); ++i) {
213         if (!strcmp(name, ftepp->macros[i]->name)) {
214             vec_remove(ftepp->macros, i, 1);
215             return;
216         }
217     }
218 }
219
220 static inline int ftepp_next(ftepp_t *ftepp)
221 {
222     return (ftepp->token = lex_do(ftepp->lex));
223 }
224
225 /* Important: this does not skip newlines! */
226 static bool ftepp_skipspace(ftepp_t *ftepp)
227 {
228     if (ftepp->token != TOKEN_WHITE)
229         return true;
230     while (ftepp_next(ftepp) == TOKEN_WHITE) {}
231     if (ftepp->token >= TOKEN_EOF) {
232         ftepp_error(ftepp, "unexpected end of preprocessor directive");
233         return false;
234     }
235     return true;
236 }
237
238 /* this one skips EOLs as well */
239 static bool ftepp_skipallwhite(ftepp_t *ftepp)
240 {
241     if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
242         return true;
243     do {
244         ftepp_next(ftepp);
245     } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
246     if (ftepp->token >= TOKEN_EOF) {
247         ftepp_error(ftepp, "unexpected end of preprocessor directive");
248         return false;
249     }
250     return true;
251 }
252
253 /**
254  * The huge macro parsing code...
255  */
256 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
257 {
258     do {
259         ftepp_next(ftepp);
260         if (!ftepp_skipspace(ftepp))
261             return false;
262         if (ftepp->token == ')')
263             break;
264         switch (ftepp->token) {
265             case TOKEN_IDENT:
266             case TOKEN_TYPENAME:
267             case TOKEN_KEYWORD:
268                 break;
269             default:
270                 ftepp_error(ftepp, "unexpected token in parameter list");
271                 return false;
272         }
273         vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
274         ftepp_next(ftepp);
275         if (!ftepp_skipspace(ftepp))
276             return false;
277     } while (ftepp->token == ',');
278     if (ftepp->token != ')') {
279         ftepp_error(ftepp, "expected closing paren after macro parameter list");
280         return false;
281     }
282     ftepp_next(ftepp);
283     /* skipspace happens in ftepp_define */
284     return true;
285 }
286
287 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
288 {
289     pptoken *ptok;
290     while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
291         ptok = pptoken_make(ftepp);
292         vec_push(macro->output, ptok);
293         ftepp_next(ftepp);
294     }
295     /* recursive expansion can cause EOFs here */
296     if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
297         ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
298         return false;
299     }
300     return true;
301 }
302
303 static bool ftepp_define(ftepp_t *ftepp)
304 {
305     ppmacro *macro;
306     size_t l = ftepp_ctx(ftepp).line;
307
308     (void)ftepp_next(ftepp);
309     if (!ftepp_skipspace(ftepp))
310         return false;
311
312     switch (ftepp->token) {
313         case TOKEN_IDENT:
314         case TOKEN_TYPENAME:
315         case TOKEN_KEYWORD:
316             macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
317             if (macro && ftepp->output_on) {
318                 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
319                     return false;
320                 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
321             }
322             macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
323             break;
324         default:
325             ftepp_error(ftepp, "expected macro name");
326             return false;
327     }
328
329     (void)ftepp_next(ftepp);
330
331     if (ftepp->token == '(') {
332         macro->has_params = true;
333         if (!ftepp_define_params(ftepp, macro))
334             return false;
335     }
336
337     if (!ftepp_skipspace(ftepp))
338         return false;
339
340     if (!ftepp_define_body(ftepp, macro))
341         return false;
342
343     if (ftepp->output_on)
344         vec_push(ftepp->macros, macro);
345     else {
346         ppmacro_delete(macro);
347     }
348
349     for (; l < ftepp_ctx(ftepp).line; ++l)
350         ftepp_out(ftepp, "\n", true);
351     return true;
352 }
353
354 /**
355  * When a macro is used we have to handle parameters as well
356  * as special-concatenation via ## or stringification via #
357  *
358  * Note: parenthesis can nest, so FOO((a),b) is valid, but only
359  * this kind of parens. Curly braces or [] don't count towards the
360  * paren-level.
361  */
362 typedef struct {
363     pptoken **tokens;
364 } macroparam;
365
366 static void macroparam_clean(macroparam *self)
367 {
368     size_t i;
369     for (i = 0; i < vec_size(self->tokens); ++i)
370         pptoken_delete(self->tokens[i]);
371     vec_free(self->tokens);
372 }
373
374 /* need to leave the last token up */
375 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
376 {
377     macroparam *params = NULL;
378     pptoken    *ptok;
379     macroparam  mp;
380     size_t      parens = 0;
381     size_t      i;
382
383     if (!ftepp_skipallwhite(ftepp))
384         return false;
385     while (ftepp->token != ')') {
386         mp.tokens = NULL;
387         if (!ftepp_skipallwhite(ftepp))
388             return false;
389         while (parens || ftepp->token != ',') {
390             if (ftepp->token == '(')
391                 ++parens;
392             else if (ftepp->token == ')') {
393                 if (!parens)
394                     break;
395                 --parens;
396             }
397             ptok = pptoken_make(ftepp);
398             vec_push(mp.tokens, ptok);
399             if (ftepp_next(ftepp) >= TOKEN_EOF) {
400                 ftepp_error(ftepp, "unexpected EOF in macro call");
401                 goto on_error;
402             }
403         }
404         vec_push(params, mp);
405         mp.tokens = NULL;
406         if (ftepp->token == ')')
407             break;
408         if (ftepp->token != ',') {
409             ftepp_error(ftepp, "expected closing paren or comma in macro call");
410             goto on_error;
411         }
412         if (ftepp_next(ftepp) >= TOKEN_EOF) {
413             ftepp_error(ftepp, "unexpected EOF in macro call");
414             goto on_error;
415         }
416     }
417     /* need to leave that up
418     if (ftepp_next(ftepp) >= TOKEN_EOF) {
419         ftepp_error(ftepp, "unexpected EOF in macro call");
420         goto on_error;
421     }
422     */
423     *out_params = params;
424     return true;
425
426 on_error:
427     if (mp.tokens)
428         macroparam_clean(&mp);
429     for (i = 0; i < vec_size(params); ++i)
430         macroparam_clean(&params[i]);
431     vec_free(params);
432     return false;
433 }
434
435 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
436 {
437     size_t i;
438     for (i = 0; i < vec_size(macro->params); ++i) {
439         if (!strcmp(macro->params[i], name)) {
440             *idx = i;
441             return true;
442         }
443     }
444     return false;
445 }
446
447 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
448 {
449     char        chs[2];
450     const char *ch;
451     chs[1] = 0;
452     switch (token->token) {
453         case TOKEN_STRINGCONST:
454             ch = token->value;
455             while (*ch) {
456                 /* in preprocessor mode strings already are string,
457                  * so we don't get actual newline bytes here.
458                  * Still need to escape backslashes and quotes.
459                  */
460                 switch (*ch) {
461                     case '\\': ftepp_out(ftepp, "\\\\", false); break;
462                     case '"':  ftepp_out(ftepp, "\\\"", false); break;
463                     default:
464                         chs[0] = *ch;
465                         ftepp_out(ftepp, chs, false);
466                         break;
467                 }
468                 ++ch;
469             }
470             break;
471         case TOKEN_WHITE:
472             ftepp_out(ftepp, " ", false);
473             break;
474         case TOKEN_EOL:
475             ftepp_out(ftepp, "\\n", false);
476             break;
477         default:
478             ftepp_out(ftepp, token->value, false);
479             break;
480     }
481 }
482
483 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
484 {
485     size_t i;
486     ftepp_out(ftepp, "\"", false);
487     for (i = 0; i < vec_size(param->tokens); ++i)
488         ftepp_stringify_token(ftepp, param->tokens[i]);
489     ftepp_out(ftepp, "\"", false);
490 }
491
492 static void ftepp_recursion_header(ftepp_t *ftepp)
493 {
494     ftepp_out(ftepp, "\n#pragma push(line)\n", false);
495 }
496
497 static void ftepp_recursion_footer(ftepp_t *ftepp)
498 {
499     ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
500 }
501
502 static bool ftepp_preprocess(ftepp_t *ftepp);
503 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
504 {
505     char     *old_string = ftepp->output_string;
506     lex_file *old_lexer = ftepp->lex;
507     bool retval = true;
508
509     size_t    o, pi, pv;
510     lex_file *inlex;
511
512     int nextok;
513
514     /* really ... */
515     if (!vec_size(macro->output))
516         return true;
517
518     ftepp->output_string = NULL;
519     for (o = 0; o < vec_size(macro->output); ++o) {
520         pptoken *out = macro->output[o];
521         switch (out->token) {
522             case TOKEN_IDENT:
523             case TOKEN_TYPENAME:
524             case TOKEN_KEYWORD:
525                 if (!macro_params_find(macro, out->value, &pi)) {
526                     ftepp_out(ftepp, out->value, false);
527                     break;
528                 } else {
529                     for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
530                         out = params[pi].tokens[pv];
531                         if (out->token == TOKEN_EOL)
532                             ftepp_out(ftepp, "\n", false);
533                         else
534                             ftepp_out(ftepp, out->value, false);
535                     }
536                 }
537                 break;
538             case '#':
539                 if (o + 1 < vec_size(macro->output)) {
540                     nextok = macro->output[o+1]->token;
541                     if (nextok == '#') {
542                         /* raw concatenation */
543                         ++o;
544                         break;
545                     }
546                     if ( (nextok == TOKEN_IDENT    ||
547                           nextok == TOKEN_KEYWORD  ||
548                           nextok == TOKEN_TYPENAME) &&
549                         macro_params_find(macro, macro->output[o+1]->value, &pi))
550                     {
551                         ++o;
552                         ftepp_stringify(ftepp, &params[pi]);
553                         break;
554                     }
555                 }
556                 ftepp_out(ftepp, "#", false);
557                 break;
558             case TOKEN_EOL:
559                 ftepp_out(ftepp, "\n", false);
560                 break;
561             default:
562                 ftepp_out(ftepp, out->value, false);
563                 break;
564         }
565     }
566     vec_push(ftepp->output_string, 0);
567     /* Now run the preprocessor recursively on this string buffer */
568     /*
569     printf("__________\n%s\n=========\n", ftepp->output_string);
570     */
571     inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
572     if (!inlex) {
573         ftepp_error(ftepp, "internal error: failed to instantiate lexer");
574         retval = false;
575         goto cleanup;
576     }
577     ftepp->output_string = old_string;
578     ftepp->lex = inlex;
579     ftepp_recursion_header(ftepp);
580     if (!ftepp_preprocess(ftepp)) {
581         lex_close(ftepp->lex);
582         retval = false;
583         goto cleanup;
584     }
585     ftepp_recursion_footer(ftepp);
586     old_string = ftepp->output_string;
587
588 cleanup:
589     ftepp->lex           = old_lexer;
590     ftepp->output_string = old_string;
591     return retval;
592 }
593
594 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
595 {
596     size_t     o;
597     macroparam *params = NULL;
598     bool        retval = true;
599
600     if (!macro->has_params) {
601         if (!ftepp_macro_expand(ftepp, macro, NULL))
602             return false;
603         ftepp_next(ftepp);
604         return true;
605     }
606     ftepp_next(ftepp);
607
608     if (!ftepp_skipallwhite(ftepp))
609         return false;
610
611     if (ftepp->token != '(') {
612         ftepp_error(ftepp, "expected macro parameters in parenthesis");
613         return false;
614     }
615
616     ftepp_next(ftepp);
617     if (!ftepp_macro_call_params(ftepp, &params))
618         return false;
619
620     if (vec_size(params) != vec_size(macro->params)) {
621         ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
622                     (unsigned int)vec_size(macro->params),
623                     (unsigned int)vec_size(params));
624         retval = false;
625         goto cleanup;
626     }
627
628     if (!ftepp_macro_expand(ftepp, macro, params))
629         retval = false;
630     ftepp_next(ftepp);
631
632 cleanup:
633     for (o = 0; o < vec_size(params); ++o)
634         macroparam_clean(&params[o]);
635     vec_free(params);
636     return retval;
637 }
638
639 /**
640  * #if - the FTEQCC way:
641  *    defined(FOO) => true if FOO was #defined regardless of parameters or contents
642  *    <numbers>    => True if the number is not 0
643  *    !<factor>    => True if the factor yields false
644  *    !!<factor>   => ERROR on 2 or more unary nots
645  *    <macro>      => becomes the macro's FIRST token regardless of parameters
646  *    <e> && <e>   => True if both expressions are true
647  *    <e> || <e>   => True if either expression is true
648  *    <string>     => False
649  *    <ident>      => False (remember for macros the <macro> rule applies instead)
650  * Unary + and - are weird and wrong in fteqcc so we don't allow them
651  * parenthesis in expressions are allowed
652  * parameter lists on macros are errors
653  * No mathematical calculations are executed
654  */
655 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
656 static bool ftepp_if_op(ftepp_t *ftepp)
657 {
658     ftepp->lex->flags.noops = false;
659     ftepp_next(ftepp);
660     if (!ftepp_skipspace(ftepp))
661         return false;
662     ftepp->lex->flags.noops = true;
663     return true;
664 }
665 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
666 {
667     ppmacro *macro;
668     bool     wasnot = false;
669
670     if (!ftepp_skipspace(ftepp))
671         return false;
672
673     while (ftepp->token == '!') {
674         wasnot = true;
675         ftepp_next(ftepp);
676         if (!ftepp_skipspace(ftepp))
677             return false;
678     }
679
680     switch (ftepp->token) {
681         case TOKEN_IDENT:
682         case TOKEN_TYPENAME:
683         case TOKEN_KEYWORD:
684             if (!strcmp(ftepp_tokval(ftepp), "defined")) {
685                 ftepp_next(ftepp);
686                 if (!ftepp_skipspace(ftepp))
687                     return false;
688                 if (ftepp->token != '(') {
689                     ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
690                     return false;
691                 }
692                 ftepp_next(ftepp);
693                 if (!ftepp_skipspace(ftepp))
694                     return false;
695                 if (ftepp->token != TOKEN_IDENT &&
696                     ftepp->token != TOKEN_TYPENAME &&
697                     ftepp->token != TOKEN_KEYWORD)
698                 {
699                     ftepp_error(ftepp, "defined() used on an unexpected token type");
700                     return false;
701                 }
702                 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
703                 *out = !!macro;
704                 ftepp_next(ftepp);
705                 if (!ftepp_skipspace(ftepp))
706                     return false;
707                 if (ftepp->token != ')') {
708                     ftepp_error(ftepp, "expected closing paren");
709                     return false;
710                 }
711                 break;
712             }
713
714             macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
715             if (!macro || !vec_size(macro->output)) {
716                 *out = false;
717                 *value_out = 0;
718             } else {
719                 /* This does not expand recursively! */
720                 switch (macro->output[0]->token) {
721                     case TOKEN_INTCONST:
722                         *value_out = macro->output[0]->constval.i;
723                         *out = !!(macro->output[0]->constval.i);
724                         break;
725                     case TOKEN_FLOATCONST:
726                         *value_out = macro->output[0]->constval.f;
727                         *out = !!(macro->output[0]->constval.f);
728                         break;
729                     default:
730                         *out = false;
731                         break;
732                 }
733             }
734             break;
735         case TOKEN_STRINGCONST:
736             *out = false;
737             break;
738         case TOKEN_INTCONST:
739             *value_out = ftepp->lex->tok.constval.i;
740             *out = !!(ftepp->lex->tok.constval.i);
741             break;
742         case TOKEN_FLOATCONST:
743             *value_out = ftepp->lex->tok.constval.f;
744             *out = !!(ftepp->lex->tok.constval.f);
745             break;
746
747         case '(':
748             ftepp_next(ftepp);
749             if (!ftepp_if_expr(ftepp, out, value_out))
750                 return false;
751             if (ftepp->token != ')') {
752                 ftepp_error(ftepp, "expected closing paren in #if expression");
753                 return false;
754             }
755             break;
756
757         default:
758             ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
759             return false;
760     }
761     if (wasnot) {
762         *out = !*out;
763         *value_out = (*out ? 1 : 0);
764     }
765     return true;
766 }
767
768 /*
769 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
770 {
771     if (!ftepp_next(ftepp))
772         return false;
773     return ftepp_if_value(ftepp, out, value_out);
774 }
775 */
776
777 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
778 {
779     if (!ftepp_if_value(ftepp, out, value_out))
780         return false;
781
782     if (!ftepp_if_op(ftepp))
783         return false;
784
785     if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
786         return true;
787
788     /* FTEQCC is all right-associative and no precedence here */
789     if (!strcmp(ftepp_tokval(ftepp), "&&") ||
790         !strcmp(ftepp_tokval(ftepp), "||"))
791     {
792         bool next = false;
793         char opc  = ftepp_tokval(ftepp)[0];
794         double nextvalue;
795
796         (void)nextvalue;
797         if (!ftepp_next(ftepp))
798             return false;
799         if (!ftepp_if_expr(ftepp, &next, &nextvalue))
800             return false;
801
802         if (opc == '&')
803             *out = *out && next;
804         else
805             *out = *out || next;
806
807         *value_out = (*out ? 1 : 0);
808         return true;
809     }
810     else if (!strcmp(ftepp_tokval(ftepp), "==") ||
811              !strcmp(ftepp_tokval(ftepp), "!=") ||
812              !strcmp(ftepp_tokval(ftepp), ">=") ||
813              !strcmp(ftepp_tokval(ftepp), "<=") ||
814              !strcmp(ftepp_tokval(ftepp), ">") ||
815              !strcmp(ftepp_tokval(ftepp), "<"))
816     {
817         bool next = false;
818         const char opc0 = ftepp_tokval(ftepp)[0];
819         const char opc1 = ftepp_tokval(ftepp)[1];
820         double other;
821
822         if (!ftepp_next(ftepp))
823             return false;
824         if (!ftepp_if_expr(ftepp, &next, &other))
825             return false;
826
827         if (opc0 == '=')
828             *out = (*value_out == other);
829         else if (opc0 == '!')
830             *out = (*value_out != other);
831         else if (opc0 == '>') {
832             if (opc1 == '=') *out = (*value_out >= other);
833             else             *out = (*value_out > other);
834         }
835         else if (opc0 == '<') {
836             if (opc1 == '=') *out = (*value_out <= other);
837             else             *out = (*value_out < other);
838         }
839         *value_out = (*out ? 1 : 0);
840
841         return true;
842     }
843     else {
844         ftepp_error(ftepp, "junk after #if");
845         return false;
846     }
847 }
848
849 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
850 {
851     bool result = false;
852     double dummy = 0;
853
854     memset(cond, 0, sizeof(*cond));
855     (void)ftepp_next(ftepp);
856
857     if (!ftepp_skipspace(ftepp))
858         return false;
859     if (ftepp->token == TOKEN_EOL) {
860         ftepp_error(ftepp, "expected expression for #if-directive");
861         return false;
862     }
863
864     if (!ftepp_if_expr(ftepp, &result, &dummy))
865         return false;
866
867     cond->on = result;
868     return true;
869 }
870
871 /**
872  * ifdef is rather simple
873  */
874 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
875 {
876     ppmacro *macro;
877     memset(cond, 0, sizeof(*cond));
878     (void)ftepp_next(ftepp);
879     if (!ftepp_skipspace(ftepp))
880         return false;
881
882     switch (ftepp->token) {
883         case TOKEN_IDENT:
884         case TOKEN_TYPENAME:
885         case TOKEN_KEYWORD:
886             macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
887             break;
888         default:
889             ftepp_error(ftepp, "expected macro name");
890             return false;
891     }
892
893     (void)ftepp_next(ftepp);
894     if (!ftepp_skipspace(ftepp))
895         return false;
896     /* relaxing this condition
897     if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
898         ftepp_error(ftepp, "stray tokens after #ifdef");
899         return false;
900     }
901     */
902     cond->on = !!macro;
903     return true;
904 }
905
906 /**
907  * undef is also simple
908  */
909 static bool ftepp_undef(ftepp_t *ftepp)
910 {
911     (void)ftepp_next(ftepp);
912     if (!ftepp_skipspace(ftepp))
913         return false;
914
915     if (ftepp->output_on) {
916         switch (ftepp->token) {
917             case TOKEN_IDENT:
918             case TOKEN_TYPENAME:
919             case TOKEN_KEYWORD:
920                 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
921                 break;
922             default:
923                 ftepp_error(ftepp, "expected macro name");
924                 return false;
925         }
926     }
927
928     (void)ftepp_next(ftepp);
929     if (!ftepp_skipspace(ftepp))
930         return false;
931     /* relaxing this condition
932     if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
933         ftepp_error(ftepp, "stray tokens after #ifdef");
934         return false;
935     }
936     */
937     return true;
938 }
939
940 /* Special unescape-string function which skips a leading quote
941  * and stops at a quote, not just at \0
942  */
943 static void unescape(const char *str, char *out) {
944     ++str;
945     while (*str && *str != '"') {
946         if (*str == '\\') {
947             ++str;
948             switch (*str) {
949                 case '\\': *out++ = *str; break;
950                 case '"':  *out++ = *str; break;
951                 case 'a':  *out++ = '\a'; break;
952                 case 'b':  *out++ = '\b'; break;
953                 case 'r':  *out++ = '\r'; break;
954                 case 'n':  *out++ = '\n'; break;
955                 case 't':  *out++ = '\t'; break;
956                 case 'f':  *out++ = '\f'; break;
957                 case 'v':  *out++ = '\v'; break;
958                 default:
959                     *out++ = '\\';
960                     *out++ = *str;
961                     break;
962             }
963             ++str;
964             continue;
965         }
966
967         *out++ = *str++;
968     }
969     *out = 0;
970 }
971
972 static char *ftepp_include_find_path(const char *file, const char *pathfile)
973 {
974     FILE       *fp;
975     char       *filename = NULL;
976     const char *last_slash;
977     size_t      len;
978
979     if (!pathfile)
980         return NULL;
981
982     last_slash = strrchr(pathfile, '/');
983
984     if (last_slash) {
985         len = last_slash - pathfile;
986         memcpy(vec_add(filename, len), pathfile, len);
987         vec_push(filename, '/');
988     }
989
990     len = strlen(file);
991     memcpy(vec_add(filename, len+1), file, len);
992     vec_last(filename) = 0;
993
994     fp = util_fopen(filename, "rb");
995     if (fp) {
996         fclose(fp);
997         return filename;
998     }
999     vec_free(filename);
1000     return NULL;
1001 }
1002
1003 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1004 {
1005     char *filename = NULL;
1006
1007     filename = ftepp_include_find_path(file, ftepp->includename);
1008     if (!filename)
1009         filename = ftepp_include_find_path(file, ftepp->itemname);
1010     return filename;
1011 }
1012
1013 /**
1014  * Include a file.
1015  * FIXME: do we need/want a -I option?
1016  * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1017  */
1018 static bool ftepp_include(ftepp_t *ftepp)
1019 {
1020     lex_file *old_lexer = ftepp->lex;
1021     lex_file *inlex;
1022     lex_ctx  ctx;
1023     char     lineno[128];
1024     char     *filename;
1025     char     *old_includename;
1026
1027     (void)ftepp_next(ftepp);
1028     if (!ftepp_skipspace(ftepp))
1029         return false;
1030
1031     if (ftepp->token != TOKEN_STRINGCONST) {
1032         ftepp_error(ftepp, "expected filename to include");
1033         return false;
1034     }
1035
1036     ctx = ftepp_ctx(ftepp);
1037
1038     unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1039
1040     ftepp_out(ftepp, "\n#pragma file(", false);
1041     ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1042     ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1043
1044     filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1045     if (!filename) {
1046         ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1047         return false;
1048     }
1049     inlex = lex_open(filename);
1050     if (!inlex) {
1051         ftepp_error(ftepp, "open failed on include file `%s`", filename);
1052         vec_free(filename);
1053         return false;
1054     }
1055     ftepp->lex = inlex;
1056     old_includename = ftepp->includename;
1057     ftepp->includename = filename;
1058     if (!ftepp_preprocess(ftepp)) {
1059         vec_free(ftepp->includename);
1060         ftepp->includename = old_includename;
1061         lex_close(ftepp->lex);
1062         ftepp->lex = old_lexer;
1063         return false;
1064     }
1065     vec_free(ftepp->includename);
1066     ftepp->includename = old_includename;
1067     lex_close(ftepp->lex);
1068     ftepp->lex = old_lexer;
1069
1070     ftepp_out(ftepp, "\n#pragma file(", false);
1071     ftepp_out(ftepp, ctx.file, false);
1072     snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1073     ftepp_out(ftepp, lineno, false);
1074
1075     /* skip the line */
1076     (void)ftepp_next(ftepp);
1077     if (!ftepp_skipspace(ftepp))
1078         return false;
1079     if (ftepp->token != TOKEN_EOL) {
1080         ftepp_error(ftepp, "stray tokens after #include");
1081         return false;
1082     }
1083     (void)ftepp_next(ftepp);
1084
1085     return true;
1086 }
1087
1088 /* Basic structure handlers */
1089 static bool ftepp_else_allowed(ftepp_t *ftepp)
1090 {
1091     if (!vec_size(ftepp->conditions)) {
1092         ftepp_error(ftepp, "#else without #if");
1093         return false;
1094     }
1095     if (vec_last(ftepp->conditions).had_else) {
1096         ftepp_error(ftepp, "multiple #else for a single #if");
1097         return false;
1098     }
1099     return true;
1100 }
1101
1102 static bool ftepp_hash(ftepp_t *ftepp)
1103 {
1104     ppcondition cond;
1105     ppcondition *pc;
1106
1107     lex_ctx ctx = ftepp_ctx(ftepp);
1108
1109     if (!ftepp_skipspace(ftepp))
1110         return false;
1111
1112     switch (ftepp->token) {
1113         case TOKEN_KEYWORD:
1114         case TOKEN_IDENT:
1115         case TOKEN_TYPENAME:
1116             if (!strcmp(ftepp_tokval(ftepp), "define")) {
1117                 return ftepp_define(ftepp);
1118             }
1119             else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1120                 return ftepp_undef(ftepp);
1121             }
1122             else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1123                 if (!ftepp_ifdef(ftepp, &cond))
1124                     return false;
1125                 cond.was_on = cond.on;
1126                 vec_push(ftepp->conditions, cond);
1127                 ftepp->output_on = ftepp->output_on && cond.on;
1128                 break;
1129             }
1130             else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1131                 if (!ftepp_ifdef(ftepp, &cond))
1132                     return false;
1133                 cond.on = !cond.on;
1134                 cond.was_on = cond.on;
1135                 vec_push(ftepp->conditions, cond);
1136                 ftepp->output_on = ftepp->output_on && cond.on;
1137                 break;
1138             }
1139             else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1140                 if (!ftepp_else_allowed(ftepp))
1141                     return false;
1142                 if (!ftepp_ifdef(ftepp, &cond))
1143                     return false;
1144                 pc = &vec_last(ftepp->conditions);
1145                 pc->on     = !pc->was_on && cond.on;
1146                 pc->was_on = pc->was_on || pc->on;
1147                 ftepp_update_output_condition(ftepp);
1148                 break;
1149             }
1150             else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1151                 if (!ftepp_else_allowed(ftepp))
1152                     return false;
1153                 if (!ftepp_ifdef(ftepp, &cond))
1154                     return false;
1155                 cond.on = !cond.on;
1156                 pc = &vec_last(ftepp->conditions);
1157                 pc->on     = !pc->was_on && cond.on;
1158                 pc->was_on = pc->was_on || pc->on;
1159                 ftepp_update_output_condition(ftepp);
1160                 break;
1161             }
1162             else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1163                 if (!ftepp_else_allowed(ftepp))
1164                     return false;
1165                 if (!ftepp_if(ftepp, &cond))
1166                     return false;
1167                 pc = &vec_last(ftepp->conditions);
1168                 pc->on     = !pc->was_on && cond.on;
1169                 pc->was_on = pc->was_on  || pc->on;
1170                 ftepp_update_output_condition(ftepp);
1171                 break;
1172             }
1173             else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1174                 if (!ftepp_if(ftepp, &cond))
1175                     return false;
1176                 cond.was_on = cond.on;
1177                 vec_push(ftepp->conditions, cond);
1178                 ftepp->output_on = ftepp->output_on && cond.on;
1179                 break;
1180             }
1181             else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1182                 if (!ftepp_else_allowed(ftepp))
1183                     return false;
1184                 pc = &vec_last(ftepp->conditions);
1185                 pc->on = !pc->was_on;
1186                 pc->had_else = true;
1187                 ftepp_next(ftepp);
1188                 ftepp_update_output_condition(ftepp);
1189                 break;
1190             }
1191             else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1192                 if (!vec_size(ftepp->conditions)) {
1193                     ftepp_error(ftepp, "#endif without #if");
1194                     return false;
1195                 }
1196                 vec_pop(ftepp->conditions);
1197                 ftepp_next(ftepp);
1198                 ftepp_update_output_condition(ftepp);
1199                 break;
1200             }
1201             else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1202                 return ftepp_include(ftepp);
1203             }
1204             else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1205                 ftepp_out(ftepp, "#", false);
1206                 break;
1207             }
1208             else {
1209                 if (ftepp->output_on) {
1210                     ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1211                     return false;
1212                 } else {
1213                     ftepp_next(ftepp);
1214                     break;
1215                 }
1216             }
1217             /* break; never reached */
1218         default:
1219             ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1220             return false;
1221         case TOKEN_EOL:
1222             ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1223             return false;
1224         case TOKEN_EOF:
1225             ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1226             return false;
1227
1228         /* Builtins! Don't forget the builtins! */
1229         case TOKEN_INTCONST:
1230         case TOKEN_FLOATCONST:
1231             ftepp_out(ftepp, "#", false);
1232             return true;
1233     }
1234     if (!ftepp_skipspace(ftepp))
1235         return false;
1236     return true;
1237 }
1238
1239 static bool ftepp_preprocess(ftepp_t *ftepp)
1240 {
1241     ppmacro *macro;
1242     bool     newline = true;
1243
1244     ftepp->lex->flags.preprocessing = true;
1245     ftepp->lex->flags.mergelines    = false;
1246     ftepp->lex->flags.noops         = true;
1247
1248     ftepp_next(ftepp);
1249     do
1250     {
1251         if (ftepp->token >= TOKEN_EOF)
1252             break;
1253 #if 0
1254         newline = true;
1255 #endif
1256
1257         switch (ftepp->token) {
1258             case TOKEN_KEYWORD:
1259             case TOKEN_IDENT:
1260             case TOKEN_TYPENAME:
1261                 if (ftepp->output_on)
1262                     macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1263                 else
1264                     macro = NULL;
1265                 if (!macro) {
1266                     ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1267                     ftepp_next(ftepp);
1268                     break;
1269                 }
1270                 if (!ftepp_macro_call(ftepp, macro))
1271                     ftepp->token = TOKEN_ERROR;
1272                 break;
1273             case '#':
1274                 if (!newline) {
1275                     ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1276                     ftepp_next(ftepp);
1277                     break;
1278                 }
1279                 ftepp->lex->flags.mergelines = true;
1280                 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1281                     ftepp_error(ftepp, "error in preprocessor directive");
1282                     ftepp->token = TOKEN_ERROR;
1283                     break;
1284                 }
1285                 if (!ftepp_hash(ftepp))
1286                     ftepp->token = TOKEN_ERROR;
1287                 ftepp->lex->flags.mergelines = false;
1288                 break;
1289             case TOKEN_EOL:
1290                 newline = true;
1291                 ftepp_out(ftepp, "\n", true);
1292                 ftepp_next(ftepp);
1293                 break;
1294             case TOKEN_WHITE:
1295                 /* same as default but don't set newline=false */
1296                 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1297                 ftepp_next(ftepp);
1298                 break;
1299             default:
1300                 newline = false;
1301                 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1302                 ftepp_next(ftepp);
1303                 break;
1304         }
1305     } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1306
1307     /* force a 0 at the end but don't count it as added to the output */
1308     vec_push(ftepp->output_string, 0);
1309     vec_shrinkby(ftepp->output_string, 1);
1310
1311     return (ftepp->token == TOKEN_EOF);
1312 }
1313
1314 /* Like in parser.c - files keep the previous state so we have one global
1315  * preprocessor. Except here we will want to warn about dangling #ifs.
1316  */
1317 static ftepp_t *ftepp;
1318
1319 static bool ftepp_preprocess_done()
1320 {
1321     bool retval = true;
1322     if (vec_size(ftepp->conditions)) {
1323         if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1324             retval = false;
1325     }
1326     lex_close(ftepp->lex);
1327     ftepp->lex = NULL;
1328     if (ftepp->itemname) {
1329         mem_d(ftepp->itemname);
1330         ftepp->itemname = NULL;
1331     }
1332     return retval;
1333 }
1334
1335 bool ftepp_preprocess_file(const char *filename)
1336 {
1337     ftepp->lex = lex_open(filename);
1338     ftepp->itemname = util_strdup(filename);
1339     if (!ftepp->lex) {
1340         con_out("failed to open file \"%s\"\n", filename);
1341         return false;
1342     }
1343     if (!ftepp_preprocess(ftepp))
1344         return false;
1345     return ftepp_preprocess_done();
1346 }
1347
1348 bool ftepp_preprocess_string(const char *name, const char *str)
1349 {
1350     ftepp->lex = lex_open_string(str, strlen(str), name);
1351     ftepp->itemname = util_strdup(name);
1352     if (!ftepp->lex) {
1353         con_out("failed to create lexer for string \"%s\"\n", name);
1354         return false;
1355     }
1356     if (!ftepp_preprocess(ftepp))
1357         return false;
1358     return ftepp_preprocess_done();
1359 }
1360
1361
1362 void ftepp_add_macro(const char *name, const char *value) {
1363     char *create = NULL;
1364
1365     /* use saner path for empty macros */
1366     if (!value) {
1367         ftepp_add_define("__builtin__", name);
1368         return;
1369     }
1370
1371     vec_upload(create, "#define ", 8);
1372     vec_upload(create, name,  strlen(name));
1373     vec_push  (create, ' ');
1374     vec_upload(create, value, strlen(value));
1375     vec_push  (create, 0);
1376
1377     ftepp_preprocess_string("__builtin__", create);
1378     vec_free  (create);
1379 }
1380
1381 bool ftepp_init()
1382 {
1383     char minor[32];
1384     char major[32];
1385
1386     ftepp = ftepp_new();
1387     if (!ftepp)
1388         return false;
1389
1390     memset(minor, 0, sizeof(minor));
1391     memset(major, 0, sizeof(major));
1392
1393     /* set the right macro based on the selected standard */
1394     ftepp_add_define(NULL, "GMQCC");
1395     if (opts.standard == COMPILER_FTEQCC) {
1396         ftepp_add_define(NULL, "__STD_FTEQCC__");
1397         /* 1.00 */
1398         major[0] = '"';
1399         major[1] = '1';
1400         major[2] = '"';
1401
1402         minor[0] = '"';
1403         minor[1] = '0';
1404         minor[2] = '"';
1405     } else if (opts.standard == COMPILER_GMQCC) {
1406         ftepp_add_define(NULL, "__STD_GMQCC__");
1407         sprintf(major, "\"%d\"", GMQCC_VERSION_MAJOR);
1408         sprintf(minor, "\"%d\"", GMQCC_VERSION_MINOR);
1409     } else if (opts.standard == COMPILER_QCC) {
1410         ftepp_add_define(NULL, "__STD_QCC__");
1411         /* 1.0 */
1412         major[0] = '"';
1413         major[1] = '1';
1414         major[2] = '"';
1415
1416         minor[0] = '"';
1417         minor[1] = '0';
1418         minor[2] = '"';
1419     }
1420
1421     ftepp_add_macro("__STD_VERSION_MINOR__", minor);
1422     ftepp_add_macro("__STD_VERSION_MAJOR__", major);
1423
1424     return true;
1425 }
1426
1427 void ftepp_add_define(const char *source, const char *name)
1428 {
1429     ppmacro *macro;
1430     lex_ctx ctx = { "__builtin__", 0 };
1431     ctx.file = source;
1432     macro = ppmacro_new(ctx, name);
1433     vec_push(ftepp->macros, macro);
1434 }
1435
1436 const char *ftepp_get()
1437 {
1438     return ftepp->output_string;
1439 }
1440
1441 void ftepp_flush()
1442 {
1443     vec_free(ftepp->output_string);
1444 }
1445
1446 void ftepp_finish()
1447 {
1448     if (!ftepp)
1449         return;
1450     ftepp_delete(ftepp);
1451     ftepp = NULL;
1452 }