added: simple procedure documenting
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 16 Mar 2023 20:46:26 +0000 (15:46 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 24 Mar 2023 01:50:10 +0000 (20:50 -0500)
compiler/include/astnodes.h
compiler/include/doc.h
compiler/src/checker.c
compiler/src/doc.c
compiler/src/symres.c
core/doc/doc.onyx

index 1ec48dd3bc1882f7408588216a69b4a807a9ff7f..0e104b896c0fbd95d731a17a4e06714faf9e69ed 100644 (file)
@@ -1603,7 +1603,7 @@ enum Runtime {
 
 
 typedef struct OnyxDocInfo {
-    bh_arr(AstFunction *) procedures;
+    bh_arr(AstBinding *) procedures;
 } OnyxDocInfo;
 
 
index 93a8348d9ceffda815bcaefa358f4a89bf666d39..45214503c275faabeda0c6831fe48c25681873f1 100644 (file)
@@ -6,6 +6,7 @@
 
 // Onyx Documentation generation
 
+void onyx_docs_submit(OnyxDocInfo *docs, AstBinding *binding);
 void onyx_docs_emit_odoc(const char *dest);
 
 
index 19b19409009fae1ab7b617c9f01756a9b4890c39..01b56267dcad39693907d1cd2c416d62a194bf40 100644 (file)
@@ -2924,11 +2924,6 @@ 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 60867c3ea889e45e498f87debc276ca39de1c065..0ddf09c1bdd5a5b5ac7a292cb1feb75d9719768a 100644 (file)
@@ -142,6 +142,18 @@ void onyx_docs_emit_symbol_info(const char *dest) {
 // Onyx Documentation Format
 //
 
+void onyx_docs_submit(OnyxDocInfo *docs, AstBinding *binding) {
+    AstNode *node = binding->node;
+    if (node->kind == Ast_Kind_Function) {
+        AstFunction *func = (void *) node;
+        if (!func->generated_from && func->intrinsic_name
+            && binding->entity && binding->entity->package) {
+
+            bh_arr_push(docs->procedures, binding);
+        }
+    }
+}
+
 #define Doc_Magic_Bytes "ODOC"
 
 static void write_cstring(bh_buffer *buffer, const char *data) {
@@ -179,6 +191,9 @@ void onyx_docs_emit_odoc(const char *dest) {
     bh_buffer_write_u32(&doc_buffer, 0);
     bh_buffer_write_u32(&doc_buffer, 0);
 
+    Table(u32) file_ids = NULL;
+    u32 next_file_id = 0;
+
     //
     // Package Info
     // 
@@ -212,27 +227,48 @@ void onyx_docs_emit_odoc(const char *dest) {
     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;
-
+    bh_arr(AstBinding *) procs = context.doc_info->procedures;
+    bh_arr_each(AstBinding *, pbind, procs) {
+        AstFunction *func = (void *) (*pbind)->node;
         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);
+        // TODO: Add polymorphic procedure support
+        if (func->kind != Ast_Kind_Function) continue;
 
-        bh_buffer_write_u32(&doc_buffer, func->entity_header->package->id - 1);
+        write_string(&doc_buffer, (*pbind)->token->length, (*pbind)->token->text);
+        // assert(func->entity_header && func->entity_header->package);
+        
+        u32 visibility = 1;
+        if ((*pbind)->flags & Ast_Flag_Private_Package) visibility = 2;
+        if ((*pbind)->flags & Ast_Flag_Private_File)    visibility = 3;
+        bh_buffer_write_u32(&doc_buffer, visibility);
 
-        // TODO: Location
-        bh_buffer_write_u32(&doc_buffer, 0);
-        bh_buffer_write_u32(&doc_buffer, 0);
-        bh_buffer_write_u32(&doc_buffer, 0);
+        bh_buffer_write_u32(&doc_buffer, (*pbind)->entity->package->id - 1);
+
+        OnyxFilePos pos = (*pbind)->token->pos;
+        if (shgeti(file_ids, pos.filename) == -1) {
+            shput(file_ids, pos.filename, next_file_id);
+            next_file_id++;
+        }
+
+        bh_buffer_write_u32(&doc_buffer, file_ids[shgeti(file_ids, pos.filename)].value);
+        bh_buffer_write_u32(&doc_buffer, pos.line);
+        bh_buffer_write_u32(&doc_buffer, pos.column);
 
         write_cstring(&doc_buffer, "");
 
         // TODO: Flags
         bh_buffer_write_u32(&doc_buffer, 0);
 
+        bh_buffer_write_u32(&doc_buffer, bh_arr_length(func->params));
+        bh_arr_each(AstParam, param, func->params) {
+            write_string(&doc_buffer, param->local->token->length, param->local->token->text);
+            write_cstring(&doc_buffer, type_get_name(param->local->type));
+            write_cstring(&doc_buffer, "");
+        }
+
+        write_cstring(&doc_buffer, type_get_name(type_build_from_ast(context.ast_alloc, func->return_type)));
+
         proc_count++;
     }
 
@@ -244,7 +280,13 @@ void onyx_docs_emit_odoc(const char *dest) {
     //
     *((u32 *) bh_pointer_add(doc_buffer.data, offset_table_index + 8)) = doc_buffer.length;
 
-    bh_buffer_write_u32(&doc_buffer, 0);
+    bh_buffer_write_u32(&doc_buffer, shlenu(file_ids));
+    fori (i, 0, shlen(file_ids)) {
+        const char *key = file_ids[i].key;
+        
+        bh_buffer_write_u32(&doc_buffer, 0);
+        write_cstring(&doc_buffer, key);
+    }
 
 
     bh_file_write(&doc_file, doc_buffer.data, doc_buffer.length);
index fb889375ead465d33f8dab5f283c4ef0d937d0c1..a5972dd9af0bd4d2ec60b53a923f070e9b9b47af 100644 (file)
@@ -3,6 +3,7 @@
 #include "utils.h"
 #include "astnodes.h"
 #include "errors.h"
+#include "doc.h"
 
 // :EliminatingSymres - notes the places where too much work is being done in symbol resolution
 
@@ -1782,6 +1783,10 @@ void symres_entity(Entity* ent) {
             symbol_introduce(current_scope, ent->binding->token, ent->binding->node);
             track_declaration_for_tags((AstNode *) ent->binding);
 
+            if (context.doc_info) {
+                onyx_docs_submit(context.doc_info, ent->binding);
+            }
+
             package_reinsert_use_packages(ent->package);
             next_state = Entity_State_Finalized;
             break;
index be9a9f54b9599244337aabe5ae092664968cd96b..d21a5057059de8c0b76bae09880928cd860b0f6d 100644 (file)
@@ -46,9 +46,16 @@ Doc_Package :: struct {
     subpackages: [] Id;
 }
 
+Doc_Visibility :: enum {
+    Public  :: 1;
+    Package :: 2;
+    Local   :: 3;
+}
+
 Doc_Procedure :: struct {
     name: str;
 
+    visibility: Doc_Visibility;
     package_id: Doc_Package.Id;
     location: Doc_Location;
 
@@ -59,5 +66,14 @@ Doc_Procedure :: struct {
         Is_Foreign :: 2;
     }
     flags: Flags;
+
+    params: [] Doc_Procedure_Param;
+    return_type: str;   // TODO: Make this not a string
+}
+
+Doc_Procedure_Param :: struct {
+    name: str;
+    type: str;          // TODO: Make this not a string
+    default_value: str;
 }