2 * Copyright (C) 2012, 2013
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
29 #define HT_MACROS 1024
39 /* a copy from the lexer */
53 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
60 typedef struct ftepp_s {
66 ppcondition *conditions;
68 ht macros; /* hashtable<string, ppmacro*> */
77 * Implement the predef subsystem now. We can do this safely with the
78 * help of lexer contexts.
80 static uint32_t ftepp_predef_countval = 0;
81 static uint32_t ftepp_predef_randval = 0;
84 char *ftepp_predef_date(lex_file *context) {
85 struct tm *itime = NULL;
87 char *value = (char*)mem_a(82);
88 /* 82 is enough for strftime but we also have " " in our string */
96 localtime_s(itime, &rtime);
98 itime = localtime(&rtime);
101 strftime(value, 82, "\"%b %d %Y\"", itime);
107 char *ftepp_predef_time(lex_file *context) {
108 struct tm *itime = NULL;
110 char *value = (char*)mem_a(82);
111 /* 82 is enough for strftime but we also have " " in our string */
119 localtime_s(itime, &rtime);
121 itime = localtime(&rtime);
124 strftime(value, 82, "\"%X\"", itime);
130 char *ftepp_predef_line(lex_file *context) {
132 util_asprintf(&value, "%d", (int)context->line);
136 char *ftepp_predef_file(lex_file *context) {
137 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
138 char *value = (char*)mem_a(length);
139 util_snprintf(value, length, "\"%s\"", context->name);
143 /* __COUNTER_LAST__ */
144 char *ftepp_predef_counterlast(lex_file *context) {
146 util_asprintf(&value, "%u", ftepp_predef_countval);
152 char *ftepp_predef_counter(lex_file *context) {
154 ftepp_predef_countval ++;
155 util_asprintf(&value, "%u", ftepp_predef_countval);
161 char *ftepp_predef_random(lex_file *context) {
163 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
164 util_asprintf(&value, "%u", ftepp_predef_randval);
169 /* __RANDOM_LAST__ */
170 char *ftepp_predef_randomlast(lex_file *context) {
172 util_asprintf(&value, "%u", ftepp_predef_randval);
178 char *ftepp_predef_timestamp(lex_file *context) {
183 if (stat(context->name, &finfo))
184 return util_strdup("\"<failed to determine timestamp>\"");
187 * ctime and its fucking annoying newline char, no worries, we're
188 * professionals here.
190 find = ctime(&finfo.st_mtime);
191 value = (char*)mem_a(strlen(find) + 1);
192 memcpy(&value[1], find, (size = strlen(find)) - 1);
202 char *(*func)(lex_file *);
205 static const ftepp_predef_t ftepp_predefs[] = {
206 { "__LINE__", &ftepp_predef_line },
207 { "__FILE__", &ftepp_predef_file },
208 { "__COUNTER__", &ftepp_predef_counter },
209 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
210 { "__RANDOM__", &ftepp_predef_random },
211 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
212 { "__DATE__", &ftepp_predef_date },
213 { "__TIME__", &ftepp_predef_time },
214 { "__TIME_STAMP__", &ftepp_predef_timestamp }
217 static GMQCC_INLINE int ftepp_predef_index(const char *name) {
218 /* no hashtable here, we simply check for one to exist the naive way */
220 for(i = 0; i < (int)(sizeof(ftepp_predefs)/sizeof(*ftepp_predefs)); i++)
221 if (!strcmp(ftepp_predefs[i].name, name))
226 bool ftepp_predef_exists(const char *name) {
227 return ftepp_predef_index(name) != -1;
230 /* singleton because we're allowed */
231 static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) {
232 int i = ftepp_predef_index(name);
233 return (i != -1) ? ftepp_predefs[i].func : NULL;
236 #define ftepp_tokval(f) ((f)->lex->tok.value)
237 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
239 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
246 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
250 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
257 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
261 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
267 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
272 static pptoken *pptoken_make(ftepp_t *ftepp)
274 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
275 token->token = ftepp->token;
277 if (token->token == TOKEN_WHITE)
278 token->value = util_strdup(" ");
281 token->value = util_strdup(ftepp_tokval(ftepp));
283 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
287 static GMQCC_INLINE void pptoken_delete(pptoken *self)
293 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
295 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
298 memset(macro, 0, sizeof(*macro));
299 macro->name = util_strdup(name);
303 static void ppmacro_delete(ppmacro *self)
306 for (i = 0; i < vec_size(self->params); ++i)
307 mem_d(self->params[i]);
308 vec_free(self->params);
309 for (i = 0; i < vec_size(self->output); ++i)
310 pptoken_delete(self->output[i]);
311 vec_free(self->output);
316 static ftepp_t* ftepp_new()
320 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
321 memset(ftepp, 0, sizeof(*ftepp));
323 ftepp->macros = util_htnew(HT_MACROS);
324 ftepp->output_on = true;
329 static GMQCC_INLINE void ftepp_flush_do(ftepp_t *self)
331 vec_free(self->output_string);
334 static void ftepp_delete(ftepp_t *self)
336 ftepp_flush_do(self);
338 mem_d(self->itemname);
339 if (self->includename)
340 vec_free(self->includename);
342 util_htrem(self->macros, (void (*)(void*))&ppmacro_delete);
344 vec_free(self->conditions);
346 lex_close(self->lex);
350 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
352 if (ignore_cond || ftepp->output_on)
357 data = vec_add(ftepp->output_string, len);
358 memcpy(data, str, len);
362 static GMQCC_INLINE void ftepp_update_output_condition(ftepp_t *ftepp)
365 ftepp->output_on = true;
366 for (i = 0; i < vec_size(ftepp->conditions); ++i)
367 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
370 static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
372 return (ppmacro*)util_htget(ftepp->macros, name);
375 static GMQCC_INLINE void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
377 util_htrm(ftepp->macros, name, NULL);
380 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
382 return (ftepp->token = lex_do(ftepp->lex));
385 /* Important: this does not skip newlines! */
386 static bool ftepp_skipspace(ftepp_t *ftepp)
388 if (ftepp->token != TOKEN_WHITE)
390 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
391 if (ftepp->token >= TOKEN_EOF) {
392 ftepp_error(ftepp, "unexpected end of preprocessor directive");
398 /* this one skips EOLs as well */
399 static bool ftepp_skipallwhite(ftepp_t *ftepp)
401 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
405 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
406 if (ftepp->token >= TOKEN_EOF) {
407 ftepp_error(ftepp, "unexpected end of preprocessor directive");
414 * The huge macro parsing code...
416 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
420 if (!ftepp_skipspace(ftepp))
422 if (ftepp->token == ')')
424 switch (ftepp->token) {
428 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
431 macro->variadic = true;
434 ftepp_error(ftepp, "unexpected token in parameter list");
438 if (!ftepp_skipspace(ftepp))
440 if (macro->variadic && ftepp->token != ')') {
441 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
444 } while (ftepp->token == ',');
446 if (ftepp->token != ')') {
447 ftepp_error(ftepp, "expected closing paren after macro parameter list");
451 /* skipspace happens in ftepp_define */
455 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
458 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
459 bool subscript = false;
461 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
462 subscript = !!(ftepp_next(ftepp) == '#');
464 if (subscript && ftepp_next(ftepp) != '#') {
465 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
467 } else if (subscript) {
468 if (ftepp_next(ftepp) == '[') {
469 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
470 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
474 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
476 if (ftepp_next(ftepp) != ']') {
477 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
482 * mark it as an array to be handled later as such and not
483 * as traditional __VA_ARGS__
485 ftepp->token = TOKEN_VA_ARGS_ARRAY;
486 ptok = pptoken_make(ftepp);
487 ptok->constval.i = index;
488 vec_push(macro->output, ptok);
491 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
495 int old = ftepp->token;
496 ftepp->token = TOKEN_VA_ARGS;
497 ptok = pptoken_make(ftepp);
498 vec_push(macro->output, ptok);
502 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
503 ftepp->token = TOKEN_VA_COUNT;
504 ptok = pptoken_make(ftepp);
505 vec_push(macro->output, ptok);
508 ptok = pptoken_make(ftepp);
509 vec_push(macro->output, ptok);
513 /* recursive expansion can cause EOFs here */
514 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
515 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
521 static bool ftepp_define(ftepp_t *ftepp)
523 ppmacro *macro = NULL;
524 size_t l = ftepp_ctx(ftepp).line;
526 (void)ftepp_next(ftepp);
527 if (!ftepp_skipspace(ftepp))
530 switch (ftepp->token) {
534 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
535 if (macro && ftepp->output_on) {
536 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
538 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
540 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
543 ftepp_error(ftepp, "expected macro name");
547 (void)ftepp_next(ftepp);
549 if (ftepp->token == '(') {
550 macro->has_params = true;
551 if (!ftepp_define_params(ftepp, macro)) {
552 ppmacro_delete(macro);
557 if (!ftepp_skipspace(ftepp)) {
558 ppmacro_delete(macro);
562 if (!ftepp_define_body(ftepp, macro)) {
563 ppmacro_delete(macro);
568 if (ftepp->output_on)
569 vec_push(ftepp->macros, macro);
571 if (ftepp->output_on)
572 util_htset(ftepp->macros, macro->name, (void*)macro);
574 ppmacro_delete(macro);
577 for (; l < ftepp_ctx(ftepp).line; ++l)
578 ftepp_out(ftepp, "\n", true);
583 * When a macro is used we have to handle parameters as well
584 * as special-concatenation via ## or stringification via #
586 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
587 * this kind of parens. Curly braces or [] don't count towards the
594 static void macroparam_clean(macroparam *self)
597 for (i = 0; i < vec_size(self->tokens); ++i)
598 pptoken_delete(self->tokens[i]);
599 vec_free(self->tokens);
602 /* need to leave the last token up */
603 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
605 macroparam *params = NULL;
611 if (!ftepp_skipallwhite(ftepp))
613 while (ftepp->token != ')') {
615 if (!ftepp_skipallwhite(ftepp))
617 while (parens || ftepp->token != ',') {
618 if (ftepp->token == '(')
620 else if (ftepp->token == ')') {
625 ptok = pptoken_make(ftepp);
626 vec_push(mp.tokens, ptok);
627 if (ftepp_next(ftepp) >= TOKEN_EOF) {
628 ftepp_error(ftepp, "unexpected EOF in macro call");
632 vec_push(params, mp);
634 if (ftepp->token == ')')
636 if (ftepp->token != ',') {
637 ftepp_error(ftepp, "expected closing paren or comma in macro call");
640 if (ftepp_next(ftepp) >= TOKEN_EOF) {
641 ftepp_error(ftepp, "unexpected EOF in macro call");
645 /* need to leave that up
646 if (ftepp_next(ftepp) >= TOKEN_EOF) {
647 ftepp_error(ftepp, "unexpected EOF in macro call");
651 *out_params = params;
656 macroparam_clean(&mp);
657 for (i = 0; i < vec_size(params); ++i)
658 macroparam_clean(¶ms[i]);
663 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
666 for (i = 0; i < vec_size(macro->params); ++i) {
667 if (!strcmp(macro->params[i], name)) {
675 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
680 switch (token->token) {
681 case TOKEN_STRINGCONST:
684 /* in preprocessor mode strings already are string,
685 * so we don't get actual newline bytes here.
686 * Still need to escape backslashes and quotes.
689 case '\\': ftepp_out(ftepp, "\\\\", false); break;
690 case '"': ftepp_out(ftepp, "\\\"", false); break;
693 ftepp_out(ftepp, chs, false);
700 ftepp_out(ftepp, " ", false);
703 ftepp_out(ftepp, "\\n", false);
706 ftepp_out(ftepp, token->value, false);
711 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
714 ftepp_out(ftepp, "\"", false);
715 for (i = 0; i < vec_size(param->tokens); ++i)
716 ftepp_stringify_token(ftepp, param->tokens[i]);
717 ftepp_out(ftepp, "\"", false);
720 static void ftepp_recursion_header(ftepp_t *ftepp)
722 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
725 static void ftepp_recursion_footer(ftepp_t *ftepp)
727 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
730 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
734 for (i = 0; i < vec_size(param->tokens); ++i) {
735 out = param->tokens[i];
736 if (out->token == TOKEN_EOL)
737 ftepp_out(ftepp, "\n", false);
739 ftepp_out(ftepp, out->value, false);
743 static bool ftepp_preprocess(ftepp_t *ftepp);
744 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
747 char *old_string = ftepp->output_string;
749 lex_file *old_lexer = ftepp->lex;
750 size_t vararg_start = vec_size(macro->params);
762 if (vararg_start < vec_size(params))
763 varargs = vec_size(params) - vararg_start;
768 if (!vec_size(macro->output))
771 ftepp->output_string = NULL;
772 for (o = 0; o < vec_size(macro->output); ++o) {
773 pptoken *out = macro->output[o];
774 switch (out->token) {
776 if (!macro->variadic) {
777 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
778 vec_free(old_string);
785 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
786 for (++pi; pi < varargs; ++pi) {
787 ftepp_out(ftepp, ", ", false);
788 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
792 case TOKEN_VA_ARGS_ARRAY:
793 if ((size_t)out->constval.i >= varargs) {
794 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
795 vec_free(old_string);
799 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
803 util_asprintf(&buffer, "%d", varargs);
804 ftepp_out(ftepp, buffer, false);
811 if (!macro_params_find(macro, out->value, &pi)) {
812 ftepp_out(ftepp, out->value, false);
815 ftepp_param_out(ftepp, ¶ms[pi]);
818 if (o + 1 < vec_size(macro->output)) {
819 nextok = macro->output[o+1]->token;
821 /* raw concatenation */
825 if ( (nextok == TOKEN_IDENT ||
826 nextok == TOKEN_KEYWORD ||
827 nextok == TOKEN_TYPENAME) &&
828 macro_params_find(macro, macro->output[o+1]->value, &pi))
831 ftepp_stringify(ftepp, ¶ms[pi]);
835 ftepp_out(ftepp, "#", false);
838 ftepp_out(ftepp, "\n", false);
841 ftepp_out(ftepp, out->value, false);
845 vec_push(ftepp->output_string, 0);
846 /* Now run the preprocessor recursively on this string buffer */
848 printf("__________\n%s\n=========\n", ftepp->output_string);
850 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
852 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
857 inlex->line = ftepp->lex->line;
858 inlex->sline = ftepp->lex->sline;
861 old_inmacro = ftepp->in_macro;
862 ftepp->in_macro = true;
863 ftepp->output_string = NULL;
864 if (!ftepp_preprocess(ftepp)) {
865 ftepp->in_macro = old_inmacro;
866 vec_free(ftepp->lex->open_string);
867 vec_free(ftepp->output_string);
868 lex_close(ftepp->lex);
872 ftepp->in_macro = old_inmacro;
873 vec_free(ftepp->lex->open_string);
874 lex_close(ftepp->lex);
876 inner_string = ftepp->output_string;
877 ftepp->output_string = old_string;
879 has_newlines = (strchr(inner_string, '\n') != NULL);
881 if (has_newlines && !old_inmacro)
882 ftepp_recursion_header(ftepp);
884 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
885 vec_free(inner_string);
887 if (has_newlines && !old_inmacro)
888 ftepp_recursion_footer(ftepp);
890 if (resetline && !ftepp->in_macro) {
892 util_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
893 ftepp_out(ftepp, lineno, false);
896 old_string = ftepp->output_string;
898 ftepp->lex = old_lexer;
899 ftepp->output_string = old_string;
903 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
906 macroparam *params = NULL;
910 if (!macro->has_params) {
911 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
918 if (!ftepp_skipallwhite(ftepp))
921 if (ftepp->token != '(') {
922 ftepp_error(ftepp, "expected macro parameters in parenthesis");
927 paramline = ftepp->lex->sline;
928 if (!ftepp_macro_call_params(ftepp, ¶ms))
931 if ( vec_size(params) < vec_size(macro->params) ||
932 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
934 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
935 (macro->variadic ? " at least" : ""),
936 (unsigned int)vec_size(macro->params),
937 (unsigned int)vec_size(params));
942 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
947 for (o = 0; o < vec_size(params); ++o)
948 macroparam_clean(¶ms[o]);
954 * #if - the FTEQCC way:
955 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
956 * <numbers> => True if the number is not 0
957 * !<factor> => True if the factor yields false
958 * !!<factor> => ERROR on 2 or more unary nots
959 * <macro> => becomes the macro's FIRST token regardless of parameters
960 * <e> && <e> => True if both expressions are true
961 * <e> || <e> => True if either expression is true
963 * <ident> => False (remember for macros the <macro> rule applies instead)
964 * Unary + and - are weird and wrong in fteqcc so we don't allow them
965 * parenthesis in expressions are allowed
966 * parameter lists on macros are errors
967 * No mathematical calculations are executed
969 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
970 static bool ftepp_if_op(ftepp_t *ftepp)
972 ftepp->lex->flags.noops = false;
974 if (!ftepp_skipspace(ftepp))
976 ftepp->lex->flags.noops = true;
979 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
985 if (!ftepp_skipspace(ftepp))
988 while (ftepp->token == '!') {
991 if (!ftepp_skipspace(ftepp))
995 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
999 if (!ftepp_skipspace(ftepp))
1003 switch (ftepp->token) {
1005 case TOKEN_TYPENAME:
1007 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
1009 if (!ftepp_skipspace(ftepp))
1011 if (ftepp->token != '(') {
1012 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
1016 if (!ftepp_skipspace(ftepp))
1018 if (ftepp->token != TOKEN_IDENT &&
1019 ftepp->token != TOKEN_TYPENAME &&
1020 ftepp->token != TOKEN_KEYWORD)
1022 ftepp_error(ftepp, "defined() used on an unexpected token type");
1025 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1028 if (!ftepp_skipspace(ftepp))
1030 if (ftepp->token != ')') {
1031 ftepp_error(ftepp, "expected closing paren");
1037 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1038 if (!macro || !vec_size(macro->output)) {
1042 /* This does not expand recursively! */
1043 switch (macro->output[0]->token) {
1044 case TOKEN_INTCONST:
1045 *value_out = macro->output[0]->constval.i;
1046 *out = !!(macro->output[0]->constval.i);
1048 case TOKEN_FLOATCONST:
1049 *value_out = macro->output[0]->constval.f;
1050 *out = !!(macro->output[0]->constval.f);
1058 case TOKEN_STRINGCONST:
1062 case TOKEN_INTCONST:
1063 *value_out = ftepp->lex->tok.constval.i;
1064 *out = !!(ftepp->lex->tok.constval.i);
1066 case TOKEN_FLOATCONST:
1067 *value_out = ftepp->lex->tok.constval.f;
1068 *out = !!(ftepp->lex->tok.constval.f);
1073 if (!ftepp_if_expr(ftepp, out, value_out))
1075 if (ftepp->token != ')') {
1076 ftepp_error(ftepp, "expected closing paren in #if expression");
1082 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1083 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1084 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1088 *value_out = -*value_out;
1091 *value_out = (*out ? 1 : 0);
1097 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1099 if (!ftepp_next(ftepp))
1101 return ftepp_if_value(ftepp, out, value_out);
1105 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1107 if (!ftepp_if_value(ftepp, out, value_out))
1110 if (!ftepp_if_op(ftepp))
1113 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1116 /* FTEQCC is all right-associative and no precedence here */
1117 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1118 !strcmp(ftepp_tokval(ftepp), "||"))
1121 char opc = ftepp_tokval(ftepp)[0];
1125 if (!ftepp_next(ftepp))
1127 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1131 *out = *out && next;
1133 *out = *out || next;
1135 *value_out = (*out ? 1 : 0);
1138 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1139 !strcmp(ftepp_tokval(ftepp), "!=") ||
1140 !strcmp(ftepp_tokval(ftepp), ">=") ||
1141 !strcmp(ftepp_tokval(ftepp), "<=") ||
1142 !strcmp(ftepp_tokval(ftepp), ">") ||
1143 !strcmp(ftepp_tokval(ftepp), "<"))
1146 const char opc0 = ftepp_tokval(ftepp)[0];
1147 const char opc1 = ftepp_tokval(ftepp)[1];
1150 if (!ftepp_next(ftepp))
1152 if (!ftepp_if_expr(ftepp, &next, &other))
1156 *out = (*value_out == other);
1157 else if (opc0 == '!')
1158 *out = (*value_out != other);
1159 else if (opc0 == '>') {
1160 if (opc1 == '=') *out = (*value_out >= other);
1161 else *out = (*value_out > other);
1163 else if (opc0 == '<') {
1164 if (opc1 == '=') *out = (*value_out <= other);
1165 else *out = (*value_out < other);
1167 *value_out = (*out ? 1 : 0);
1172 ftepp_error(ftepp, "junk after #if");
1177 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1179 bool result = false;
1182 memset(cond, 0, sizeof(*cond));
1183 (void)ftepp_next(ftepp);
1185 if (!ftepp_skipspace(ftepp))
1187 if (ftepp->token == TOKEN_EOL) {
1188 ftepp_error(ftepp, "expected expression for #if-directive");
1192 if (!ftepp_if_expr(ftepp, &result, &dummy))
1200 * ifdef is rather simple
1202 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1205 memset(cond, 0, sizeof(*cond));
1206 (void)ftepp_next(ftepp);
1207 if (!ftepp_skipspace(ftepp))
1210 switch (ftepp->token) {
1212 case TOKEN_TYPENAME:
1214 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1217 ftepp_error(ftepp, "expected macro name");
1221 (void)ftepp_next(ftepp);
1222 if (!ftepp_skipspace(ftepp))
1224 /* relaxing this condition
1225 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1226 ftepp_error(ftepp, "stray tokens after #ifdef");
1235 * undef is also simple
1237 static bool ftepp_undef(ftepp_t *ftepp)
1239 (void)ftepp_next(ftepp);
1240 if (!ftepp_skipspace(ftepp))
1243 if (ftepp->output_on) {
1244 switch (ftepp->token) {
1246 case TOKEN_TYPENAME:
1248 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1251 ftepp_error(ftepp, "expected macro name");
1256 (void)ftepp_next(ftepp);
1257 if (!ftepp_skipspace(ftepp))
1259 /* relaxing this condition
1260 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1261 ftepp_error(ftepp, "stray tokens after #ifdef");
1268 /* Special unescape-string function which skips a leading quote
1269 * and stops at a quote, not just at \0
1271 static void unescape(const char *str, char *out) {
1273 while (*str && *str != '"') {
1277 case '\\': *out++ = *str; break;
1278 case '"': *out++ = *str; break;
1279 case 'a': *out++ = '\a'; break;
1280 case 'b': *out++ = '\b'; break;
1281 case 'r': *out++ = '\r'; break;
1282 case 'n': *out++ = '\n'; break;
1283 case 't': *out++ = '\t'; break;
1284 case 'f': *out++ = '\f'; break;
1285 case 'v': *out++ = '\v'; break;
1300 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1303 char *filename = NULL;
1304 const char *last_slash;
1310 last_slash = strrchr(pathfile, '/');
1313 len = last_slash - pathfile;
1314 memcpy(vec_add(filename, len), pathfile, len);
1315 vec_push(filename, '/');
1319 memcpy(vec_add(filename, len+1), file, len);
1320 vec_last(filename) = 0;
1322 fp = fs_file_open(filename, "rb");
1331 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1333 char *filename = NULL;
1335 filename = ftepp_include_find_path(file, ftepp->includename);
1337 filename = ftepp_include_find_path(file, ftepp->itemname);
1341 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1342 char *message = NULL;
1344 if (!ftepp_skipspace(ftepp))
1347 /* handle the odd non string constant case so it works like C */
1348 if (ftepp->token != TOKEN_STRINGCONST) {
1350 vec_upload(message, "#warning", 8);
1352 while (ftepp->token != TOKEN_EOL) {
1353 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1356 vec_push(message, '\0');
1357 if (ftepp->output_on)
1358 store = ftepp_warn(ftepp, WARN_CPP, message);
1365 if (!ftepp->output_on)
1368 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1369 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1372 static void ftepp_directive_error(ftepp_t *ftepp) {
1373 char *message = NULL;
1375 if (!ftepp_skipspace(ftepp))
1378 /* handle the odd non string constant case so it works like C */
1379 if (ftepp->token != TOKEN_STRINGCONST) {
1380 vec_upload(message, "#error", 6);
1382 while (ftepp->token != TOKEN_EOL) {
1383 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1386 vec_push(message, '\0');
1387 if (ftepp->output_on)
1388 ftepp_error(ftepp, message);
1393 if (!ftepp->output_on)
1396 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1397 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1400 static void ftepp_directive_message(ftepp_t *ftepp) {
1401 char *message = NULL;
1403 if (!ftepp_skipspace(ftepp))
1406 /* handle the odd non string constant case so it works like C */
1407 if (ftepp->token != TOKEN_STRINGCONST) {
1408 vec_upload(message, "#message", 8);
1410 while (ftepp->token != TOKEN_EOL) {
1411 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1414 vec_push(message, '\0');
1415 if (ftepp->output_on)
1416 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1421 if (!ftepp->output_on)
1424 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1425 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1430 * FIXME: do we need/want a -I option?
1431 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1433 static bool ftepp_include(ftepp_t *ftepp)
1435 lex_file *old_lexer = ftepp->lex;
1440 char *old_includename;
1442 (void)ftepp_next(ftepp);
1443 if (!ftepp_skipspace(ftepp))
1446 if (ftepp->token != TOKEN_STRINGCONST) {
1447 ftepp_error(ftepp, "expected filename to include");
1451 if (!ftepp->output_on) {
1456 ctx = ftepp_ctx(ftepp);
1458 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1460 ftepp_out(ftepp, "\n#pragma file(", false);
1461 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1462 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1464 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1466 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1469 inlex = lex_open(filename);
1471 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1476 old_includename = ftepp->includename;
1477 ftepp->includename = filename;
1478 if (!ftepp_preprocess(ftepp)) {
1479 vec_free(ftepp->includename);
1480 ftepp->includename = old_includename;
1481 lex_close(ftepp->lex);
1482 ftepp->lex = old_lexer;
1485 vec_free(ftepp->includename);
1486 ftepp->includename = old_includename;
1487 lex_close(ftepp->lex);
1488 ftepp->lex = old_lexer;
1490 ftepp_out(ftepp, "\n#pragma file(", false);
1491 ftepp_out(ftepp, ctx.file, false);
1492 util_snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1493 ftepp_out(ftepp, lineno, false);
1496 (void)ftepp_next(ftepp);
1497 if (!ftepp_skipspace(ftepp))
1499 if (ftepp->token != TOKEN_EOL) {
1500 ftepp_error(ftepp, "stray tokens after #include");
1503 (void)ftepp_next(ftepp);
1508 /* Basic structure handlers */
1509 static bool ftepp_else_allowed(ftepp_t *ftepp)
1511 if (!vec_size(ftepp->conditions)) {
1512 ftepp_error(ftepp, "#else without #if");
1515 if (vec_last(ftepp->conditions).had_else) {
1516 ftepp_error(ftepp, "multiple #else for a single #if");
1522 static bool ftepp_hash(ftepp_t *ftepp)
1527 lex_ctx ctx = ftepp_ctx(ftepp);
1529 if (!ftepp_skipspace(ftepp))
1532 switch (ftepp->token) {
1535 case TOKEN_TYPENAME:
1536 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1537 return ftepp_define(ftepp);
1539 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1540 return ftepp_undef(ftepp);
1542 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1543 if (!ftepp_ifdef(ftepp, &cond))
1545 cond.was_on = cond.on;
1546 vec_push(ftepp->conditions, cond);
1547 ftepp->output_on = ftepp->output_on && cond.on;
1550 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1551 if (!ftepp_ifdef(ftepp, &cond))
1554 cond.was_on = cond.on;
1555 vec_push(ftepp->conditions, cond);
1556 ftepp->output_on = ftepp->output_on && cond.on;
1559 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1560 if (!ftepp_else_allowed(ftepp))
1562 if (!ftepp_ifdef(ftepp, &cond))
1564 pc = &vec_last(ftepp->conditions);
1565 pc->on = !pc->was_on && cond.on;
1566 pc->was_on = pc->was_on || pc->on;
1567 ftepp_update_output_condition(ftepp);
1570 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1571 if (!ftepp_else_allowed(ftepp))
1573 if (!ftepp_ifdef(ftepp, &cond))
1576 pc = &vec_last(ftepp->conditions);
1577 pc->on = !pc->was_on && cond.on;
1578 pc->was_on = pc->was_on || pc->on;
1579 ftepp_update_output_condition(ftepp);
1582 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1583 if (!ftepp_else_allowed(ftepp))
1585 if (!ftepp_if(ftepp, &cond))
1587 pc = &vec_last(ftepp->conditions);
1588 pc->on = !pc->was_on && cond.on;
1589 pc->was_on = pc->was_on || pc->on;
1590 ftepp_update_output_condition(ftepp);
1593 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1594 if (!ftepp_if(ftepp, &cond))
1596 cond.was_on = cond.on;
1597 vec_push(ftepp->conditions, cond);
1598 ftepp->output_on = ftepp->output_on && cond.on;
1601 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1602 if (!ftepp_else_allowed(ftepp))
1604 pc = &vec_last(ftepp->conditions);
1605 pc->on = !pc->was_on;
1606 pc->had_else = true;
1608 ftepp_update_output_condition(ftepp);
1611 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1612 if (!vec_size(ftepp->conditions)) {
1613 ftepp_error(ftepp, "#endif without #if");
1616 vec_pop(ftepp->conditions);
1618 ftepp_update_output_condition(ftepp);
1621 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1622 return ftepp_include(ftepp);
1624 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1625 ftepp_out(ftepp, "#", false);
1628 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1629 ftepp_directive_warning(ftepp);
1632 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1633 ftepp_directive_error(ftepp);
1636 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1637 ftepp_directive_message(ftepp);
1641 if (ftepp->output_on) {
1642 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1649 /* break; never reached */
1651 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1654 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1657 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1660 /* Builtins! Don't forget the builtins! */
1661 case TOKEN_INTCONST:
1662 case TOKEN_FLOATCONST:
1663 ftepp_out(ftepp, "#", false);
1666 if (!ftepp_skipspace(ftepp))
1671 static bool ftepp_preprocess(ftepp_t *ftepp)
1674 bool newline = true;
1677 char *expand = NULL;
1679 ftepp->lex->flags.preprocessing = true;
1680 ftepp->lex->flags.mergelines = false;
1681 ftepp->lex->flags.noops = true;
1686 if (ftepp->token >= TOKEN_EOF)
1692 switch (ftepp->token) {
1695 case TOKEN_TYPENAME:
1696 /* is it a predef? */
1697 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1698 char *(*predef)(lex_file*) = ftepp_predef(ftepp_tokval(ftepp));
1700 expand = predef(ftepp->lex);
1701 ftepp_out (ftepp, expand, false);
1709 if (ftepp->output_on)
1710 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1715 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1719 if (!ftepp_macro_call(ftepp, macro))
1720 ftepp->token = TOKEN_ERROR;
1724 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1728 ftepp->lex->flags.mergelines = true;
1729 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1730 ftepp_error(ftepp, "error in preprocessor directive");
1731 ftepp->token = TOKEN_ERROR;
1734 if (!ftepp_hash(ftepp))
1735 ftepp->token = TOKEN_ERROR;
1736 ftepp->lex->flags.mergelines = false;
1740 ftepp_out(ftepp, "\n", true);
1744 /* same as default but don't set newline=false */
1745 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1750 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1754 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1756 /* force a 0 at the end but don't count it as added to the output */
1757 vec_push(ftepp->output_string, 0);
1758 vec_shrinkby(ftepp->output_string, 1);
1760 return (ftepp->token == TOKEN_EOF);
1763 /* Like in parser.c - files keep the previous state so we have one global
1764 * preprocessor. Except here we will want to warn about dangling #ifs.
1766 static bool ftepp_preprocess_done(ftepp_t *ftepp)
1769 if (vec_size(ftepp->conditions)) {
1770 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1773 lex_close(ftepp->lex);
1775 if (ftepp->itemname) {
1776 mem_d(ftepp->itemname);
1777 ftepp->itemname = NULL;
1782 bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
1784 ftepp->lex = lex_open(filename);
1785 ftepp->itemname = util_strdup(filename);
1787 con_out("failed to open file \"%s\"\n", filename);
1790 if (!ftepp_preprocess(ftepp))
1792 return ftepp_preprocess_done(ftepp);
1795 bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
1797 ftepp->lex = lex_open_string(str, strlen(str), name);
1798 ftepp->itemname = util_strdup(name);
1800 con_out("failed to create lexer for string \"%s\"\n", name);
1803 if (!ftepp_preprocess(ftepp))
1805 return ftepp_preprocess_done(ftepp);
1809 void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
1810 char *create = NULL;
1812 /* use saner path for empty macros */
1814 ftepp_add_define(ftepp, "__builtin__", name);
1818 vec_upload(create, "#define ", 8);
1819 vec_upload(create, name, strlen(name));
1820 vec_push (create, ' ');
1821 vec_upload(create, value, strlen(value));
1822 vec_push (create, 0);
1824 ftepp_preprocess_string(ftepp, "__builtin__", create);
1828 ftepp_t *ftepp_create()
1834 ftepp = ftepp_new();
1838 memset(minor, 0, sizeof(minor));
1839 memset(major, 0, sizeof(major));
1841 /* set the right macro based on the selected standard */
1842 ftepp_add_define(ftepp, NULL, "GMQCC");
1843 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1844 ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
1853 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1854 ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
1855 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1856 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1857 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1858 ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
1859 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1860 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1861 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1862 ftepp_add_define(ftepp, NULL, "__STD_QCC__");
1873 ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
1874 ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
1877 * We're going to just make __NULL__ nil, which works for 60% of the
1878 * cases of __NULL_ for fteqcc.
1880 ftepp_add_macro(ftepp, "__NULL__", "nil");
1885 void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
1888 lex_ctx ctx = { "__builtin__", 0 };
1890 macro = ppmacro_new(ctx, name);
1891 /*vec_push(ftepp->macros, macro);*/
1892 util_htset(ftepp->macros, name, macro);
1895 const char *ftepp_get(ftepp_t *ftepp)
1897 return ftepp->output_string;
1900 void ftepp_flush(ftepp_t *ftepp)
1902 ftepp_flush_do(ftepp);
1905 void ftepp_finish(ftepp_t *ftepp)
1909 ftepp_delete(ftepp);