+/**
+ * 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 end of file 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 end of file 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, bool resetline)
+{
+ char *buffer = NULL;
+ char *old_string = ftepp->output_string;
+ char *inner_string;
+ lex_file *old_lexer = ftepp->lex;
+ size_t vararg_start = vec_size(macro->params);
+ bool retval = true;
+ bool has_newlines;
+ size_t varargs;
+
+ size_t o, pi;
+ lex_file *inlex;
+
+ bool old_inmacro;
+
+ 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");
+ vec_free(old_string);
+ 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_VA_ARGS_ARRAY:
+ if ((size_t)out->constval.i >= varargs) {
+ ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
+ vec_free(old_string);
+ return false;
+ }
+
+ ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
+ break;
+
+ case TOKEN_VA_COUNT:
+ util_asprintf(&buffer, "%d", varargs);
+ ftepp_out(ftepp, buffer, false);
+ mem_d(buffer);
+ 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;
+ }
+
+ inlex->line = ftepp->lex->line;
+ inlex->sline = ftepp->lex->sline;
+ ftepp->lex = inlex;
+
+ old_inmacro = ftepp->in_macro;
+ ftepp->in_macro = true;
+ ftepp->output_string = NULL;
+ if (!ftepp_preprocess(ftepp)) {
+ ftepp->in_macro = old_inmacro;
+ vec_free(ftepp->lex->open_string);
+ vec_free(ftepp->output_string);
+ lex_close(ftepp->lex);
+ retval = false;
+ goto cleanup;
+ }
+ ftepp->in_macro = old_inmacro;
+ vec_free(ftepp->lex->open_string);
+ lex_close(ftepp->lex);
+
+ inner_string = ftepp->output_string;
+ ftepp->output_string = old_string;
+
+ has_newlines = (strchr(inner_string, '\n') != NULL);
+
+ if (has_newlines && !old_inmacro)
+ ftepp_recursion_header(ftepp);
+
+ vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
+ vec_free(inner_string);
+
+ if (has_newlines && !old_inmacro)
+ ftepp_recursion_footer(ftepp);
+
+ if (resetline && !ftepp->in_macro) {
+ char lineno[128];
+ util_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
+ ftepp_out(ftepp, lineno, false);
+ }
+
+ 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;
+ size_t paramline;
+
+ if (!macro->has_params) {
+ if (!ftepp_macro_expand(ftepp, macro, NULL, false))
+ 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);
+ paramline = ftepp->lex->sline;
+ 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, (paramline != ftepp->lex->sline)))
+ retval = false;
+ ftepp_next(ftepp);
+
+cleanup:
+ for (o = 0; o < vec_size(params); ++o)
+ macroparam_clean(¶ms[o]);
+ vec_free(params);
+ return retval;
+}
+