From 099953cf7dc933c6b2b6553069ffea3726f7f817 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 16 Mar 2023 13:13:50 -0500 Subject: [PATCH] added: started outputting procedures to doc file --- compiler/include/astnodes.h | 7 +++++++ compiler/src/checker.c | 5 +++++ compiler/src/doc.c | 34 +++++++++++++++++++++++++++++++--- compiler/src/onyx.c | 5 +++++ compiler/src/utils.c | 1 + core/doc/doc.onyx | 24 ++++++++++++------------ core/std.onyx | 2 ++ shared/include/bh.h | 16 ++++++++++++++++ 8 files changed, 79 insertions(+), 15 deletions(-) diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index 413f1a92..1ec48dd3 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -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; diff --git a/compiler/src/checker.c b/compiler/src/checker.c index 01b56267..19b19409 100644 --- a/compiler/src/checker.c +++ b/compiler/src/checker.c @@ -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; } diff --git a/compiler/src/doc.c b/compiler/src/doc.c index 0665846f..60867c3e 100644 --- a/compiler/src/doc.c +++ b/compiler/src/doc.c @@ -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 diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index 7362c7e3..d2197c7d 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -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() { diff --git a/compiler/src/utils.c b/compiler/src/utils.c index c7d7033e..c91aec6c 100644 --- a/compiler/src/utils.c +++ b/compiler/src/utils.c @@ -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); diff --git a/core/doc/doc.onyx b/core/doc/doc.onyx index b78bfef3..be9a9f54 100644 --- a/core/doc/doc.onyx +++ b/core/doc/doc.onyx @@ -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; } diff --git a/core/std.onyx b/core/std.onyx index d33b78ad..19836794 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -56,6 +56,8 @@ package core #load "./runtime/common" +#load "./doc/doc" + #if runtime.platform.Supports_Files { #load "./os/file" } diff --git a/shared/include/bh.h b/shared/include/bh.h index bad259a2..a519f5a6 100644 --- a/shared/include/bh.h +++ b/shared/include/bh.h @@ -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); -- 2.25.1