added: `#allow_stale_code` directive; marked uncommon packages
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 7 Apr 2023 16:03:04 +0000 (11:03 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 7 Apr 2023 16:03:04 +0000 (11:03 -0500)
19 files changed:
compiler/include/astnodes.h
compiler/include/utils.h
compiler/src/onyx.c
compiler/src/parser.c
compiler/src/symres.c
compiler/src/utils.c
core/container/avl_tree.onyx
core/container/bucket_array.onyx
core/container/list.onyx
core/doc/doc.onyx
core/encoding/base64.onyx
core/encoding/csv.onyx
core/encoding/ini.onyx
core/encoding/osad.onyx
core/encoding/utf8.onyx
core/hash/md5.onyx
core/hash/sha256.onyx
core/io/binary_reader.onyx
core/test/testing.onyx

index 44b7c1cb6bd39f7a1dd1ea04ec4973249fe1975b..7bf74752b7ebe884dbbe52fbfc5604743ba469a8 100644 (file)
@@ -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;
index c35cbf06f05639393d8a08742638617d538c28c9..36885992182cc05e396c03610bdf57776b867c45 100644 (file)
@@ -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);
index 5a715d7c07d63e6c39afa1a07d19316a1fefdec7..c5755bca3c6886f96b29358cd89279120b706546 100644 (file)
@@ -60,6 +60,7 @@ static const char *build_docstring = DOCSTRING_HEADER
     "\t--doc <doc_file>        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 <target_file> 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]);
             }
index 11c36a2eea3cbe337a25b36f6a468ec60b2a9ef5..3230c510cb053fe5fee2161a5c543372408c52dc 100644 (file)
@@ -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;
index 73dae651c11e27f86eb9d2db4b4ab0c67ea7782c..492ff2d01f75bbc8809512b38e91e6c6a262777e 100644 (file)
@@ -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) {
index 2cbb09f36d0fb45df39adead912c86859e800708..cbc8fff4e42bbaa548f1ad0d2930b908d91bdfa3 100644 (file)
@@ -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
index f95136849f75923fda67f8f36c5d55295e82eee8..a8e0d8c4519fe84d6607368bb25b73d53cff88ce 100644 (file)
@@ -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
+}
index f50d154571be314080636893a89b471fb55c8070..e7eadb86ec8b6d67ea7fc9b5718357392953fde6 100644 (file)
@@ -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
index 444e59aec7ac4b259fde0c24cb7da6e077602d9e..639725cbe4d4158a7b8ad044ad4b391dd57eb0ba 100644 (file)
@@ -1,4 +1,5 @@
 package core.list
+#allow_stale_code
 
 use core
 
index 2b18a683313724f52f09384c9e3c16fdee8bf8ed..8c64e9e7f38d00702cf563a08379a81bc39ee386 100644 (file)
@@ -1,4 +1,5 @@
 package core.doc
+#allow_stale_code
 
 Doc :: struct {
     header: Doc_Header;
index 1f3fa0e664f9973312cb3f962c49efece91e457d..d35635a38c8d9bfc05925a7f6253975f8e0d824f 100644 (file)
@@ -1,4 +1,5 @@
 package core.encoding.base64
+#allow_stale_code
 
 use core.array
 
index 25b4b7247fd55f46d1ac5e60e6c4fe2bd355daac..915f1578a87baf128c036197aa1f6bb0312ae1e9 100644 (file)
@@ -1,4 +1,5 @@
 package core.encoding.csv
+#allow_stale_code
 
 //
 // A simple CSV parsing and encoding library.
index fed04d1b22b4fd6aa6c1eb1cc83b14844ca906a4..ab935ee78cc94c14103031edf70a8e7a7186e157 100644 (file)
@@ -1,4 +1,5 @@
 package core.encoding.ini
+#allow_stale_code
 
 //
 // This library allows for parsing and formatting a simple 'ini' like
index 7b368ec03d36e3d8a29adb99529dd30d0a5dabb6..6fa05f590c96e804f6fdf8c2d9cbe2fd3b200554 100644 (file)
@@ -1,4 +1,5 @@
 package core.encoding.osad
+#allow_stale_code
 
 use core.io
 use core.string
index 73a1e2e5dad50df1791c1d8982cada9fec8132cf..3c1117d238ccfd7025adfe7b05f3c8286dffb808 100644 (file)
@@ -1,4 +1,5 @@
 package core.encoding.utf8
+#allow_stale_code
 
 use core.string
 use core.array
index ab4984e3fd069d2949b60ab59750350eafbcc9ff..3255bfe72d4fd0c4524727a09d5d8739b189e173 100644 (file)
@@ -1,4 +1,5 @@
 package core.hash.md5
+#allow_stale_code
 
 use core {io, memory, conv}
 use core.intrinsics.wasm {rotl_i32}
index 3c0e29ba59a3a63b06c2b30d70e4df019d2f355d..30fa09678f18b1b442398c54a3b0fd589528b023 100644 (file)
@@ -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
+];
index 34afcedc5e153b5d21184203d1de49493ff5fd8c..915eb9329f6373aeffddda85ba5afa4e2abc2c3d 100644 (file)
@@ -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
index 9ee93686bbd66ad103342274e7d69d50191b9c06..24c93dc5e375f2c5f97e18da5256705693152fa2 100644 (file)
@@ -1,4 +1,5 @@
 package core.test
+#allow_stale_code
 
 use core
 use core.array