added: started outputting procedures to doc file
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 16 Mar 2023 18:13:50 +0000 (13:13 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 24 Mar 2023 01:50:10 +0000 (20:50 -0500)
compiler/include/astnodes.h
compiler/src/checker.c
compiler/src/doc.c
compiler/src/onyx.c
compiler/src/utils.c
core/doc/doc.onyx
core/std.onyx
shared/include/bh.h

index 413f1a9246eb1ee0ea72e24a3a9a0b73e0c392e6..1ec48dd3bc1882f7408588216a69b4a807a9ff7f 100644 (file)
@@ -1564,6 +1564,7 @@ void emit_entity(Entity* ent);
 
 struct Package {
     char *name;
+    char *unqualified_name;
 
     Scope *scope;
     Scope *private_scope;
@@ -1601,6 +1602,11 @@ enum Runtime {
 };
 
 
+typedef struct OnyxDocInfo {
+    bh_arr(AstFunction *) procedures;
+} OnyxDocInfo;
+
+
 typedef struct CompileOptions CompileOptions;
 struct CompileOptions {
     bh_allocator allocator;
@@ -1658,6 +1664,7 @@ struct Context {
     bh_arr(AstNode *) tag_locations;
 
     struct SymbolInfoTable *symbol_info;
+    struct OnyxDocInfo     *doc_info;
 
     u32 cycle_almost_detected : 2;
     b32 cycle_detected : 1;
index 01b56267dcad39693907d1cd2c416d62a194bf40..19b19409009fae1ab7b617c9f01756a9b4890c39 100644 (file)
@@ -2924,6 +2924,11 @@ CheckStatus check_function_header(AstFunction* func) {
     func->type = type_build_function_type(context.ast_alloc, func);
     if (func->type == NULL) YIELD(func->token->pos, "Waiting for function type to be constructed");
 
+    // TODO nocheckin move this.
+    if (context.doc_info && func->entity_header && !func->generated_from) {
+        bh_arr_push(context.doc_info->procedures, func);
+    }
+
     return Check_Success;
 }
 
index 0665846f0e31e89af827f68c56c113a5f3371519..60867c3ea889e45e498f87debc276ca39de1c065 100644 (file)
@@ -194,22 +194,50 @@ void onyx_docs_emit_odoc(const char *dest) {
         // to serve as indicies into the array.
         bh_buffer_write_u32(&doc_buffer, p->id - 1);
 
-        write_cstring(&doc_buffer, p->name);
+        write_cstring(&doc_buffer, p->unqualified_name);
         write_cstring(&doc_buffer, package_qualified_name);
 
         bh_buffer_write_u32(&doc_buffer, bh_arr_length(p->sub_packages));
         fori (j, 0, bh_arr_length(p->sub_packages)) {
-            bh_buffer_write_u32(&doc_buffer, (u32) p->sub_packages[j]);
+            bh_buffer_write_u32(&doc_buffer, (u32) p->sub_packages[j] - 1);
         }
     }
 
     //
-    // Entity Info
+    // Procedure Info
     //
     *((u32 *) bh_pointer_add(doc_buffer.data, offset_table_index + 4)) = doc_buffer.length;
 
+    u32 proc_count_patch = doc_buffer.length;
     bh_buffer_write_u32(&doc_buffer, 0);
 
+    u32 proc_count = 0;
+    bh_arr(AstFunction *) procs = context.doc_info->procedures;
+    bh_arr_each(AstFunction *, pfunc, procs) {
+        AstFunction *func = *pfunc;
+
+        if (!func->intrinsic_name) continue;
+
+        write_string(&doc_buffer, func->intrinsic_name->length, func->intrinsic_name->text);
+        assert(func->entity_header && func->entity_header->package);
+
+        bh_buffer_write_u32(&doc_buffer, func->entity_header->package->id - 1);
+
+        // TODO: Location
+        bh_buffer_write_u32(&doc_buffer, 0);
+        bh_buffer_write_u32(&doc_buffer, 0);
+        bh_buffer_write_u32(&doc_buffer, 0);
+
+        write_cstring(&doc_buffer, "");
+
+        // TODO: Flags
+        bh_buffer_write_u32(&doc_buffer, 0);
+
+        proc_count++;
+    }
+
+    *((u32 *) bh_pointer_add(doc_buffer.data, proc_count_patch)) = proc_count;
+
 
     //
     // File Info
index 7362c7e368e90f5c7ee1a7337041b0728942ad52..d2197c7db96572d1daad03e55d3796b08bccfe23 100644 (file)
@@ -397,6 +397,11 @@ static void context_init(CompileOptions* opts) {
         bh_arr_new(global_heap_allocator, context.symbol_info->symbols_resolutions, 128);
         sh_new_arena(context.symbol_info->files);
     }
+
+    if (context.options->documentation_file) {
+        context.doc_info = bh_alloc_item(global_heap_allocator, OnyxDocInfo);
+        bh_arr_new(global_heap_allocator, context.doc_info->procedures, 128);
+    }
 }
 
 static void context_free() {
index c7d7033e6117cc5818c68ccb4f3b900ee32031b4..c91aec6c422863c0b20946c9edcdbe71e3c8f670 100644 (file)
@@ -42,6 +42,7 @@ Package* package_lookup_or_create(char* package_name, Scope* parent_scope, bh_al
         pac_name[strlen(package_name)] = '\0';
 
         package->name = pac_name;
+        package->unqualified_name = pac_name + bh_str_last_index_of(pac_name, '.');
         package->use_package_entities = NULL;
         package->id = next_package_id++;
         bh_arr_new(global_heap_allocator, package->sub_packages, 4);
index b78bfef3d4039631e31f660ec061148ace280ac6..be9a9f54b9599244337aabe5ae092664968cd96b 100644 (file)
@@ -3,9 +3,9 @@ package core.doc
 Doc :: struct {
     header: Doc_Header;
 
-    packages: [] Doc_Package;
-    entities: [] Doc_Entity;
-    files:    [] Doc_File;
+    packages:   [] Doc_Package;
+    procedures: [] Doc_Procedure;
+    files:      [] Doc_File;
 }
 
 
@@ -19,7 +19,7 @@ Doc_Header :: struct {
     build_time: u32;
 
     packages_info_start: u32;
-    entities_info_start: u32;
+    procedures_info_start: u32;
     files_info_start: u32;
 }
 
@@ -46,18 +46,18 @@ Doc_Package :: struct {
     subpackages: [] Id;
 }
 
-
-Doc_Entity_Kind :: enum {
-    Procedure :: 1;
-}
-
-Doc_Entity :: struct {
-    kind: Doc_Entity_Kind;
+Doc_Procedure :: struct {
+    name: str;
 
     package_id: Doc_Package.Id;
     location: Doc_Location;
 
-    name: str;
     notes: str;
+
+    Flags :: enum #flags (u32) {
+        Is_Macro   :: 1;
+        Is_Foreign :: 2;
+    }
+    flags: Flags;
 }
 
index d33b78ad66f57182d6973cfff641c9c8b4d0811f..198367948bbc1de7a72ed4b49b1328e21f604da9 100644 (file)
@@ -56,6 +56,8 @@ package core
 
 #load "./runtime/common"
 
+#load "./doc/doc"
+
 #if runtime.platform.Supports_Files {
     #load "./os/file"
 }
index bad259a275c72a74445ef0f98cc0f7be451c231d..a519f5a67025e48894e83392b5011cf317a6d684 100644 (file)
@@ -335,6 +335,7 @@ BH_ALLOCATOR_PROC(bh_scratch_allocator_proc);
 b32 bh_str_starts_with(char* str, char* start);
 b32 bh_str_ends_with(char* str, char* end);
 b32 bh_str_contains(char *str, char *needle);
+u32 bh_str_last_index_of(char *str, char needle);
 char* bh_strdup(bh_allocator a, char* str);
 
 
@@ -1492,6 +1493,21 @@ b32 bh_str_contains(char *str, char *needle) {
     return 0;
 }
 
+u32 bh_str_last_index_of(char *str, char needle) {
+    u32 count = strlen(str);
+    char *end = str + count - 1;
+
+    while (end != str) {
+        if (*end == needle) break;
+        count -= 1;
+        end--;
+    }
+
+    if (end == str) count = 0;
+
+    return count;
+}
+
 char* bh_strdup(bh_allocator a, char* str) {
     u32 len = strlen(str);
     char* buf = bh_alloc(a, len + 1);