+static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
+{
+ if (ignore_cond || ftepp->output_on)
+ {
+ size_t len;
+ char *data;
+ len = strlen(str);
+ data = vec_add(ftepp->output_string, len);
+ memcpy(data, str, len);
+ }
+}
+
+static void ftepp_update_output_condition(ftepp_t *ftepp)
+{
+ size_t i;
+ ftepp->output_on = true;
+ for (i = 0; i < vec_size(ftepp->conditions); ++i)
+ ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
+}
+
+static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
+{
+ size_t i;
+ for (i = 0; i < vec_size(ftepp->macros); ++i) {
+ if (!strcmp(name, ftepp->macros[i]->name))
+ return ftepp->macros[i];
+ }
+ return NULL;
+}
+
+static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
+{
+ size_t i;
+ for (i = 0; i < vec_size(ftepp->macros); ++i) {
+ if (!strcmp(name, ftepp->macros[i]->name)) {
+ vec_remove(ftepp->macros, i, 1);
+ return;
+ }
+ }
+}
+
+static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
+{
+ return (ftepp->token = lex_do(ftepp->lex));
+}
+
+/* Important: this does not skip newlines! */
+static bool ftepp_skipspace(ftepp_t *ftepp)
+{
+ if (ftepp->token != TOKEN_WHITE)
+ return true;
+ while (ftepp_next(ftepp) == TOKEN_WHITE) {}
+ if (ftepp->token >= TOKEN_EOF) {
+ ftepp_error(ftepp, "unexpected end of preprocessor directive");
+ return false;
+ }
+ return true;
+}
+
+/* this one skips EOLs as well */
+static bool ftepp_skipallwhite(ftepp_t *ftepp)
+{
+ if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
+ return true;
+ do {
+ ftepp_next(ftepp);
+ } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
+ if (ftepp->token >= TOKEN_EOF) {
+ ftepp_error(ftepp, "unexpected end of preprocessor directive");
+ return false;
+ }
+ return true;
+}
+
+/**
+ * The huge macro parsing code...
+ */
+static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
+{
+ do {
+ ftepp_next(ftepp);
+ if (!ftepp_skipspace(ftepp))
+ return false;
+ if (ftepp->token == ')')
+ break;
+ switch (ftepp->token) {
+ case TOKEN_IDENT:
+ case TOKEN_TYPENAME:
+ case TOKEN_KEYWORD:
+ vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
+ break;
+ case TOKEN_DOTS:
+ macro->variadic = true;
+ break;
+ default:
+ ftepp_error(ftepp, "unexpected token in parameter list");
+ return false;
+ }
+ ftepp_next(ftepp);
+ if (!ftepp_skipspace(ftepp))
+ return false;
+ if (macro->variadic && ftepp->token != ')') {
+ ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
+ return false;
+ }
+ } while (ftepp->token == ',');
+ if (ftepp->token != ')') {
+ ftepp_error(ftepp, "expected closing paren after macro parameter list");
+ return false;
+ }
+ ftepp_next(ftepp);
+ /* skipspace happens in ftepp_define */
+ return true;
+}
+
+static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
+{
+ pptoken *ptok;
+ while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
+ if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__"))
+ ftepp->token = TOKEN_VA_ARGS;
+ ptok = pptoken_make(ftepp);
+ vec_push(macro->output, ptok);
+ ftepp_next(ftepp);
+ }
+ /* recursive expansion can cause EOFs here */
+ if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
+ ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
+ return false;
+ }
+ return true;
+}
+
+static bool ftepp_define(ftepp_t *ftepp)
+{
+ ppmacro *macro;
+ size_t l = ftepp_ctx(ftepp).line;
+
+ (void)ftepp_next(ftepp);
+ if (!ftepp_skipspace(ftepp))
+ return false;
+
+ switch (ftepp->token) {
+ case TOKEN_IDENT:
+ case TOKEN_TYPENAME:
+ case TOKEN_KEYWORD:
+ macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
+ if (macro && ftepp->output_on) {
+ if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
+ return false;
+ ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
+ }
+ macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
+ break;
+ default:
+ ftepp_error(ftepp, "expected macro name");
+ return false;
+ }
+
+ (void)ftepp_next(ftepp);
+
+ if (ftepp->token == '(') {
+ macro->has_params = true;
+ if (!ftepp_define_params(ftepp, macro))
+ return false;
+ }
+
+ if (!ftepp_skipspace(ftepp))
+ return false;
+
+ if (!ftepp_define_body(ftepp, macro))
+ return false;
+
+ if (ftepp->output_on)
+ vec_push(ftepp->macros, macro);
+ else {
+ ppmacro_delete(macro);
+ }
+
+ for (; l < ftepp_ctx(ftepp).line; ++l)
+ ftepp_out(ftepp, "\n", true);
+ return true;
+}
+
+/**
+ * When a macro is used we have to handle parameters as well
+ * as special-concatenation via ## or stringification via #
+ *
+ * Note: parenthesis can nest, so FOO((a),b) is valid, but only
+ * this kind of parens. Curly braces or [] don't count towards the
+ * paren-level.
+ */
+typedef struct {
+ pptoken **tokens;
+} macroparam;
+
+static void macroparam_clean(macroparam *self)
+{
+ size_t i;
+ for (i = 0; i < vec_size(self->tokens); ++i)
+ pptoken_delete(self->tokens[i]);
+ vec_free(self->tokens);
+}
+
+/* need to leave the last token up */
+static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
+{
+ macroparam *params = NULL;
+ pptoken *ptok;
+ macroparam mp;
+ size_t parens = 0;
+ size_t i;
+
+ if (!ftepp_skipallwhite(ftepp))
+ return false;
+ while (ftepp->token != ')') {
+ mp.tokens = NULL;
+ if (!ftepp_skipallwhite(ftepp))
+ return false;
+ while (parens || ftepp->token != ',') {
+ if (ftepp->token == '(')
+ ++parens;
+ else if (ftepp->token == ')') {
+ if (!parens)
+ break;
+ --parens;
+ }
+ ptok = pptoken_make(ftepp);
+ vec_push(mp.tokens, ptok);
+ if (ftepp_next(ftepp) >= TOKEN_EOF) {
+ ftepp_error(ftepp, "unexpected EOF in macro call");
+ goto on_error;
+ }
+ }
+ vec_push(params, mp);
+ mp.tokens = NULL;
+ if (ftepp->token == ')')
+ break;
+ if (ftepp->token != ',') {
+ ftepp_error(ftepp, "expected closing paren or comma in macro call");
+ goto on_error;
+ }
+ if (ftepp_next(ftepp) >= TOKEN_EOF) {
+ ftepp_error(ftepp, "unexpected EOF in macro call");
+ goto on_error;
+ }
+ }
+ /* need to leave that up
+ if (ftepp_next(ftepp) >= TOKEN_EOF) {
+ ftepp_error(ftepp, "unexpected EOF in macro call");
+ goto on_error;
+ }
+ */
+ *out_params = params;
+ return true;
+
+on_error:
+ if (mp.tokens)
+ macroparam_clean(&mp);
+ for (i = 0; i < vec_size(params); ++i)
+ macroparam_clean(¶ms[i]);
+ vec_free(params);
+ return false;
+}
+
+static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
+{
+ size_t i;
+ for (i = 0; i < vec_size(macro->params); ++i) {
+ if (!strcmp(macro->params[i], name)) {
+ *idx = i;
+ return true;
+ }
+ }
+ return false;
+}
+
+static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
+{
+ char chs[2];
+ const char *ch;
+ chs[1] = 0;
+ switch (token->token) {
+ case TOKEN_STRINGCONST:
+ ch = token->value;
+ while (*ch) {
+ /* in preprocessor mode strings already are string,
+ * so we don't get actual newline bytes here.
+ * Still need to escape backslashes and quotes.
+ */
+ switch (*ch) {
+ case '\\': ftepp_out(ftepp, "\\\\", false); break;
+ case '"': ftepp_out(ftepp, "\\\"", false); break;
+ default:
+ chs[0] = *ch;
+ ftepp_out(ftepp, chs, false);
+ break;
+ }
+ ++ch;
+ }
+ break;
+ case TOKEN_WHITE:
+ ftepp_out(ftepp, " ", false);
+ break;
+ case TOKEN_EOL:
+ ftepp_out(ftepp, "\\n", false);
+ break;
+ default:
+ ftepp_out(ftepp, token->value, false);
+ break;
+ }
+}
+
+static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
+{
+ size_t i;
+ ftepp_out(ftepp, "\"", false);
+ for (i = 0; i < vec_size(param->tokens); ++i)
+ ftepp_stringify_token(ftepp, param->tokens[i]);
+ ftepp_out(ftepp, "\"", false);
+}
+
+static void ftepp_recursion_header(ftepp_t *ftepp)
+{
+ ftepp_out(ftepp, "\n#pragma push(line)\n", false);
+}
+
+static void ftepp_recursion_footer(ftepp_t *ftepp)
+{
+ ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
+}
+
+static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
+{
+ size_t i;
+ pptoken *out;
+ for (i = 0; i < vec_size(param->tokens); ++i) {
+ out = param->tokens[i];
+ if (out->token == TOKEN_EOL)
+ ftepp_out(ftepp, "\n", false);
+ else
+ ftepp_out(ftepp, out->value, false);
+ }
+}
+
+static bool ftepp_preprocess(ftepp_t *ftepp);
+static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
+{
+ char *old_string = ftepp->output_string;
+ lex_file *old_lexer = ftepp->lex;
+ size_t vararg_start = vec_size(macro->params);
+ bool retval = true;
+ size_t varargs;
+
+ size_t o, pi;
+ lex_file *inlex;
+
+ int nextok;
+
+ if (vararg_start < vec_size(params))
+ varargs = vec_size(params) - vararg_start;
+ else
+ varargs = 0;
+
+ /* really ... */
+ if (!vec_size(macro->output))
+ return true;
+
+ ftepp->output_string = NULL;
+ for (o = 0; o < vec_size(macro->output); ++o) {
+ pptoken *out = macro->output[o];
+ switch (out->token) {
+ case TOKEN_VA_ARGS:
+ if (!macro->variadic) {
+ ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
+ return false;
+ }
+ if (!varargs)
+ break;
+ pi = 0;
+ ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
+ for (++pi; pi < varargs; ++pi) {
+ ftepp_out(ftepp, ", ", false);
+ ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
+ }
+ break;
+ case TOKEN_IDENT:
+ case TOKEN_TYPENAME:
+ case TOKEN_KEYWORD:
+ if (!macro_params_find(macro, out->value, &pi)) {
+ ftepp_out(ftepp, out->value, false);
+ break;
+ } else
+ ftepp_param_out(ftepp, ¶ms[pi]);
+ break;
+ case '#':
+ if (o + 1 < vec_size(macro->output)) {
+ nextok = macro->output[o+1]->token;
+ if (nextok == '#') {
+ /* raw concatenation */
+ ++o;
+ break;
+ }
+ if ( (nextok == TOKEN_IDENT ||
+ nextok == TOKEN_KEYWORD ||
+ nextok == TOKEN_TYPENAME) &&
+ macro_params_find(macro, macro->output[o+1]->value, &pi))
+ {
+ ++o;
+ ftepp_stringify(ftepp, ¶ms[pi]);
+ break;
+ }
+ }
+ ftepp_out(ftepp, "#", false);
+ break;
+ case TOKEN_EOL:
+ ftepp_out(ftepp, "\n", false);
+ break;
+ default:
+ ftepp_out(ftepp, out->value, false);
+ break;
+ }
+ }
+ vec_push(ftepp->output_string, 0);
+ /* Now run the preprocessor recursively on this string buffer */
+ /*
+ printf("__________\n%s\n=========\n", ftepp->output_string);
+ */
+ inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
+ if (!inlex) {
+ ftepp_error(ftepp, "internal error: failed to instantiate lexer");
+ retval = false;
+ goto cleanup;
+ }
+ ftepp->output_string = old_string;
+ inlex->line = ftepp->lex->line;
+ inlex->sline = ftepp->lex->sline;
+ ftepp->lex = inlex;
+ ftepp_recursion_header(ftepp);
+ if (!ftepp_preprocess(ftepp)) {
+ vec_free(ftepp->lex->open_string);
+ old_string = ftepp->output_string;
+ lex_close(ftepp->lex);
+ retval = false;
+ goto cleanup;
+ }
+ vec_free(ftepp->lex->open_string);
+ ftepp_recursion_footer(ftepp);
+ old_string = ftepp->output_string;
+
+cleanup:
+ ftepp->lex = old_lexer;
+ ftepp->output_string = old_string;
+ return retval;
+}
+
+static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
+{
+ size_t o;
+ macroparam *params = NULL;
+ bool retval = true;
+
+ if (!macro->has_params) {
+ if (!ftepp_macro_expand(ftepp, macro, NULL))
+ return false;
+ ftepp_next(ftepp);
+ return true;
+ }
+ ftepp_next(ftepp);
+
+ if (!ftepp_skipallwhite(ftepp))
+ return false;
+
+ if (ftepp->token != '(') {
+ ftepp_error(ftepp, "expected macro parameters in parenthesis");
+ return false;
+ }
+
+ ftepp_next(ftepp);
+ if (!ftepp_macro_call_params(ftepp, ¶ms))
+ return false;
+
+ if ( vec_size(params) < vec_size(macro->params) ||
+ (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
+ {
+ ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
+ (macro->variadic ? " at least" : ""),
+ (unsigned int)vec_size(macro->params),
+ (unsigned int)vec_size(params));
+ retval = false;
+ goto cleanup;
+ }
+
+ if (!ftepp_macro_expand(ftepp, macro, params))
+ retval = false;
+ ftepp_next(ftepp);
+
+cleanup:
+ for (o = 0; o < vec_size(params); ++o)
+ macroparam_clean(¶ms[o]);
+ vec_free(params);
+ return retval;
+}
+
+/**
+ * #if - the FTEQCC way:
+ * defined(FOO) => true if FOO was #defined regardless of parameters or contents
+ * <numbers> => True if the number is not 0
+ * !<factor> => True if the factor yields false
+ * !!<factor> => ERROR on 2 or more unary nots
+ * <macro> => becomes the macro's FIRST token regardless of parameters
+ * <e> && <e> => True if both expressions are true
+ * <e> || <e> => True if either expression is true
+ * <string> => False
+ * <ident> => False (remember for macros the <macro> rule applies instead)
+ * Unary + and - are weird and wrong in fteqcc so we don't allow them
+ * parenthesis in expressions are allowed
+ * parameter lists on macros are errors
+ * No mathematical calculations are executed
+ */
+static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
+static bool ftepp_if_op(ftepp_t *ftepp)
+{
+ ftepp->lex->flags.noops = false;
+ ftepp_next(ftepp);
+ if (!ftepp_skipspace(ftepp))
+ return false;
+ ftepp->lex->flags.noops = true;
+ return true;
+}
+static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
+{
+ ppmacro *macro;
+ bool wasnot = false;
+ bool wasneg = false;
+
+ if (!ftepp_skipspace(ftepp))
+ return false;
+
+ while (ftepp->token == '!') {
+ wasnot = true;
+ ftepp_next(ftepp);
+ if (!ftepp_skipspace(ftepp))
+ return false;
+ }
+
+ if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
+ {
+ wasneg = true;
+ ftepp_next(ftepp);
+ if (!ftepp_skipspace(ftepp))
+ return false;
+ }
+
+ switch (ftepp->token) {
+ case TOKEN_IDENT:
+ case TOKEN_TYPENAME:
+ case TOKEN_KEYWORD:
+ if (!strcmp(ftepp_tokval(ftepp), "defined")) {
+ ftepp_next(ftepp);
+ if (!ftepp_skipspace(ftepp))
+ return false;
+ if (ftepp->token != '(') {
+ ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
+ return false;
+ }
+ ftepp_next(ftepp);
+ if (!ftepp_skipspace(ftepp))
+ return false;
+ if (ftepp->token != TOKEN_IDENT &&
+ ftepp->token != TOKEN_TYPENAME &&
+ ftepp->token != TOKEN_KEYWORD)
+ {
+ ftepp_error(ftepp, "defined() used on an unexpected token type");
+ return false;
+ }
+ macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
+ *out = !!macro;
+ ftepp_next(ftepp);
+ if (!ftepp_skipspace(ftepp))
+ return false;
+ if (ftepp->token != ')') {
+ ftepp_error(ftepp, "expected closing paren");
+ return false;
+ }
+ break;
+ }
+
+ macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
+ if (!macro || !vec_size(macro->output)) {
+ *out = false;
+ *value_out = 0;
+ } else {
+ /* This does not expand recursively! */
+ switch (macro->output[0]->token) {
+ case TOKEN_INTCONST:
+ *value_out = macro->output[0]->constval.i;
+ *out = !!(macro->output[0]->constval.i);
+ break;
+ case TOKEN_FLOATCONST:
+ *value_out = macro->output[0]->constval.f;
+ *out = !!(macro->output[0]->constval.f);
+ break;
+ default:
+ *out = false;
+ break;
+ }
+ }
+ break;
+ case TOKEN_STRINGCONST:
+ *value_out = 0;
+ *out = false;
+ break;
+ case TOKEN_INTCONST:
+ *value_out = ftepp->lex->tok.constval.i;
+ *out = !!(ftepp->lex->tok.constval.i);
+ break;
+ case TOKEN_FLOATCONST:
+ *value_out = ftepp->lex->tok.constval.f;
+ *out = !!(ftepp->lex->tok.constval.f);
+ break;
+
+ case '(':
+ ftepp_next(ftepp);
+ if (!ftepp_if_expr(ftepp, out, value_out))
+ return false;
+ if (ftepp->token != ')') {
+ ftepp_error(ftepp, "expected closing paren in #if expression");
+ return false;
+ }
+ break;
+
+ default:
+ ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
+ if (opts.debug)
+ ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
+ return false;
+ }
+ if (wasneg)
+ *value_out = -*value_out;
+ if (wasnot) {
+ *out = !*out;
+ *value_out = (*out ? 1 : 0);
+ }
+ return true;
+}
+
+/*
+static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
+{
+ if (!ftepp_next(ftepp))
+ return false;
+ return ftepp_if_value(ftepp, out, value_out);
+}
+*/
+
+static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
+{
+ if (!ftepp_if_value(ftepp, out, value_out))
+ return false;
+
+ if (!ftepp_if_op(ftepp))
+ return false;
+
+ if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
+ return true;
+
+ /* FTEQCC is all right-associative and no precedence here */
+ if (!strcmp(ftepp_tokval(ftepp), "&&") ||
+ !strcmp(ftepp_tokval(ftepp), "||"))
+ {
+ bool next = false;
+ char opc = ftepp_tokval(ftepp)[0];
+ double nextvalue;
+
+ (void)nextvalue;
+ if (!ftepp_next(ftepp))
+ return false;
+ if (!ftepp_if_expr(ftepp, &next, &nextvalue))
+ return false;
+
+ if (opc == '&')
+ *out = *out && next;
+ else
+ *out = *out || next;
+
+ *value_out = (*out ? 1 : 0);
+ return true;
+ }
+ else if (!strcmp(ftepp_tokval(ftepp), "==") ||
+ !strcmp(ftepp_tokval(ftepp), "!=") ||
+ !strcmp(ftepp_tokval(ftepp), ">=") ||
+ !strcmp(ftepp_tokval(ftepp), "<=") ||
+ !strcmp(ftepp_tokval(ftepp), ">") ||
+ !strcmp(ftepp_tokval(ftepp), "<"))
+ {
+ bool next = false;
+ const char opc0 = ftepp_tokval(ftepp)[0];
+ const char opc1 = ftepp_tokval(ftepp)[1];
+ double other;
+
+ if (!ftepp_next(ftepp))
+ return false;
+ if (!ftepp_if_expr(ftepp, &next, &other))
+ return false;
+
+ if (opc0 == '=')
+ *out = (*value_out == other);
+ else if (opc0 == '!')
+ *out = (*value_out != other);
+ else if (opc0 == '>') {
+ if (opc1 == '=') *out = (*value_out >= other);
+ else *out = (*value_out > other);
+ }
+ else if (opc0 == '<') {
+ if (opc1 == '=') *out = (*value_out <= other);
+ else *out = (*value_out < other);
+ }
+ *value_out = (*out ? 1 : 0);
+
+ return true;
+ }
+ else {
+ ftepp_error(ftepp, "junk after #if");
+ return false;