From: Brendan Hansen Date: Fri, 7 Apr 2023 16:03:04 +0000 (-0500) Subject: added: `#allow_stale_code` directive; marked uncommon packages X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9193915cdcd3a0e7d4b8778520b122db038c7f44;p=onyx.git added: `#allow_stale_code` directive; marked uncommon packages --- diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index 44b7c1cb..7bf74752 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1620,6 +1620,12 @@ struct Package { // the code base. This is used when a static if clears and new symbols are introduced. // 'use package' statements have to be reevaluated to pull in the new symbols. bh_arr(Entity *) use_package_entities; + + // NOTE: These are entities that are stored in packages marked with `#allow_stale_code`. + // These entities are flushed to the entity heap when the package has been explicit used + // somewhere. + bh_arr(Entity *) buffered_entities; + b32 is_included_somewhere : 1; }; typedef enum CompileAction CompileAction; @@ -1670,6 +1676,7 @@ struct CompileOptions { b32 use_multi_threading : 1; b32 generate_foreign_info : 1; b32 no_std : 1; + b32 no_stale_code : 1; b32 generate_tag_file : 1; b32 generate_symbol_info_file : 1; diff --git a/compiler/include/utils.h b/compiler/include/utils.h index c35cbf06..36885992 100644 --- a/compiler/include/utils.h +++ b/compiler/include/utils.h @@ -14,6 +14,7 @@ Package* package_lookup(char* package_name); Package* package_lookup_or_create(char* package_name, Scope* parent_scope, bh_allocator alloc, OnyxFilePos pos); void package_track_use_package(Package* package, Entity* entity); void package_reinsert_use_packages(Package* package); +void package_mark_as_used(Package* package); Scope* scope_create(bh_allocator a, Scope* parent, OnyxFilePos created_at); void scope_include(Scope* target, Scope* source, OnyxFilePos pos); diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index 5a715d7c..c5755bca 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -60,6 +60,7 @@ static const char *build_docstring = DOCSTRING_HEADER "\t--doc Generates an O-DOC file, a.k.a an Onyx documentation file. Used by onyx-doc-gen.\n" "\t--tag Generates a C-Tag file.\n" "\t--syminfo Generates a symbol resolution information file. Used by onyx-lsp.\n" + "\t--no-stale-code Disables use of `#allow_stale_code` directive\n" "\t--generate-foreign-info\n" "\n" "Developer options:\n" @@ -83,6 +84,7 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg .use_post_mvp_features = 1, .use_multi_threading = 0, .no_std = 0, + .no_stale_code = 0, .runtime = Runtime_Onyx, @@ -191,6 +193,9 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg else if (!strcmp(argv[i], "--no-std")) { options.no_std = 1; } + else if (!strcmp(argv[i], "--no-stale-code")) { + options.no_stale_code = 1; + } else if (!strcmp(argv[i], "-I")) { bh_arr_push(options.included_folders, argv[++i]); } diff --git a/compiler/src/parser.c b/compiler/src/parser.c index 11c36a2e..3230c510 100644 --- a/compiler/src/parser.c +++ b/compiler/src/parser.c @@ -3784,6 +3784,10 @@ OnyxParser onyx_parser_create(bh_allocator alloc, OnyxTokenizer *tokenizer) { } void onyx_parser_free(OnyxParser* parser) { + bh_arr_free(parser->alternate_entity_placement_stack); + bh_arr_free(parser->current_symbol_stack); + bh_arr_free(parser->scope_flags); + bh_arr_free(parser->stored_tags); } void onyx_parse(OnyxParser *parser) { @@ -3794,6 +3798,13 @@ void onyx_parse(OnyxParser *parser) { parser->file_scope = scope_create(parser->allocator, parser->package->private_scope, parser->tokenizer->tokens[0].pos); parser->current_scope = parser->file_scope; + if (parse_possible_directive(parser, "allow_stale_code") + && !parser->package->is_included_somewhere + && !context.options->no_stale_code) { + bh_arr_new(global_heap_allocator, parser->package->buffered_entities, 32); + bh_arr_push(parser->alternate_entity_placement_stack, &parser->package->buffered_entities); + } + parse_top_level_statements_until(parser, Token_Type_End_Stream); parser->current_scope = parser->current_scope->parent; diff --git a/compiler/src/symres.c b/compiler/src/symres.c index 73dae651..492ff2d0 100644 --- a/compiler/src/symres.c +++ b/compiler/src/symres.c @@ -1121,6 +1121,7 @@ static SymresStatus symres_package(AstPackage* package) { } if (package->package) { + package_mark_as_used(package->package); return Symres_Success; } else { if (report_unresolved_symbols) { diff --git a/compiler/src/utils.c b/compiler/src/utils.c index 2cbb09f3..cbc8fff4 100644 --- a/compiler/src/utils.c +++ b/compiler/src/utils.c @@ -97,6 +97,19 @@ void package_reinsert_use_packages(Package* package) { bh_arr_set_length(package->use_package_entities, 0); } +void package_mark_as_used(Package* package) { + if (!package) return; + if (package->is_included_somewhere) return; + package->is_included_somewhere = 1; + + bh_arr_each(Entity *, pent, package->buffered_entities) { + entity_heap_insert_existing(&context.entities, *pent); + } + + bh_arr_clear(package->buffered_entities); +} + + // // Scoping diff --git a/core/container/avl_tree.onyx b/core/container/avl_tree.onyx index f9513684..a8e0d8c4 100644 --- a/core/container/avl_tree.onyx +++ b/core/container/avl_tree.onyx @@ -1,4 +1,5 @@ package core.avl_tree +#allow_stale_code use core use core.math @@ -108,4 +109,4 @@ print :: (tree: &AVL_Tree) { A.height = math.max(get_height(A.left), get_height(A.right)) + 1; B.height = math.max(get_height(B.left), get_height(B.right)) + 1; -} \ No newline at end of file +} diff --git a/core/container/bucket_array.onyx b/core/container/bucket_array.onyx index f50d1545..e7eadb86 100644 --- a/core/container/bucket_array.onyx +++ b/core/container/bucket_array.onyx @@ -1,6 +1,7 @@ // @Incomplete // This implementation is not functional at all but is something I want to get around to adding. package core.bucket_array +#allow_stale_code use core use core.array diff --git a/core/container/list.onyx b/core/container/list.onyx index 444e59ae..639725cb 100644 --- a/core/container/list.onyx +++ b/core/container/list.onyx @@ -1,4 +1,5 @@ package core.list +#allow_stale_code use core diff --git a/core/doc/doc.onyx b/core/doc/doc.onyx index 2b18a683..8c64e9e7 100644 --- a/core/doc/doc.onyx +++ b/core/doc/doc.onyx @@ -1,4 +1,5 @@ package core.doc +#allow_stale_code Doc :: struct { header: Doc_Header; diff --git a/core/encoding/base64.onyx b/core/encoding/base64.onyx index 1f3fa0e6..d35635a3 100644 --- a/core/encoding/base64.onyx +++ b/core/encoding/base64.onyx @@ -1,4 +1,5 @@ package core.encoding.base64 +#allow_stale_code use core.array diff --git a/core/encoding/csv.onyx b/core/encoding/csv.onyx index 25b4b724..915f1578 100644 --- a/core/encoding/csv.onyx +++ b/core/encoding/csv.onyx @@ -1,4 +1,5 @@ package core.encoding.csv +#allow_stale_code // // A simple CSV parsing and encoding library. diff --git a/core/encoding/ini.onyx b/core/encoding/ini.onyx index fed04d1b..ab935ee7 100644 --- a/core/encoding/ini.onyx +++ b/core/encoding/ini.onyx @@ -1,4 +1,5 @@ package core.encoding.ini +#allow_stale_code // // This library allows for parsing and formatting a simple 'ini' like diff --git a/core/encoding/osad.onyx b/core/encoding/osad.onyx index 7b368ec0..6fa05f59 100644 --- a/core/encoding/osad.onyx +++ b/core/encoding/osad.onyx @@ -1,4 +1,5 @@ package core.encoding.osad +#allow_stale_code use core.io use core.string diff --git a/core/encoding/utf8.onyx b/core/encoding/utf8.onyx index 73a1e2e5..3c1117d2 100644 --- a/core/encoding/utf8.onyx +++ b/core/encoding/utf8.onyx @@ -1,4 +1,5 @@ package core.encoding.utf8 +#allow_stale_code use core.string use core.array diff --git a/core/hash/md5.onyx b/core/hash/md5.onyx index ab4984e3..3255bfe7 100644 --- a/core/hash/md5.onyx +++ b/core/hash/md5.onyx @@ -1,4 +1,5 @@ package core.hash.md5 +#allow_stale_code use core {io, memory, conv} use core.intrinsics.wasm {rotl_i32} diff --git a/core/hash/sha256.onyx b/core/hash/sha256.onyx index 3c0e29ba..30fa0967 100644 --- a/core/hash/sha256.onyx +++ b/core/hash/sha256.onyx @@ -1,4 +1,5 @@ package core.hash.sha256 +#allow_stale_code use core {memory} use core.intrinsics.wasm {wasm :: package} @@ -167,4 +168,4 @@ k := u32.[ 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 -]; \ No newline at end of file +]; diff --git a/core/io/binary_reader.onyx b/core/io/binary_reader.onyx index 34afcedc..915eb932 100644 --- a/core/io/binary_reader.onyx +++ b/core/io/binary_reader.onyx @@ -1,4 +1,5 @@ package core.io.binary +#allow_stale_code // NOTE: This is a very experimental and not well put together package. // The primary reason it exists is because I was working on reading TTF diff --git a/core/test/testing.onyx b/core/test/testing.onyx index 9ee93686..24c93dc5 100644 --- a/core/test/testing.onyx +++ b/core/test/testing.onyx @@ -1,4 +1,5 @@ package core.test +#allow_stale_code use core use core.array