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
31 #define HT_MACROS 1024
41 /* a copy from the lexer */
55 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
62 typedef struct ftepp_s {
68 ppcondition *conditions;
70 ht macros; /* hashtable<string, ppmacro*> */
79 * Implement the predef subsystem now. We can do this safely with the
80 * help of lexer contexts.
82 static uint32_t ftepp_predef_countval = 0;
83 static uint32_t ftepp_predef_randval = 0;
86 static char *ftepp_predef_date(lex_file *context) {
87 const struct tm *itime = NULL;
88 char *value = (char*)mem_a(82);
94 itime = platform_localtime(&rtime);
95 strftime(value, 82, "\"%b %d %Y\"", itime);
101 static char *ftepp_predef_time(lex_file *context) {
102 const struct tm *itime = NULL;
103 char *value = (char*)mem_a(82);
109 itime = platform_localtime(&rtime);
110 strftime(value, 82, "\"%X\"", itime);
116 static char *ftepp_predef_line(lex_file *context) {
118 util_asprintf(&value, "%d", (int)context->line);
122 static char *ftepp_predef_file(lex_file *context) {
123 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
124 char *value = (char*)mem_a(length);
125 platform_snprintf(value, length, "\"%s\"", context->name);
129 /* __COUNTER_LAST__ */
130 static char *ftepp_predef_counterlast(lex_file *context) {
132 util_asprintf(&value, "%u", ftepp_predef_countval);
138 static char *ftepp_predef_counter(lex_file *context) {
140 ftepp_predef_countval ++;
141 util_asprintf(&value, "%u", ftepp_predef_countval);
147 static char *ftepp_predef_random(lex_file *context) {
149 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
150 util_asprintf(&value, "%u", ftepp_predef_randval);
155 /* __RANDOM_LAST__ */
156 static char *ftepp_predef_randomlast(lex_file *context) {
158 util_asprintf(&value, "%u", ftepp_predef_randval);
164 static char *ftepp_predef_timestamp(lex_file *context) {
170 if (stat(context->name, &finfo))
171 return util_strdup("\"<failed to determine timestamp>\"");
173 find = platform_ctime(&finfo.st_mtime);
174 value = (char*)mem_a(strlen(find) + 1);
175 memcpy(&value[1], find, (size = strlen(find)) - 1);
185 char *(*func)(lex_file *);
188 static const ftepp_predef_t ftepp_predefs[] = {
189 { "__LINE__", &ftepp_predef_line },
190 { "__FILE__", &ftepp_predef_file },
191 { "__COUNTER__", &ftepp_predef_counter },
192 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
193 { "__RANDOM__", &ftepp_predef_random },
194 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
195 { "__DATE__", &ftepp_predef_date },
196 { "__TIME__", &ftepp_predef_time },
197 { "__TIME_STAMP__", &ftepp_predef_timestamp }
200 static GMQCC_INLINE size_t ftepp_predef_index(const char *name) {
201 /* no hashtable here, we simply check for one to exist the naive way */
203 for(i = 1; i < GMQCC_ARRAY_COUNT(ftepp_predefs) + 1; i++)
204 if (!strcmp(ftepp_predefs[i-1].name, name))
209 bool ftepp_predef_exists(const char *name);
210 bool ftepp_predef_exists(const char *name) {
211 return ftepp_predef_index(name) != 0;
214 /* singleton because we're allowed */
215 static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) {
216 size_t i = ftepp_predef_index(name);
217 return (i != 0) ? ftepp_predefs[i-1].func : NULL;
220 #define ftepp_tokval(f) ((f)->lex->tok.value)
221 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
223 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx_t ctx, const char *fmt, ...)
230 con_cvprintmsg(ctx, LVL_ERROR, "error", fmt, ap);
234 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
241 con_cvprintmsg(ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
245 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
251 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
256 static pptoken *pptoken_make(ftepp_t *ftepp)
258 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
259 token->token = ftepp->token;
261 if (token->token == TOKEN_WHITE)
262 token->value = util_strdup(" ");
265 token->value = util_strdup(ftepp_tokval(ftepp));
267 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
271 static GMQCC_INLINE void pptoken_delete(pptoken *self)
277 static ppmacro *ppmacro_new(lex_ctx_t ctx, const char *name)
279 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
282 memset(macro, 0, sizeof(*macro));
283 macro->name = util_strdup(name);
287 static void ppmacro_delete(ppmacro *self)
290 for (i = 0; i < vec_size(self->params); ++i)
291 mem_d(self->params[i]);
292 vec_free(self->params);
293 for (i = 0; i < vec_size(self->output); ++i)
294 pptoken_delete(self->output[i]);
295 vec_free(self->output);
300 static ftepp_t* ftepp_new(void)
304 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
305 memset(ftepp, 0, sizeof(*ftepp));
307 ftepp->macros = util_htnew(HT_MACROS);
308 ftepp->output_on = true;
313 static GMQCC_INLINE void ftepp_flush_do(ftepp_t *self)
315 vec_free(self->output_string);
318 static void ftepp_delete(ftepp_t *self)
320 ftepp_flush_do(self);
322 mem_d(self->itemname);
323 if (self->includename)
324 vec_free(self->includename);
326 util_htrem(self->macros, (void (*)(void*))&ppmacro_delete);
328 vec_free(self->conditions);
330 lex_close(self->lex);
334 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
336 if (ignore_cond || ftepp->output_on)
341 data = vec_add(ftepp->output_string, len);
342 memcpy(data, str, len);
346 static GMQCC_INLINE void ftepp_update_output_condition(ftepp_t *ftepp)
349 ftepp->output_on = true;
350 for (i = 0; i < vec_size(ftepp->conditions); ++i)
351 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
354 static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
356 return (ppmacro*)util_htget(ftepp->macros, name);
359 static GMQCC_INLINE void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
361 util_htrm(ftepp->macros, name, (void (*)(void*))&ppmacro_delete);
364 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
366 return (ftepp->token = lex_do(ftepp->lex));
369 /* Important: this does not skip newlines! */
370 static bool ftepp_skipspace(ftepp_t *ftepp)
372 if (ftepp->token != TOKEN_WHITE)
374 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
375 if (ftepp->token >= TOKEN_EOF) {
376 ftepp_error(ftepp, "unexpected end of preprocessor directive");
382 /* this one skips EOLs as well */
383 static bool ftepp_skipallwhite(ftepp_t *ftepp)
385 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
389 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
390 if (ftepp->token >= TOKEN_EOF) {
391 ftepp_error(ftepp, "unexpected end of preprocessor directive");
398 * The huge macro parsing code...
400 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
404 if (!ftepp_skipspace(ftepp))
406 if (ftepp->token == ')')
408 switch (ftepp->token) {
412 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
415 macro->variadic = true;
418 ftepp_error(ftepp, "unexpected token in parameter list");
422 if (!ftepp_skipspace(ftepp))
424 if (macro->variadic && ftepp->token != ')') {
425 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
428 } while (ftepp->token == ',');
430 if (ftepp->token != ')') {
431 ftepp_error(ftepp, "expected closing paren after macro parameter list");
435 /* skipspace happens in ftepp_define */
439 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
442 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
443 bool subscript = false;
445 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
446 subscript = !!(ftepp_next(ftepp) == '#');
448 if (subscript && ftepp_next(ftepp) != '#') {
449 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
451 } else if (subscript) {
452 if (ftepp_next(ftepp) == '[') {
453 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
454 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
458 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
460 if (ftepp_next(ftepp) != ']') {
461 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
466 * mark it as an array to be handled later as such and not
467 * as traditional __VA_ARGS__
469 ftepp->token = TOKEN_VA_ARGS_ARRAY;
470 ptok = pptoken_make(ftepp);
471 ptok->constval.i = index;
472 vec_push(macro->output, ptok);
475 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
479 int old = ftepp->token;
480 ftepp->token = TOKEN_VA_ARGS;
481 ptok = pptoken_make(ftepp);
482 vec_push(macro->output, ptok);
486 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
487 ftepp->token = TOKEN_VA_COUNT;
488 ptok = pptoken_make(ftepp);
489 vec_push(macro->output, ptok);
492 ptok = pptoken_make(ftepp);
493 vec_push(macro->output, ptok);
497 /* recursive expansion can cause EOFs here */
498 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
499 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
505 static bool ftepp_define(ftepp_t *ftepp)
507 ppmacro *macro = NULL;
508 size_t l = ftepp_ctx(ftepp).line;
510 (void)ftepp_next(ftepp);
511 if (!ftepp_skipspace(ftepp))
514 switch (ftepp->token) {
518 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
519 if (macro && ftepp->output_on) {
520 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
522 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
524 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
527 ftepp_error(ftepp, "expected macro name");
531 (void)ftepp_next(ftepp);
533 if (ftepp->token == '(') {
534 macro->has_params = true;
535 if (!ftepp_define_params(ftepp, macro)) {
536 ppmacro_delete(macro);
541 if (!ftepp_skipspace(ftepp)) {
542 ppmacro_delete(macro);
546 if (!ftepp_define_body(ftepp, macro)) {
547 ppmacro_delete(macro);
551 if (ftepp->output_on)
552 util_htset(ftepp->macros, macro->name, (void*)macro);
554 ppmacro_delete(macro);
557 for (; l < ftepp_ctx(ftepp).line; ++l)
558 ftepp_out(ftepp, "\n", true);
563 * When a macro is used we have to handle parameters as well
564 * as special-concatenation via ## or stringification via #
566 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
567 * this kind of parens. Curly braces or [] don't count towards the
574 static void macroparam_clean(macroparam *self)
577 for (i = 0; i < vec_size(self->tokens); ++i)
578 pptoken_delete(self->tokens[i]);
579 vec_free(self->tokens);
582 /* need to leave the last token up */
583 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
585 macroparam *params = NULL;
591 if (!ftepp_skipallwhite(ftepp))
593 while (ftepp->token != ')') {
595 if (!ftepp_skipallwhite(ftepp))
597 while (parens || ftepp->token != ',') {
598 if (ftepp->token == '(')
600 else if (ftepp->token == ')') {
605 ptok = pptoken_make(ftepp);
606 vec_push(mp.tokens, ptok);
607 if (ftepp_next(ftepp) >= TOKEN_EOF) {
608 ftepp_error(ftepp, "unexpected end of file in macro call");
612 vec_push(params, mp);
614 if (ftepp->token == ')')
616 if (ftepp->token != ',') {
617 ftepp_error(ftepp, "expected closing paren or comma in macro call");
620 if (ftepp_next(ftepp) >= TOKEN_EOF) {
621 ftepp_error(ftepp, "unexpected end of file in macro call");
625 *out_params = params;
630 macroparam_clean(&mp);
631 for (i = 0; i < vec_size(params); ++i)
632 macroparam_clean(¶ms[i]);
637 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
640 for (i = 0; i < vec_size(macro->params); ++i) {
641 if (!strcmp(macro->params[i], name)) {
649 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
654 switch (token->token) {
655 case TOKEN_STRINGCONST:
658 /* in preprocessor mode strings already are string,
659 * so we don't get actual newline bytes here.
660 * Still need to escape backslashes and quotes.
663 case '\\': ftepp_out(ftepp, "\\\\", false); break;
664 case '"': ftepp_out(ftepp, "\\\"", false); break;
667 ftepp_out(ftepp, chs, false);
674 ftepp_out(ftepp, " ", false);
677 ftepp_out(ftepp, "\\n", false);
680 ftepp_out(ftepp, token->value, false);
685 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
688 ftepp_out(ftepp, "\"", false);
689 for (i = 0; i < vec_size(param->tokens); ++i)
690 ftepp_stringify_token(ftepp, param->tokens[i]);
691 ftepp_out(ftepp, "\"", false);
694 static void ftepp_recursion_header(ftepp_t *ftepp)
696 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
699 static void ftepp_recursion_footer(ftepp_t *ftepp)
701 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
704 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
708 for (i = 0; i < vec_size(param->tokens); ++i) {
709 out = param->tokens[i];
710 if (out->token == TOKEN_EOL)
711 ftepp_out(ftepp, "\n", false);
713 ftepp_out(ftepp, out->value, false);
717 static bool ftepp_preprocess(ftepp_t *ftepp);
718 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
721 char *old_string = ftepp->output_string;
723 lex_file *old_lexer = ftepp->lex;
724 size_t vararg_start = vec_size(macro->params);
736 if (vararg_start < vec_size(params))
737 varargs = vec_size(params) - vararg_start;
742 if (!vec_size(macro->output))
745 ftepp->output_string = NULL;
746 for (o = 0; o < vec_size(macro->output); ++o) {
747 pptoken *out = macro->output[o];
748 switch (out->token) {
750 if (!macro->variadic) {
751 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
752 vec_free(old_string);
759 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
760 for (++pi; pi < varargs; ++pi) {
761 ftepp_out(ftepp, ", ", false);
762 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
766 case TOKEN_VA_ARGS_ARRAY:
767 if ((size_t)out->constval.i >= varargs) {
768 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
769 vec_free(old_string);
773 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
777 util_asprintf(&buffer, "%d", varargs);
778 ftepp_out(ftepp, buffer, false);
785 if (!macro_params_find(macro, out->value, &pi)) {
786 ftepp_out(ftepp, out->value, false);
789 ftepp_param_out(ftepp, ¶ms[pi]);
792 if (o + 1 < vec_size(macro->output)) {
793 nextok = macro->output[o+1]->token;
795 /* raw concatenation */
799 if ( (nextok == TOKEN_IDENT ||
800 nextok == TOKEN_KEYWORD ||
801 nextok == TOKEN_TYPENAME) &&
802 macro_params_find(macro, macro->output[o+1]->value, &pi))
805 ftepp_stringify(ftepp, ¶ms[pi]);
809 ftepp_out(ftepp, "#", false);
812 ftepp_out(ftepp, "\n", false);
815 ftepp_out(ftepp, out->value, false);
819 vec_push(ftepp->output_string, 0);
820 /* Now run the preprocessor recursively on this string buffer */
822 printf("__________\n%s\n=========\n", ftepp->output_string);
824 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
826 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
831 inlex->line = ftepp->lex->line;
832 inlex->sline = ftepp->lex->sline;
835 old_inmacro = ftepp->in_macro;
836 ftepp->in_macro = true;
837 ftepp->output_string = NULL;
838 if (!ftepp_preprocess(ftepp)) {
839 ftepp->in_macro = old_inmacro;
840 vec_free(ftepp->lex->open_string);
841 vec_free(ftepp->output_string);
842 lex_close(ftepp->lex);
846 ftepp->in_macro = old_inmacro;
847 vec_free(ftepp->lex->open_string);
848 lex_close(ftepp->lex);
850 inner_string = ftepp->output_string;
851 ftepp->output_string = old_string;
853 has_newlines = (strchr(inner_string, '\n') != NULL);
855 if (has_newlines && !old_inmacro)
856 ftepp_recursion_header(ftepp);
858 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
859 vec_free(inner_string);
861 if (has_newlines && !old_inmacro)
862 ftepp_recursion_footer(ftepp);
864 if (resetline && !ftepp->in_macro) {
866 platform_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
867 ftepp_out(ftepp, lineno, false);
870 old_string = ftepp->output_string;
872 ftepp->lex = old_lexer;
873 ftepp->output_string = old_string;
877 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
880 macroparam *params = NULL;
884 if (!macro->has_params) {
885 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
892 if (!ftepp_skipallwhite(ftepp))
895 if (ftepp->token != '(') {
896 ftepp_error(ftepp, "expected macro parameters in parenthesis");
901 paramline = ftepp->lex->sline;
902 if (!ftepp_macro_call_params(ftepp, ¶ms))
905 if ( vec_size(params) < vec_size(macro->params) ||
906 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
908 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
909 (macro->variadic ? " at least" : ""),
910 (unsigned int)vec_size(macro->params),
911 (unsigned int)vec_size(params));
916 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
921 for (o = 0; o < vec_size(params); ++o)
922 macroparam_clean(¶ms[o]);
928 * #if - the FTEQCC way:
929 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
930 * <numbers> => True if the number is not 0
931 * !<factor> => True if the factor yields false
932 * !!<factor> => ERROR on 2 or more unary nots
933 * <macro> => becomes the macro's FIRST token regardless of parameters
934 * <e> && <e> => True if both expressions are true
935 * <e> || <e> => True if either expression is true
937 * <ident> => False (remember for macros the <macro> rule applies instead)
938 * Unary + and - are weird and wrong in fteqcc so we don't allow them
939 * parenthesis in expressions are allowed
940 * parameter lists on macros are errors
941 * No mathematical calculations are executed
943 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
944 static bool ftepp_if_op(ftepp_t *ftepp)
946 ftepp->lex->flags.noops = false;
948 if (!ftepp_skipspace(ftepp))
950 ftepp->lex->flags.noops = true;
953 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
959 if (!ftepp_skipspace(ftepp))
962 while (ftepp->token == '!') {
965 if (!ftepp_skipspace(ftepp))
969 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
973 if (!ftepp_skipspace(ftepp))
977 switch (ftepp->token) {
981 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
983 if (!ftepp_skipspace(ftepp))
985 if (ftepp->token != '(') {
986 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
990 if (!ftepp_skipspace(ftepp))
992 if (ftepp->token != TOKEN_IDENT &&
993 ftepp->token != TOKEN_TYPENAME &&
994 ftepp->token != TOKEN_KEYWORD)
996 ftepp_error(ftepp, "defined() used on an unexpected token type");
999 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1002 if (!ftepp_skipspace(ftepp))
1004 if (ftepp->token != ')') {
1005 ftepp_error(ftepp, "expected closing paren");
1011 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1012 if (!macro || !vec_size(macro->output)) {
1016 /* This does not expand recursively! */
1017 switch (macro->output[0]->token) {
1018 case TOKEN_INTCONST:
1019 *value_out = macro->output[0]->constval.i;
1020 *out = !!(macro->output[0]->constval.i);
1022 case TOKEN_FLOATCONST:
1023 *value_out = macro->output[0]->constval.f;
1024 *out = !!(macro->output[0]->constval.f);
1032 case TOKEN_STRINGCONST:
1036 case TOKEN_INTCONST:
1037 *value_out = ftepp->lex->tok.constval.i;
1038 *out = !!(ftepp->lex->tok.constval.i);
1040 case TOKEN_FLOATCONST:
1041 *value_out = ftepp->lex->tok.constval.f;
1042 *out = !!(ftepp->lex->tok.constval.f);
1047 if (!ftepp_if_expr(ftepp, out, value_out))
1049 if (ftepp->token != ')') {
1050 ftepp_error(ftepp, "expected closing paren in #if expression");
1056 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1057 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1058 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1062 *value_out = -*value_out;
1065 *value_out = (*out ? 1 : 0);
1071 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1073 if (!ftepp_next(ftepp))
1075 return ftepp_if_value(ftepp, out, value_out);
1079 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1081 if (!ftepp_if_value(ftepp, out, value_out))
1084 if (!ftepp_if_op(ftepp))
1087 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1090 /* FTEQCC is all right-associative and no precedence here */
1091 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1092 !strcmp(ftepp_tokval(ftepp), "||"))
1095 char opc = ftepp_tokval(ftepp)[0];
1099 if (!ftepp_next(ftepp))
1101 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1105 *out = *out && next;
1107 *out = *out || next;
1109 *value_out = (*out ? 1 : 0);
1112 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1113 !strcmp(ftepp_tokval(ftepp), "!=") ||
1114 !strcmp(ftepp_tokval(ftepp), ">=") ||
1115 !strcmp(ftepp_tokval(ftepp), "<=") ||
1116 !strcmp(ftepp_tokval(ftepp), ">") ||
1117 !strcmp(ftepp_tokval(ftepp), "<"))
1120 const char opc0 = ftepp_tokval(ftepp)[0];
1121 const char opc1 = ftepp_tokval(ftepp)[1];
1124 if (!ftepp_next(ftepp))
1126 if (!ftepp_if_expr(ftepp, &next, &other))
1130 *out = (*value_out == other);
1131 else if (opc0 == '!')
1132 *out = (*value_out != other);
1133 else if (opc0 == '>') {
1134 if (opc1 == '=') *out = (*value_out >= other);
1135 else *out = (*value_out > other);
1137 else if (opc0 == '<') {
1138 if (opc1 == '=') *out = (*value_out <= other);
1139 else *out = (*value_out < other);
1141 *value_out = (*out ? 1 : 0);
1146 ftepp_error(ftepp, "junk after #if");
1151 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1153 bool result = false;
1156 memset(cond, 0, sizeof(*cond));
1157 (void)ftepp_next(ftepp);
1159 if (!ftepp_skipspace(ftepp))
1161 if (ftepp->token == TOKEN_EOL) {
1162 ftepp_error(ftepp, "expected expression for #if-directive");
1166 if (!ftepp_if_expr(ftepp, &result, &dummy))
1174 * ifdef is rather simple
1176 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1179 memset(cond, 0, sizeof(*cond));
1180 (void)ftepp_next(ftepp);
1181 if (!ftepp_skipspace(ftepp))
1184 switch (ftepp->token) {
1186 case TOKEN_TYPENAME:
1188 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1191 ftepp_error(ftepp, "expected macro name");
1195 (void)ftepp_next(ftepp);
1196 if (!ftepp_skipspace(ftepp))
1198 /* relaxing this condition
1199 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1200 ftepp_error(ftepp, "stray tokens after #ifdef");
1209 * undef is also simple
1211 static bool ftepp_undef(ftepp_t *ftepp)
1213 (void)ftepp_next(ftepp);
1214 if (!ftepp_skipspace(ftepp))
1217 if (ftepp->output_on) {
1218 switch (ftepp->token) {
1220 case TOKEN_TYPENAME:
1222 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1225 ftepp_error(ftepp, "expected macro name");
1230 (void)ftepp_next(ftepp);
1231 if (!ftepp_skipspace(ftepp))
1233 /* relaxing this condition
1234 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1235 ftepp_error(ftepp, "stray tokens after #ifdef");
1242 /* Special unescape-string function which skips a leading quote
1243 * and stops at a quote, not just at \0
1245 static void unescape(const char *str, char *out) {
1247 while (*str && *str != '"') {
1251 case '\\': *out++ = *str; break;
1252 case '"': *out++ = *str; break;
1253 case 'a': *out++ = '\a'; break;
1254 case 'b': *out++ = '\b'; break;
1255 case 'r': *out++ = '\r'; break;
1256 case 'n': *out++ = '\n'; break;
1257 case 't': *out++ = '\t'; break;
1258 case 'f': *out++ = '\f'; break;
1259 case 'v': *out++ = '\v'; break;
1274 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1277 char *filename = NULL;
1278 const char *last_slash;
1284 last_slash = strrchr(pathfile, '/');
1287 len = last_slash - pathfile;
1288 memcpy(vec_add(filename, len), pathfile, len);
1289 vec_push(filename, '/');
1293 memcpy(vec_add(filename, len+1), file, len);
1294 vec_last(filename) = 0;
1296 fp = fs_file_open(filename, "rb");
1305 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1307 char *filename = NULL;
1309 filename = ftepp_include_find_path(file, ftepp->includename);
1311 filename = ftepp_include_find_path(file, ftepp->itemname);
1315 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1316 char *message = NULL;
1318 if (!ftepp_skipspace(ftepp))
1321 /* handle the odd non string constant case so it works like C */
1322 if (ftepp->token != TOKEN_STRINGCONST) {
1324 vec_append(message, 8, "#warning");
1326 while (ftepp->token != TOKEN_EOL) {
1327 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1330 vec_push(message, '\0');
1331 if (ftepp->output_on)
1332 store = ftepp_warn(ftepp, WARN_CPP, message);
1339 if (!ftepp->output_on)
1342 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1343 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1346 static void ftepp_directive_error(ftepp_t *ftepp) {
1347 char *message = NULL;
1349 if (!ftepp_skipspace(ftepp))
1352 /* handle the odd non string constant case so it works like C */
1353 if (ftepp->token != TOKEN_STRINGCONST) {
1354 vec_append(message, 6, "#error");
1356 while (ftepp->token != TOKEN_EOL) {
1357 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1360 vec_push(message, '\0');
1361 if (ftepp->output_on)
1362 ftepp_error(ftepp, message);
1367 if (!ftepp->output_on)
1370 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1371 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1374 static void ftepp_directive_message(ftepp_t *ftepp) {
1375 char *message = NULL;
1377 if (!ftepp_skipspace(ftepp))
1380 /* handle the odd non string constant case so it works like C */
1381 if (ftepp->token != TOKEN_STRINGCONST) {
1382 vec_append(message, 8, "#message");
1384 while (ftepp->token != TOKEN_EOL) {
1385 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1388 vec_push(message, '\0');
1389 if (ftepp->output_on)
1390 con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1395 if (!ftepp->output_on)
1398 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1399 con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1404 * FIXME: do we need/want a -I option?
1405 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1407 static bool ftepp_include(ftepp_t *ftepp)
1409 lex_file *old_lexer = ftepp->lex;
1414 char *old_includename;
1416 (void)ftepp_next(ftepp);
1417 if (!ftepp_skipspace(ftepp))
1420 if (ftepp->token != TOKEN_STRINGCONST) {
1421 ftepp_error(ftepp, "expected filename to include");
1425 if (!ftepp->output_on) {
1430 ctx = ftepp_ctx(ftepp);
1432 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1434 ftepp_out(ftepp, "\n#pragma file(", false);
1435 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1436 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1438 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1440 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1443 inlex = lex_open(filename);
1445 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1450 old_includename = ftepp->includename;
1451 ftepp->includename = filename;
1452 if (!ftepp_preprocess(ftepp)) {
1453 vec_free(ftepp->includename);
1454 ftepp->includename = old_includename;
1455 lex_close(ftepp->lex);
1456 ftepp->lex = old_lexer;
1459 vec_free(ftepp->includename);
1460 ftepp->includename = old_includename;
1461 lex_close(ftepp->lex);
1462 ftepp->lex = old_lexer;
1464 ftepp_out(ftepp, "\n#pragma file(", false);
1465 ftepp_out(ftepp, ctx.file, false);
1466 platform_snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1467 ftepp_out(ftepp, lineno, false);
1470 (void)ftepp_next(ftepp);
1471 if (!ftepp_skipspace(ftepp))
1473 if (ftepp->token != TOKEN_EOL) {
1474 ftepp_error(ftepp, "stray tokens after #include");
1477 (void)ftepp_next(ftepp);
1482 /* Basic structure handlers */
1483 static bool ftepp_else_allowed(ftepp_t *ftepp)
1485 if (!vec_size(ftepp->conditions)) {
1486 ftepp_error(ftepp, "#else without #if");
1489 if (vec_last(ftepp->conditions).had_else) {
1490 ftepp_error(ftepp, "multiple #else for a single #if");
1496 static GMQCC_INLINE void ftepp_inmacro(ftepp_t *ftepp, const char *hash) {
1497 if (ftepp->in_macro)
1498 (void)!ftepp_warn(ftepp, WARN_DIRECTIVE_INMACRO, "`#%s` directive in macro", hash);
1501 static bool ftepp_hash(ftepp_t *ftepp)
1506 lex_ctx_t ctx = ftepp_ctx(ftepp);
1508 if (!ftepp_skipspace(ftepp))
1511 switch (ftepp->token) {
1514 case TOKEN_TYPENAME:
1515 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1516 ftepp_inmacro(ftepp, "define");
1517 return ftepp_define(ftepp);
1519 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1520 ftepp_inmacro(ftepp, "undef");
1521 return ftepp_undef(ftepp);
1523 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1524 ftepp_inmacro(ftepp, "ifdef");
1525 if (!ftepp_ifdef(ftepp, &cond))
1527 cond.was_on = cond.on;
1528 vec_push(ftepp->conditions, cond);
1529 ftepp->output_on = ftepp->output_on && cond.on;
1532 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1533 ftepp_inmacro(ftepp, "ifndef");
1534 if (!ftepp_ifdef(ftepp, &cond))
1537 cond.was_on = cond.on;
1538 vec_push(ftepp->conditions, cond);
1539 ftepp->output_on = ftepp->output_on && cond.on;
1542 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1543 ftepp_inmacro(ftepp, "elifdef");
1544 if (!ftepp_else_allowed(ftepp))
1546 if (!ftepp_ifdef(ftepp, &cond))
1548 pc = &vec_last(ftepp->conditions);
1549 pc->on = !pc->was_on && cond.on;
1550 pc->was_on = pc->was_on || pc->on;
1551 ftepp_update_output_condition(ftepp);
1554 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1555 ftepp_inmacro(ftepp, "elifndef");
1556 if (!ftepp_else_allowed(ftepp))
1558 if (!ftepp_ifdef(ftepp, &cond))
1561 pc = &vec_last(ftepp->conditions);
1562 pc->on = !pc->was_on && cond.on;
1563 pc->was_on = pc->was_on || pc->on;
1564 ftepp_update_output_condition(ftepp);
1567 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1568 ftepp_inmacro(ftepp, "elif");
1569 if (!ftepp_else_allowed(ftepp))
1571 if (!ftepp_if(ftepp, &cond))
1573 pc = &vec_last(ftepp->conditions);
1574 pc->on = !pc->was_on && cond.on;
1575 pc->was_on = pc->was_on || pc->on;
1576 ftepp_update_output_condition(ftepp);
1579 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1580 ftepp_inmacro(ftepp, "if");
1581 if (!ftepp_if(ftepp, &cond))
1583 cond.was_on = cond.on;
1584 vec_push(ftepp->conditions, cond);
1585 ftepp->output_on = ftepp->output_on && cond.on;
1588 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1589 ftepp_inmacro(ftepp, "else");
1590 if (!ftepp_else_allowed(ftepp))
1592 pc = &vec_last(ftepp->conditions);
1593 pc->on = !pc->was_on;
1594 pc->had_else = true;
1596 ftepp_update_output_condition(ftepp);
1599 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1600 ftepp_inmacro(ftepp, "endif");
1601 if (!vec_size(ftepp->conditions)) {
1602 ftepp_error(ftepp, "#endif without #if");
1605 vec_pop(ftepp->conditions);
1607 ftepp_update_output_condition(ftepp);
1610 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1611 ftepp_inmacro(ftepp, "include");
1612 return ftepp_include(ftepp);
1614 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1615 ftepp_out(ftepp, "#", false);
1618 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1619 ftepp_directive_warning(ftepp);
1622 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1623 ftepp_directive_error(ftepp);
1626 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1627 ftepp_directive_message(ftepp);
1631 if (ftepp->output_on) {
1632 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1639 /* break; never reached */
1641 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1644 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1647 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1650 /* Builtins! Don't forget the builtins! */
1651 case TOKEN_INTCONST:
1652 case TOKEN_FLOATCONST:
1653 ftepp_out(ftepp, "#", false);
1656 if (!ftepp_skipspace(ftepp))
1661 static bool ftepp_preprocess(ftepp_t *ftepp)
1664 bool newline = true;
1667 char *expand = NULL;
1669 ftepp->lex->flags.preprocessing = true;
1670 ftepp->lex->flags.mergelines = false;
1671 ftepp->lex->flags.noops = true;
1676 if (ftepp->token >= TOKEN_EOF)
1682 switch (ftepp->token) {
1685 case TOKEN_TYPENAME:
1686 /* is it a predef? */
1687 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1688 char *(*predef)(lex_file*) = ftepp_predef(ftepp_tokval(ftepp));
1690 expand = predef(ftepp->lex);
1691 ftepp_out (ftepp, expand, false);
1699 if (ftepp->output_on)
1700 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1705 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1709 if (!ftepp_macro_call(ftepp, macro))
1710 ftepp->token = TOKEN_ERROR;
1714 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1718 ftepp->lex->flags.mergelines = true;
1719 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1720 ftepp_error(ftepp, "error in preprocessor directive");
1721 ftepp->token = TOKEN_ERROR;
1724 if (!ftepp_hash(ftepp))
1725 ftepp->token = TOKEN_ERROR;
1726 ftepp->lex->flags.mergelines = false;
1730 ftepp_out(ftepp, "\n", true);
1734 /* same as default but don't set newline=false */
1735 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1740 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1744 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1746 /* force a 0 at the end but don't count it as added to the output */
1747 vec_push(ftepp->output_string, 0);
1748 vec_shrinkby(ftepp->output_string, 1);
1750 return (ftepp->token == TOKEN_EOF);
1753 /* Like in parser.c - files keep the previous state so we have one global
1754 * preprocessor. Except here we will want to warn about dangling #ifs.
1756 static bool ftepp_preprocess_done(ftepp_t *ftepp)
1759 if (vec_size(ftepp->conditions)) {
1760 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1763 lex_close(ftepp->lex);
1765 if (ftepp->itemname) {
1766 mem_d(ftepp->itemname);
1767 ftepp->itemname = NULL;
1772 bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
1774 ftepp->lex = lex_open(filename);
1775 ftepp->itemname = util_strdup(filename);
1777 con_out("failed to open file \"%s\"\n", filename);
1780 if (!ftepp_preprocess(ftepp))
1782 return ftepp_preprocess_done(ftepp);
1785 bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
1787 ftepp->lex = lex_open_string(str, strlen(str), name);
1788 ftepp->itemname = util_strdup(name);
1790 con_out("failed to create lexer for string \"%s\"\n", name);
1793 if (!ftepp_preprocess(ftepp))
1795 return ftepp_preprocess_done(ftepp);
1799 void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
1800 char *create = NULL;
1802 /* use saner path for empty macros */
1804 ftepp_add_define(ftepp, "__builtin__", name);
1808 vec_append(create, 8, "#define ");
1809 vec_append(create, strlen(name), name);
1810 vec_push (create, ' ');
1811 vec_append(create, strlen(value), value);
1812 vec_push (create, 0);
1814 ftepp_preprocess_string(ftepp, "__builtin__", create);
1818 ftepp_t *ftepp_create()
1824 ftepp = ftepp_new();
1828 memset(minor, 0, sizeof(minor));
1829 memset(major, 0, sizeof(major));
1831 /* set the right macro based on the selected standard */
1832 ftepp_add_define(ftepp, NULL, "GMQCC");
1833 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1834 ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
1843 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1844 ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
1845 platform_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1846 platform_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1847 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1848 ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
1849 platform_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1850 platform_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1851 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1852 ftepp_add_define(ftepp, NULL, "__STD_QCC__");
1863 ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
1864 ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
1867 * We're going to just make __NULL__ nil, which works for 60% of the
1868 * cases of __NULL_ for fteqcc.
1870 ftepp_add_macro(ftepp, "__NULL__", "nil");
1875 void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
1878 lex_ctx_t ctx = { "__builtin__", 0, 0 };
1880 macro = ppmacro_new(ctx, name);
1881 /*vec_push(ftepp->macros, macro);*/
1882 util_htset(ftepp->macros, name, macro);
1885 const char *ftepp_get(ftepp_t *ftepp)
1887 return ftepp->output_string;
1890 void ftepp_flush(ftepp_t *ftepp)
1892 ftepp_flush_do(ftepp);
1895 void ftepp_finish(ftepp_t *ftepp)
1899 ftepp_delete(ftepp);