From: Brendan Hansen Date: Wed, 22 Mar 2023 03:29:16 +0000 (-0500) Subject: added: basic structure documentation X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9d2638d0e559ffdfbf3c4ad962b9c360aed751a2;p=onyx.git added: basic structure documentation --- diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index 770e48f4..0a637908 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1610,6 +1610,7 @@ enum Runtime { typedef struct OnyxDocInfo { bh_arr(AstBinding *) procedures; + bh_arr(AstBinding *) structures; Table(u32) file_ids; u32 next_file_id; diff --git a/compiler/src/doc.c b/compiler/src/doc.c index 7b9d1512..e577f362 100644 --- a/compiler/src/doc.c +++ b/compiler/src/doc.c @@ -165,6 +165,10 @@ void onyx_docs_submit(OnyxDocInfo *docs, AstBinding *binding) { if (node->kind == Ast_Kind_Overloaded_Function) { bh_arr_push(docs->procedures, binding); } + + if (node->kind == Ast_Kind_Struct_Type) { + bh_arr_push(docs->structures, binding); + } } #define Doc_Magic_Bytes "ODOC" @@ -319,6 +323,14 @@ static void write_type_node(bh_buffer *buffer, void *vnode) { if (node) bh_buffer_write_string(buffer, onyx_ast_node_kind_string(node->kind)); } +static void write_doc_notes(bh_buffer *buffer, AstBinding *binding) { + if (!binding || !binding->documentation) { + write_cstring(buffer, ""); + } else { + write_string(buffer, binding->documentation->length, binding->documentation->text); + } +} + static void write_entity_header(bh_buffer *buffer, AstBinding *binding, OnyxFilePos location) { if (!binding) { bh_buffer_write_u32(buffer, 0); @@ -341,18 +353,13 @@ static void write_entity_header(bh_buffer *buffer, AstBinding *binding, OnyxFile // Location write_location(buffer, location); + + // Notes + write_doc_notes(buffer, binding); } static b32 write_doc_procedure(bh_buffer *buffer, AstBinding *binding, AstNode *proc); -static void write_doc_notes(bh_buffer *buffer, AstBinding *binding) { - if (!binding || !binding->documentation) { - write_cstring(buffer, ""); - } else { - write_string(buffer, binding->documentation->length, binding->documentation->text); - } -} - static b32 write_doc_function(bh_buffer *buffer, AstBinding *binding, AstNode *proc) { AstFunction *func = (void *) proc; if (func->kind == Ast_Kind_Macro) { @@ -361,9 +368,6 @@ static b32 write_doc_function(bh_buffer *buffer, AstBinding *binding, AstNode *p write_entity_header(buffer, binding, func->token->pos); - // Notes - write_doc_notes(buffer, binding); - // Flags bh_buffer_write_u32(buffer, proc->kind == Ast_Kind_Macro ? Doc_Procedure_Flag_Macro : 0); @@ -393,9 +397,6 @@ static b32 write_doc_overloaded_function(bh_buffer *buffer, AstBinding *binding, write_entity_header(buffer, binding, ofunc->token->pos); - // Notes - write_doc_notes(buffer, binding); - // Flags bh_buffer_write_u32(buffer, Doc_Procedure_Flag_Overloaded); @@ -428,9 +429,6 @@ static b32 write_doc_polymorphic_proc(bh_buffer *buffer, AstBinding *binding, As } write_entity_header(buffer, binding, func->token->pos); - - // Notes - write_doc_notes(buffer, binding); // Flags bh_buffer_write_u32(buffer, proc->kind == Ast_Kind_Macro ? Doc_Procedure_Flag_Macro : 0); @@ -489,6 +487,28 @@ static b32 write_doc_procedure(bh_buffer *buffer, AstBinding *binding, AstNode * return 0; } +static b32 write_doc_structure(bh_buffer *buffer, AstBinding *binding, AstNode *node) { + AstStructType *struct_node = (void *) node; + + write_entity_header(buffer, binding, node->token->pos); + + Type *struct_type = struct_node->stcache; + assert(struct_type); + + bh_buffer_write_u32(buffer, bh_arr_length(struct_type->Struct.memarr)); + bh_arr_each(StructMember*, pmem, struct_type->Struct.memarr) { + StructMember* mem = *pmem; + + write_cstring(buffer, mem->name); + write_cstring(buffer, type_get_name(mem->type)); + write_cstring(buffer, ""); + + bh_buffer_write_u32(buffer, 0); + } + + return 1; +} + void onyx_docs_emit_odoc(const char *dest) { bh_file doc_file; if (bh_file_create(&doc_file, dest) != BH_FILE_ERROR_NONE) { @@ -512,6 +532,7 @@ void onyx_docs_emit_odoc(const char *dest) { 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, 0); // // Package Info @@ -559,10 +580,29 @@ void onyx_docs_emit_odoc(const char *dest) { // - // File Info + // Structure Info // *((u32 *) bh_pointer_add(doc_buffer.data, offset_table_index + 8)) = doc_buffer.length; + u32 struct_count_patch = doc_buffer.length; + bh_buffer_write_u32(&doc_buffer, 0); + + u32 struct_count = 0; + bh_arr(AstBinding *) structs = context.doc_info->structures; + bh_arr_each(AstBinding *, pbind, structs) { + if (write_doc_structure(&doc_buffer, *pbind, (*pbind)->node)) { + struct_count++; + } + } + + *((u32 *) bh_pointer_add(doc_buffer.data, struct_count_patch)) = struct_count; + + + // + // File Info + // + *((u32 *) bh_pointer_add(doc_buffer.data, offset_table_index + 12)) = doc_buffer.length; + bh_buffer_write_u32(&doc_buffer, shlenu(context.doc_info->file_ids)); fori (i, 0, shlen(context.doc_info->file_ids)) { const char *key = context.doc_info->file_ids[i].key; diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index 333b7327..5c78f4ea 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -402,6 +402,7 @@ static void context_init(CompileOptions* opts) { context.doc_info = bh_alloc_item(global_heap_allocator, OnyxDocInfo); memset(context.doc_info, 0, sizeof(OnyxDocInfo)); bh_arr_new(global_heap_allocator, context.doc_info->procedures, 128); + bh_arr_new(global_heap_allocator, context.doc_info->structures, 128); } } diff --git a/core/doc/doc.onyx b/core/doc/doc.onyx index 21ff0419..c1304769 100644 --- a/core/doc/doc.onyx +++ b/core/doc/doc.onyx @@ -5,6 +5,7 @@ Doc :: struct { packages: [] Doc_Package; procedures: [] Doc_Procedure; + structures: [] Doc_Structure; files: [] Doc_File; } @@ -20,6 +21,7 @@ Doc_Header :: struct { packages_info_start: u32; procedures_info_start: u32; + structures_info_start: u32; files_info_start: u32; } @@ -47,13 +49,7 @@ Doc_Package :: struct { subpackages: [] Id; } -Doc_Visibility :: enum { - Public :: 1; - Package :: 2; - Local :: 3; -} - -Doc_Procedure :: struct { +Doc_Entity :: struct { name: str; visibility: Doc_Visibility; @@ -61,7 +57,17 @@ Doc_Procedure :: struct { location: Doc_Location; notes: str; +} + +Doc_Visibility :: enum { + Public :: 1; + Package :: 2; + Local :: 3; +} +Doc_Procedure :: struct { + use base: Doc_Entity; + Flags :: enum #flags (u32) { Is_Macro :: 1; Is_Foreign :: 2; @@ -82,3 +88,20 @@ Doc_Procedure_Param :: struct { default_value: str; } +Doc_Structure :: struct { + use base: Doc_Entity; + + members: [] Doc_Structure_Member; +} + +Doc_Structure_Member :: struct { + name: str; + type: str; // TODO: Make this not a string + default_value: str; + + Flags :: enum #flags { + Is_Used; + } + flags: Flags; +} +