diff --git a/examples/add_dynamic_array_macro/build.cpp b/examples/add_dynamic_array_macro/build.cpp index ceded66..e42b4c1 100644 --- a/examples/add_dynamic_array_macro/build.cpp +++ b/examples/add_dynamic_array_macro/build.cpp @@ -8,7 +8,7 @@ bool add_dynamic_array_macro() { LC_RegisterPackageDir("../examples"); LC_Intern name = LC_ILit("add_dynamic_array_macro"); - LC_ParsePackagesUsingRegistry(name); + LC_ParsePackagesPass(name); LC_BuildIfPass(); if (L->errors) { LC_LangEnd(lang); @@ -159,14 +159,14 @@ bool add_dynamic_array_macro() { LC_DLLAdd(file->afile.fdecl, file->afile.ldecl, ast); } - LC_OrderAndResolveTopLevelDecls(name); - LC_ResolveAllProcBodies(); + LC_OrderAndResolveTopLevelPass(name); + LC_ResolveProcBodiesPass(); if (L->errors) { LC_LangEnd(lang); return false; } - LC_String code = LC_GenerateUnityBuild(L->ordered_packages); + LC_String code = LC_GenerateUnityBuild(); OS_MakeDir("examples/add_dynamic_array_macro"); OS_WriteFile("examples/add_dynamic_array_macro/add_dynamic_array_macro.c", code); diff --git a/examples/add_instrumentation/build.cpp b/examples/add_instrumentation/build.cpp index ed5f20b..22a0bc3 100644 --- a/examples/add_instrumentation/build.cpp +++ b/examples/add_instrumentation/build.cpp @@ -1,9 +1,7 @@ -bool OnDeclParsed_AddInstrumentation(bool discard, LC_AST *n) { - if (discard) return discard; - +void OnDeclParsed_AddInstrumentation(LC_AST *n) { if (n->kind == LC_ASTKind_DeclProc && n->dproc.body) { - if (n->dbase.name == LC_ILit("BeginProc")) return false; - if (n->dbase.name == LC_ILit("EndProc")) return false; + if (n->dbase.name == LC_ILit("BeginProc")) return; + if (n->dbase.name == LC_ILit("EndProc")) return; LC_AST *body = n->dproc.body; @@ -13,7 +11,6 @@ bool OnDeclParsed_AddInstrumentation(bool discard, LC_AST *n) { LC_DLLAddFront(body->sblock.first, body->sblock.last, end); LC_DLLAddFront(body->sblock.first, body->sblock.last, begin); } - return discard; } bool add_instrumentation() { @@ -26,37 +23,30 @@ bool add_instrumentation() { LC_RegisterPackageDir("../pkgs"); LC_RegisterPackageDir("../examples"); - LC_Intern name = LC_ILit("add_instrumentation"); - LC_ASTRefList packages = LC_ResolvePackageByName(name); - if (L->errors) { + LC_Intern name = LC_ILit("add_instrumentation"); + LC_ParseAndResolve(name); + if (lang->errors) { LC_LangEnd(lang); return false; } - DebugVerifyAST(packages); - if (L->errors) { + DebugVerifyAST(L->ordered_packages); + if (lang->errors) { LC_LangEnd(lang); return false; } - S8_String code = LC_GenerateUnityBuild(packages); + LC_String code = LC_GenerateUnityBuild(); + LC_LangEnd(lang); + S8_String path = "examples/add_instrumentation/add_instrumentation.c"; OS_MakeDir("examples"); OS_MakeDir("examples/add_instrumentation"); OS_WriteFile(path, code); - if (!UseCL) { - LC_LangEnd(lang); - return true; - } + if (!UseCL) return true; S8_String cmd = Fmt("cl %.*s -Zi -std:c11 -nologo -FC -Fd:examples/add_instrumentation/a.pdb -Fe:examples/add_instrumentation/add_instrumentation.exe %.*s", S8_Expand(path), S8_Expand(RaylibLIB)); int errcode = Run(cmd); - if (errcode != 0) { - LC_LangEnd(lang); - return false; - } - // Run("examples/add_instrumentation/add_instrumentation.exe"); - - LC_LangEnd(lang); + if (errcode != 0) return false; return true; } \ No newline at end of file diff --git a/examples/add_printf_format_check/build.cpp b/examples/add_printf_format_check/build.cpp index e88b9f4..62d113b 100644 --- a/examples/add_printf_format_check/build.cpp +++ b/examples/add_printf_format_check/build.cpp @@ -13,7 +13,7 @@ bool add_printf_format_check() { LC_RegisterPackageDir("../examples"); LC_Intern name = LC_ILit("add_printf_format_check"); - LC_ResolvePackageByName(name); + LC_ParseAndResolve(name); LC_LangEnd(lang); return result; diff --git a/examples/add_source_location_macro/build.cpp b/examples/add_source_location_macro/build.cpp index 29c41b3..00bf9da 100644 --- a/examples/add_source_location_macro/build.cpp +++ b/examples/add_source_location_macro/build.cpp @@ -26,23 +26,18 @@ bool add_source_location_macro() { LC_RegisterPackageDir("../examples/"); LC_RegisterPackageDir("../pkgs"); - LC_Intern name = LC_ILit("add_source_location_macro"); - LC_ASTRefList packages = LC_ResolvePackageByName(name); - if (L->errors) { - LC_LangEnd(lang); - return false; - } - - S8_String dir = "examples/add_source_location_macro"; - OS_MakeDir(dir); - S8_String cfile = "examples/add_source_location_macro/add_source_location_macro.c"; - S8_String code = LC_GenerateUnityBuild(packages); - OS_WriteFile(cfile, code); + LC_Intern name = LC_ILit("add_source_location_macro"); + LC_ParseAndResolve(name); if (L->errors) { LC_LangEnd(lang); return false; } + LC_String code = LC_GenerateUnityBuild(); LC_LangEnd(lang); + + OS_MakeDir("examples/add_source_location_macro"); + OS_WriteFile("examples/add_source_location_macro/add_source_location_macro.c", code); + return true; } \ No newline at end of file diff --git a/examples/create_raylib_window/build.cpp b/examples/create_raylib_window/build.cpp index 8fd4daf..8f4863c 100644 --- a/examples/create_raylib_window/build.cpp +++ b/examples/create_raylib_window/build.cpp @@ -6,8 +6,8 @@ bool create_raylib_window() { LC_RegisterPackageDir("../examples/"); LC_RegisterPackageDir("../pkgs"); - LC_Intern name = LC_ILit("create_raylib_window"); - LC_ASTRefList packages = LC_ResolvePackageByName(name); + LC_Intern name = LC_ILit("create_raylib_window"); + LC_ParseAndResolve(name); if (L->errors) { LC_LangEnd(lang); return false; @@ -15,7 +15,7 @@ bool create_raylib_window() { OS_MakeDir("examples/create_raylib_window"); S8_String path = "examples/create_raylib_window/create_raylib_window.c"; - S8_String code = LC_GenerateUnityBuild(packages); + LC_String code = LC_GenerateUnityBuild(); OS_WriteFile(path, code); bool success = true; diff --git a/examples/generate_type_info/build.cpp b/examples/generate_type_info/build.cpp index e0c7c5a..9d3cfd1 100644 --- a/examples/generate_type_info/build.cpp +++ b/examples/generate_type_info/build.cpp @@ -11,14 +11,14 @@ bool generate_type_info() { LC_Intern name = LC_ILit("generate_type_info"); - LC_ParsePackagesUsingRegistry(name); + LC_ParsePackagesPass(name); LC_BuildIfPass(); if (L->errors) { LC_LangEnd(lang); return false; } - LC_OrderAndResolveTopLevelDecls(name); + LC_OrderAndResolveTopLevelPass(name); if (L->errors) { LC_LangEnd(lang); return false; @@ -55,9 +55,9 @@ bool generate_type_info() { } // Resolve decls again with new content added in - LC_OrderAndResolveTopLevelDecls(name); - LC_ResolveAllProcBodies(); - LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls(); + LC_OrderAndResolveTopLevelPass(name); + LC_ResolveProcBodiesPass(); + LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(); bool result = L->errors ? false : true; LC_LangEnd(lang); diff --git a/examples/hello_world/build.cpp b/examples/hello_world/build.cpp index 836e46d..a67b7ce 100644 --- a/examples/hello_world/build.cpp +++ b/examples/hello_world/build.cpp @@ -6,24 +6,23 @@ bool hello_world() { LC_RegisterPackageDir("../examples/"); LC_RegisterPackageDir("../pkgs"); - LC_Intern name = LC_ILit("hello_world"); - LC_ASTRefList packages = LC_ResolvePackageByName(name); - if (L->errors) { + LC_Intern name = LC_ILit("hello_world"); + LC_ParseAndResolve(name); + if (lang->errors) { LC_LangEnd(lang); return false; } OS_MakeDir("examples/hello_world"); - S8_String code = LC_GenerateUnityBuild(packages); + LC_ParseAndResolve(name); + LC_String code = LC_GenerateUnityBuild(); + LC_LangEnd(lang); + S8_String path = "examples/hello_world/hello_world.c"; OS_WriteFile(path, code); - - bool success = true; if (UseCL) { - S8_String cmd = Fmt("cl %.*s -Zi -std:c11 -nologo -FC -Fd:examples/hello_world/hello_world.pdb -Fe:examples/hello_world/hello_world.exe %.*s", S8_Expand(path), S8_Expand(RaylibLIB)); - if (Run(cmd) != 0) success = false; + S8_String cmd = Fmt("cl %.*s -nologo -FC -Fd:examples/hello_world/hello_world.pdb -Fe:examples/hello_world/hello_world.exe", S8_Expand(path)); + if (Run(cmd) != 0) return false; } - - LC_LangEnd(lang); - return success; + return true; } \ No newline at end of file diff --git a/examples/pathfinding_visualizer/build.cpp b/examples/pathfinding_visualizer/build.cpp index ef0942b..d634f4a 100644 --- a/examples/pathfinding_visualizer/build.cpp +++ b/examples/pathfinding_visualizer/build.cpp @@ -7,15 +7,15 @@ bool pathfinding_visualizer() { LC_RegisterPackageDir("../examples/"); LC_RegisterPackageDir("../pkgs"); - LC_Intern name = LC_ILit("pathfinding_visualizer"); - LC_ASTRefList packages = LC_ResolvePackageByName(name); - LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls(); + LC_Intern name = LC_ILit("pathfinding_visualizer"); + LC_ParseAndResolve(name); + LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(); if (L->errors) { LC_LangEnd(lang); return false; } - DebugVerifyAST(packages); + DebugVerifyAST(L->ordered_packages); if (L->errors) { LC_LangEnd(lang); return false; @@ -23,7 +23,7 @@ bool pathfinding_visualizer() { OS_MakeDir("examples"); OS_MakeDir("examples/pathfinding_visualizer"); - S8_String code = LC_GenerateUnityBuild(packages); + LC_String code = LC_GenerateUnityBuild(); S8_String path = "examples/pathfinding_visualizer/pathfinding_visualizer.c"; OS_WriteFile(path, code); diff --git a/examples/sandbox/build.cpp b/examples/sandbox/build.cpp index f8ed0dd..e44a62e 100644 --- a/examples/sandbox/build.cpp +++ b/examples/sandbox/build.cpp @@ -64,7 +64,7 @@ bool sandbox() { LC_RegisterPackageDir("../examples"); LC_Intern name = LC_ILit("sandbox"); - LC_ParsePackagesUsingRegistry(name); + LC_ParsePackagesPass(name); LC_BuildIfPass(); if (L->errors) { LC_LangEnd(lang); @@ -97,14 +97,14 @@ bool sandbox() { LC_DLLAdd(package->apackage.ffile, package->apackage.lfile, new_array_file); } - LC_OrderAndResolveTopLevelDecls(name); - LC_ResolveAllProcBodies(); + LC_OrderAndResolveTopLevelPass(name); + LC_ResolveProcBodiesPass(); if (L->errors) { LC_LangEnd(lang); return false; } - S8_String code = LC_GenerateUnityBuild(L->ordered_packages); + LC_String code = LC_GenerateUnityBuild(); S8_String path = "examples/sandbox/sandbox.c"; OS_MakeDir("examples/sandbox"); diff --git a/examples/text_editor/build.cpp b/examples/text_editor/build.cpp index 2d117bb..1c1c068 100644 --- a/examples/text_editor/build.cpp +++ b/examples/text_editor/build.cpp @@ -8,20 +8,20 @@ bool text_editor() { LC_RegisterPackageDir("../pkgs"); LC_RegisterPackageDir("../examples"); - LC_Intern name = LC_ILit("text_editor"); - LC_ASTRefList packages = LC_ResolvePackageByName(name); + LC_Intern name = LC_ILit("text_editor"); + LC_ParseAndResolve(name); if (L->errors) return false; - DebugVerifyAST(packages); + DebugVerifyAST(L->ordered_packages); if (L->errors) return false; - LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls(); + LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(); OS_MakeDir("examples"); OS_MakeDir("examples/text_editor"); OS_CopyFile(RaylibDLL, "examples/text_editor/raylib.dll", true); - S8_String code = LC_GenerateUnityBuild(packages); + LC_String code = LC_GenerateUnityBuild(); S8_String path = "examples/text_editor/text_editor.c"; OS_WriteFile(path, code); diff --git a/examples/use_as_data_format_with_typechecking/build.cpp b/examples/use_as_data_format_with_typechecking/build.cpp index 5aeebfa..0a5c200 100644 --- a/examples/use_as_data_format_with_typechecking/build.cpp +++ b/examples/use_as_data_format_with_typechecking/build.cpp @@ -8,8 +8,8 @@ bool use_as_data_format_with_typechecking() { LC_RegisterPackageDir("../examples/"); LC_RegisterPackageDir("../pkgs"); - LC_Intern name = LC_ILit("use_as_data_format_with_typechecking"); - LC_ASTRefList packages = LC_ResolvePackageByName(name); + LC_Intern name = LC_ILit("use_as_data_format_with_typechecking"); + LC_ParseAndResolve(name); if (L->errors) { LC_LangEnd(lang); return false; diff --git a/src/build_file/test_readme.cpp b/src/build_file/test_readme.cpp index 53e23f3..4729d54 100644 --- a/src/build_file/test_readme.cpp +++ b/src/build_file/test_readme.cpp @@ -30,8 +30,8 @@ void TestReadme() { LC_RegisterPackageDir("../pkgs"); LC_Intern name = LC_ILit("readme_test"); LC_AddSingleFilePackage(name, readme_path); - LC_ResolvePackageByName(name); - // LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls(); + LC_ParseAndResolve(name); + // LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(); } } } diff --git a/src/build_file/testsuite.cpp b/src/build_file/testsuite.cpp index edb2e2a..b3b9987 100644 --- a/src/build_file/testsuite.cpp +++ b/src/build_file/testsuite.cpp @@ -276,16 +276,16 @@ void RunTestFile(TestDesc it) { LC_AddSingleFilePackage(name, it.absolute_path); L->first_package = name; - LC_ParsePackagesUsingRegistry(name); + LC_ParsePackagesPass(name); LC_BuildIfPass(); if (L->errors) { result = Failed_Parse; goto end_of_test; } - LC_OrderAndResolveTopLevelDecls(name); - LC_ResolveAllProcBodies(); - LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls(); + LC_OrderAndResolveTopLevelPass(name); + LC_ResolveProcBodiesPass(); + LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(); if (L->errors) { result = Failed_Resolve; goto end_of_test; @@ -294,7 +294,7 @@ void RunTestFile(TestDesc it) { DebugVerifyAST(L->ordered_packages); OS_MakeDir(it.filename); - code = LC_GenerateUnityBuild(L->ordered_packages); + code = LC_GenerateUnityBuild(); out_path = S8_Format(L->arena, "%.*s/%.*s.c", S8_Expand(it.filename), S8_Expand(it.filename)); OS_WriteFile(out_path, code); @@ -333,15 +333,15 @@ void RunTestDir(TestDesc it, S8_String package_name) { LC_Intern first_package = LC_ILit(package_name.str); LC_RegisterPackageDir(it.absolute_path.str); LC_RegisterPackageDir("../../pkgs"); - LC_ASTRefList packages = LC_ResolvePackageByName(first_package); - LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls(); + LC_ParseAndResolve(first_package); + LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(); if (L->errors != 0) result = Failed_Package; if (result == OK && T->expected_result == OK) { - DebugVerifyAST(packages); + DebugVerifyAST(L->ordered_packages); OS_MakeDir(it.filename); - S8_String code = LC_GenerateUnityBuild(packages); + S8_String code = LC_GenerateUnityBuild(); S8_String out_path = S8_Format(L->arena, "%.*s/%.*s.c", S8_Expand(it.filename), S8_Expand(it.filename)); OS_WriteFile(out_path, code); if (!T->dont_run) result = Compile(it.filename, out_path); @@ -437,16 +437,16 @@ void RunTests() { lang->breakpoint_on_error = BreakpointOnError; LC_LangBegin(lang); LC_RegisterPackageDir(T->path); - LC_ASTRefList dll_packages = LC_ResolvePackageByName(LC_ILit("dll")); - LC_ASTRefList exe_packages = LC_ResolvePackageByName(LC_ILit("exe")); - result = L->errors >= 1 ? Failed_Package : OK; + LC_ParseAndResolve(LC_ILit("dll")); + LC_ParseAndResolve(LC_ILit("exe")); + result = L->errors >= 1 ? Failed_Package : OK; if (result == OK) { - DebugVerifyAST(dll_packages); + DebugVerifyAST(L->ordered_packages); // DebugVerifyAST(exe_packages); S8_String prev = PushDir("example_ui_and_hot_reloading"); - S8_String code = LC_GenerateUnityBuild(exe_packages); + LC_String code = LC_GenerateUnityBuild(); OS_WriteFile("example_ui_and_hot_reloading", code); OS_SetWorkingDir(prev); } diff --git a/src/compiler/extended_passes.c b/src/compiler/extended_passes.c index 5a63d22..e7466cd 100644 --- a/src/compiler/extended_passes.c +++ b/src/compiler/extended_passes.c @@ -29,7 +29,7 @@ LC_FUNCTION LC_Map LC_CountDeclRefs(LC_Arena *arena) { return map; } -LC_FUNCTION void LC_RemoveUnreferencedGlobalDecls(LC_Map *map_of_visits) { +LC_FUNCTION void LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits) { for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) { for (LC_Decl *decl = it->ast->apackage.ext->first_ordered; decl;) { intptr_t ref_count = (intptr_t)LC_MapGetP(map_of_visits, decl); @@ -43,7 +43,7 @@ LC_FUNCTION void LC_RemoveUnreferencedGlobalDecls(LC_Map *map_of_visits) { } } -LC_FUNCTION void LC_ErrorOnUnreferencedLocals(LC_Map *map_of_visits) { +LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits) { LC_Decl *first = (LC_Decl *)L->decl_arena->memory.data; for (int i = 0; i < L->decl_count; i += 1) { LC_Decl *decl = first + i; @@ -60,13 +60,13 @@ LC_FUNCTION void LC_ErrorOnUnreferencedLocals(LC_Map *map_of_visits) { } } -LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls(void) { +LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(void) { if (L->errors) return; LC_TempArena check = LC_BeginTemp(L->arena); LC_Map map = LC_CountDeclRefs(check.arena); - LC_ErrorOnUnreferencedLocals(&map); - LC_RemoveUnreferencedGlobalDecls(&map); + LC_ErrorOnUnreferencedLocalsPass(&map); + LC_RemoveUnreferencedGlobalDeclsPass(&map); LC_EndTemp(check); } \ No newline at end of file diff --git a/src/compiler/lex.c b/src/compiler/lex.c index 337a65e..6a7f81c 100644 --- a/src/compiler/lex.c +++ b/src/compiler/lex.c @@ -449,6 +449,7 @@ LC_FUNCTION LC_Lex *LC_LexStream(char *file, char *str, int line) { LC_LexNext(x, t); if (t->kind == LC_TokenKind_EOF) break; } + if (L->on_tokens_lexed) L->on_tokens_lexed(x); return x; } @@ -530,6 +531,7 @@ LC_FUNCTION void LC_InternTokens(LC_Lex *x) { } } } + if (L->on_tokens_interned) L->on_tokens_interned(x); } #undef LC_IF diff --git a/src/compiler/lib_compiler.h b/src/compiler/lib_compiler.h index 66a5572..ed213dc 100644 --- a/src/compiler/lib_compiler.h +++ b/src/compiler/lib_compiler.h @@ -966,7 +966,9 @@ struct LC_Lang { bool breakpoint_on_error; bool use_colored_terminal_output; - bool (*on_decl_parsed)(bool discarded, LC_AST *n); // returning 'true' from here indicates that declaration should be discarded + void (*on_tokens_lexed)(LC_Lex *x); + void (*on_tokens_interned)(LC_Lex *x); + void (*on_decl_parsed)(LC_AST *n); void (*on_expr_parsed)(LC_AST *n); void (*on_stmt_parsed)(LC_AST *n); void (*on_typespec_parsed)(LC_AST *n); @@ -982,7 +984,6 @@ struct LC_Lang { }; extern LC_THREAD_LOCAL LC_Lang *L; -extern LC_Operand LC_OPNull; // // Main @api @@ -992,23 +993,23 @@ LC_FUNCTION LC_Lang *LC_LangAlloc(void); // This allocates memory for LC_ LC_FUNCTION void LC_LangBegin(LC_Lang *l); // Prepare for compilation: init types, init builtins, set architecture variables stuff like that LC_FUNCTION void LC_LangEnd(LC_Lang *lang); // Deallocate language memory -LC_FUNCTION void LC_RegisterPackageDir(char *dir); // Add a package search directory -LC_FUNCTION LC_ASTRefList LC_ResolvePackageByName(LC_Intern name); // Fully resolve a package and all it's dependences -LC_FUNCTION LC_String LC_GenerateUnityBuild(LC_ASTRefList packages); // Generate the C program and return as a string +LC_FUNCTION void LC_RegisterPackageDir(char *dir); // Add a package search directory +LC_FUNCTION void LC_ParseAndResolve(LC_Intern name); // Fully resolve a package and all it's dependences +LC_FUNCTION LC_String LC_GenerateUnityBuild(void); // Generate the C program and return as a string // Smaller passes for AST modification -LC_FUNCTION void LC_ParsePackagesUsingRegistry(LC_Intern name); // These 3 functions are equivalent to LC_ResolvePackageByName, -LC_FUNCTION void LC_OrderAndResolveTopLevelDecls(LC_Intern name); // you can use them to hook into the compilation process - you can modify the AST -LC_FUNCTION void LC_ResolveAllProcBodies(void); // before resolving or use resolved top declarations to generate some code. - // The Parse and Order functions can be called multiple times to accommodate this. +LC_FUNCTION void LC_ParsePackagesPass(LC_Intern name); // These functions are equivalent to LC_ParseAndResolve, +LC_FUNCTION void LC_BuildIfPass(void); // you can use them to hook into the compilation process - you can modify the AST +LC_FUNCTION void LC_OrderAndResolveTopLevelPass(LC_Intern name); // before resolving or use resolved top declarations to generate some code. +LC_FUNCTION void LC_ResolveProcBodiesPass(void); // The Parse and Order functions can be called multiple times to accommodate this. // Extended pass / optimization -LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls(void); // Extended pass that you can execute once you have resolved all packages +LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(void); // Extended pass that you can execute once you have resolved all packages -// These three functions are used to implement LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls +// These three functions are used to implement LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass LC_FUNCTION LC_Map LC_CountDeclRefs(LC_Arena *arena); -LC_FUNCTION void LC_RemoveUnreferencedGlobalDecls(LC_Map *map_of_visits); -LC_FUNCTION void LC_ErrorOnUnreferencedLocals(LC_Map *map_of_visits); +LC_FUNCTION void LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits); +LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits); // Notes LC_FUNCTION void LC_DeclareNote(LC_Intern intern); @@ -1231,6 +1232,7 @@ LC_FUNCTION LC_Operand LC_ResolveTypeCast(LC_AST *pos, LC_Operan LC_FUNCTION LC_Operand LC_ResolveTypeVarDecl(LC_AST *pos, LC_Operand t, LC_Operand v); LC_FUNCTION LC_Operand LC_ResolveTypeAggregate(LC_AST *pos, LC_Type *type); +extern LC_Operand LC_OPNull; LC_FUNCTION LC_Operand LC_OPError(void); LC_FUNCTION LC_Operand LC_OPConstType(LC_Type *type); LC_FUNCTION LC_Operand LC_OPDecl(LC_Decl *decl); diff --git a/src/compiler/packages.c b/src/compiler/packages.c index 905bc8a..e2262a3 100644 --- a/src/compiler/packages.c +++ b/src/compiler/packages.c @@ -205,7 +205,7 @@ LC_FUNCTION void LC_ParsePackage(LC_AST *n) { } } -LC_FUNCTION void LC_ParsePackagesUsingRegistry(LC_Intern name) { +LC_FUNCTION void LC_ParsePackagesPass(LC_Intern name) { LC_AST *n = LC_GetPackageByName(name); if (!n) { LC_SendErrorMessagef(NULL, NULL, "no package with name '%s'\n", name); @@ -218,7 +218,7 @@ LC_FUNCTION void LC_ParsePackagesUsingRegistry(LC_Intern name) { LC_ParsePackage(n); LC_ASTRefList imports = LC_GetPackageImports(n); for (LC_ASTRef *it = imports.first; it; it = it->next) { - LC_ParsePackagesUsingRegistry(it->ast->gimport.path); + LC_ParsePackagesPass(it->ast->gimport.path); } } @@ -320,7 +320,7 @@ LC_FUNCTION LC_AST *LC_OrderPackagesAndBasicResolve(LC_AST *pos, LC_Intern name) return n; } -LC_FUNCTION void LC_OrderAndResolveTopLevelDecls(LC_Intern name) { +LC_FUNCTION void LC_OrderAndResolveTopLevelPass(LC_Intern name) { L->first_package = name; LC_OrderPackagesAndBasicResolve(NULL, name); @@ -334,7 +334,7 @@ LC_FUNCTION void LC_OrderAndResolveTopLevelDecls(LC_Intern name) { } } -LC_FUNCTION void LC_ResolveAllProcBodies(void) { +LC_FUNCTION void LC_ResolveProcBodiesPass(void) { // We don't need to check errors, only valid packages should have been put into // the list. for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) { @@ -344,19 +344,18 @@ LC_FUNCTION void LC_ResolveAllProcBodies(void) { } } -LC_FUNCTION LC_ASTRefList LC_ResolvePackageByName(LC_Intern name) { - LC_ParsePackagesUsingRegistry(name); +LC_FUNCTION void LC_ParseAndResolve(LC_Intern name) { + LC_ParsePackagesPass(name); LC_BuildIfPass(); - LC_ASTRefList empty = {0}; - if (L->errors) return empty; + if (L->errors) return; - LC_OrderAndResolveTopLevelDecls(name); - LC_ResolveAllProcBodies(); - return L->ordered_packages; + LC_OrderAndResolveTopLevelPass(name); + LC_ResolveProcBodiesPass(); } -LC_FUNCTION LC_String LC_GenerateUnityBuild(LC_ASTRefList packages) { +LC_FUNCTION LC_String LC_GenerateUnityBuild(void) { if (L->errors) return LC_MakeEmptyString(); + LC_ASTRefList packages = L->ordered_packages; LC_BeginStringGen(L->arena); diff --git a/src/compiler/parse.c b/src/compiler/parse.c index f1a5f7d..ac74cc0 100644 --- a/src/compiler/parse.c +++ b/src/compiler/parse.c @@ -987,8 +987,8 @@ LC_FUNCTION LC_AST *LC_ParseFileEx(LC_AST *package) { if (decl->kind == LC_ASTKind_Error) { LC_EatUntilNextValidDecl(); } else { - if (L->on_decl_parsed) L->on_decl_parsed(false, decl); LC_DLLAdd(n->afile.fdecl, n->afile.ldecl, decl); + if (L->on_decl_parsed) L->on_decl_parsed(decl); } } diff --git a/src/wasm_playground/wasm_main.c b/src/wasm_playground/wasm_main.c index 30829b1..4c899e5 100644 --- a/src/wasm_playground/wasm_main.c +++ b/src/wasm_playground/wasm_main.c @@ -115,7 +115,7 @@ void WASM_EXPORT(test)(void) { LC_Intern name = LC_ILit("file"); LC_AddSingleFilePackage(name, LC_Lit("file.lc")); - LC_ASTRefList packages = LC_ResolvePackageByName(name); + LC_ASTRefList packages = LC_ParseAndResolve(name); if (L->errors == 0) { LC_BeginStringGen(L->arena); diff --git a/tests/compilation/test_compile_packed.c b/tests/compilation/test_compile_packed.c index 8148b18..86b8f30 100644 --- a/tests/compilation/test_compile_packed.c +++ b/tests/compilation/test_compile_packed.c @@ -32,7 +32,7 @@ int main(int argc, char **argv) { } LC_Intern name = LC_InternStrLen(package.str, (int)package.len); - LC_ASTRefList packages = LC_ResolvePackageByName(name); + LC_ASTRefList packages = LC_ParseAndResolve(name); if (lang->errors) return 1; LC_String code = LC_GenerateUnityBuild(packages); diff --git a/tests/compilation/test_compile_packed_cpp.c b/tests/compilation/test_compile_packed_cpp.c index 8148b18..86b8f30 100644 --- a/tests/compilation/test_compile_packed_cpp.c +++ b/tests/compilation/test_compile_packed_cpp.c @@ -32,7 +32,7 @@ int main(int argc, char **argv) { } LC_Intern name = LC_InternStrLen(package.str, (int)package.len); - LC_ASTRefList packages = LC_ResolvePackageByName(name); + LC_ASTRefList packages = LC_ParseAndResolve(name); if (lang->errors) return 1; LC_String code = LC_GenerateUnityBuild(packages); diff --git a/tools/lcompile.c b/tools/lcompile.c index ae42c22..055d734 100644 --- a/tools/lcompile.c +++ b/tools/lcompile.c @@ -42,7 +42,7 @@ int main(int argc, char **argv) { S8_For(it, dirs) { LC_RegisterPackageDir(it->string.str); } LC_Intern name = LC_InternStrLen(package.str, (int)package.len); - LC_ASTRefList packages = LC_ResolvePackageByName(name); + LC_ASTRefList packages = LC_ParseAndResolve(name); if (lang->errors) return 1; S8_String code = LC_GenerateUnityBuild(packages);