From fb683a2b1b962da4f00eb86c57b9c75fbcdf9908 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 30 Oct 2024 14:02:34 -0400 Subject: [PATCH] Organize typedef headers so the typedefs come before the namespace members --- compile.c | 66 ++++++++++++++++++++++++++++++++++++++----------------- compile.h | 3 ++- enums.c | 2 +- structs.c | 3 +-- tomo.c | 6 +++-- 5 files changed, 54 insertions(+), 26 deletions(-) diff --git a/compile.c b/compile.c index db1255b..a3ec1f7 100644 --- a/compile.c +++ b/compile.c @@ -805,7 +805,7 @@ CORD compile_statement(env_t *env, ast_t *ast) } else { code = CORD_all(ret_type_code, " ", name, arg_signature); if (fndef->is_inline) - code = CORD_cat("inline ", code); + code = CORD_cat("INLINE ", code); if (!is_private) code = CORD_cat("public ", code); } @@ -896,10 +896,12 @@ CORD compile_statement(env_t *env, ast_t *ast) else text = CORD_all(text, ")"); - env->code->function_naming = CORD_all( - env->code->function_naming, - CORD_asprintf("register_function(%r, Text(\"%r [%s.tm:%ld]\"));\n", - name, text, file_base_name(ast->file->filename), get_line_number(ast->file, ast->start))); + if (!fndef->is_inline) { + env->code->function_naming = CORD_all( + env->code->function_naming, + CORD_asprintf("register_function(%r, Text(\"%r [%s.tm:%ld]\"));\n", + name, text, file_base_name(ast->file->filename), get_line_number(ast->file, ast->start))); + } return CORD_EMPTY; } case Skip: { @@ -3433,16 +3435,6 @@ void compile_namespace(env_t *env, const char *ns_name, ast_t *block) } } -CORD compile_namespace_header(env_t *env, const char *ns_name, ast_t *block) -{ - env_t *ns_env = namespace_env(env, ns_name); - CORD header = CORD_EMPTY; - for (ast_list_t *stmt = block ? Match(block, Block)->statements : NULL; stmt; stmt = stmt->next) { - header = CORD_all(header, compile_statement_header(ns_env, stmt->ast)); - } - return header; -} - CORD compile_type_info(env_t *env, type_t *t) { if (t == THREAD_TYPE) return "&Thread$info"; @@ -3707,7 +3699,7 @@ CORD compile_file(env_t *env, ast_t *ast) "}\n"); } -CORD compile_statement_header(env_t *env, ast_t *ast) +CORD compile_statement_type_header(env_t *env, ast_t *ast) { switch (ast->tag) { case Declare: { @@ -3725,7 +3717,7 @@ CORD compile_statement_header(env_t *env, ast_t *ast) code_err(ast, "You can't declare a variable with a %T value", t); return CORD_all( - compile_statement_header(env, decl->value), + compile_statement_type_header(env, decl->value), "extern ", compile_declaration(t, CORD_cat(namespace_prefix(env, env->namespace), decl_name)), ";\n"); } case Use: { @@ -3761,8 +3753,7 @@ CORD compile_statement_header(env_t *env, ast_t *ast) "(text) ((", namespace_prefix(env, env->namespace), def->name, "_t){.length=sizeof(text)-1, .tag=TEXT_ASCII, .ascii=\"\" text})\n" "#define ", namespace_prefix(env, env->namespace), def->name, "s(...) ((", namespace_prefix(env, env->namespace), def->name, "_t)Texts(__VA_ARGS__))\n" - "extern const TypeInfo_t ", full_name, ";\n", - compile_namespace_header(env, def->name, def->namespace) + "extern const TypeInfo_t ", full_name, ";\n" ); } case FunctionDef: { @@ -3805,6 +3796,39 @@ CORD compile_statement_header(env_t *env, ast_t *ast) } } +CORD compile_statement_namespace_header(env_t *env, ast_t *ast) +{ + const char *ns_name = NULL; + ast_t *block = NULL; + switch (ast->tag) { + case LangDef: { + auto def = Match(ast, LangDef); + ns_name = def->name; + block = def->namespace; + break; + } + case StructDef: { + auto def = Match(ast, StructDef); + ns_name = def->name; + block = def->namespace; + break; + } + case EnumDef: { + auto def = Match(ast, EnumDef); + ns_name = def->name; + block = def->namespace; + break; + } + default: return CORD_EMPTY; + } + env_t *ns_env = namespace_env(env, ns_name); + CORD header = CORD_EMPTY; + for (ast_list_t *stmt = block ? Match(block, Block)->statements : NULL; stmt; stmt = stmt->next) { + header = CORD_all(header, compile_statement_namespace_header(ns_env, stmt->ast)); + } + return header; +} + typedef struct { env_t *env; CORD *header; @@ -3812,7 +3836,9 @@ typedef struct { static void _visit_statement(compile_typedef_info_t *info, ast_t *ast) { - *info->header = CORD_all(*info->header, compile_statement_header(info->env, ast)); + *info->header = CORD_all(*info->header, + compile_statement_type_header(info->env, ast), + compile_statement_namespace_header(info->env, ast)); } CORD compile_file_header(env_t *env, ast_t *ast) diff --git a/compile.h b/compile.h index 2a352f8..d6c0648 100644 --- a/compile.h +++ b/compile.h @@ -17,7 +17,8 @@ CORD compile(env_t *env, ast_t *ast); void compile_namespace(env_t *env, const char *ns_name, ast_t *block); CORD compile_namespace_header(env_t *env, const char *ns_name, ast_t *block); CORD compile_statement(env_t *env, ast_t *ast); -CORD compile_statement_header(env_t *env, ast_t *ast); +CORD compile_statement_type_header(env_t *env, ast_t *ast); +CORD compile_statement_namespace_header(env_t *env, ast_t *ast); CORD compile_type_info(env_t *env, type_t *t); CORD compile_cli_arg_call(env_t *env, CORD fn_name, type_t *fn_type); diff --git a/enums.c b/enums.c index da4dd08..0a4d336 100644 --- a/enums.c +++ b/enums.c @@ -106,7 +106,7 @@ CORD compile_enum_header(env_t *env, ast_t *ast) CORD constructor_def = CORD_all(full_name, "_t ", full_name, "$tagged$", tag->name, "(", arg_sig, ");\n"); all_defs = CORD_all(all_defs, constructor_def); } - return CORD_all(all_defs, compile_namespace_header(env, def->name, def->namespace)); + return all_defs; } // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/structs.c b/structs.c index 489d27a..5915f11 100644 --- a/structs.c +++ b/structs.c @@ -74,8 +74,7 @@ CORD compile_struct_header(env_t *env, ast_t *ast) "};\n" "};\n" "} ", namespace_prefix(env, env->namespace), "$Optional", def->name, "_t;\n" - "extern const TypeInfo_t ", full_name, ";\n", - compile_namespace_header(env, def->name, def->namespace)); + "extern const TypeInfo_t ", full_name, ";\n"); } // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/tomo.c b/tomo.c index 632b4e8..1832c29 100644 --- a/tomo.c +++ b/tomo.c @@ -203,10 +203,12 @@ static void _compile_statement_header_for_library(libheader_info_t *info, ast_t Text_t path = Text$from_str(use->path); if (!Table$get(*info->used_imports, &path, Table$info(&Path$info, &Path$info))) { Table$set(info->used_imports, &path, &path, Table$info(&Text$info, &Text$info)); - CORD_put(compile_statement_header(info->env, ast), info->output); + CORD_put(compile_statement_type_header(info->env, ast), info->output); + CORD_put(compile_statement_namespace_header(info->env, ast), info->output); } } else { - CORD_put(compile_statement_header(info->env, ast), info->output); + CORD_put(compile_statement_type_header(info->env, ast), info->output); + CORD_put(compile_statement_namespace_header(info->env, ast), info->output); } }