From: Brendan Hansen Date: Wed, 24 May 2023 13:43:02 +0000 (-0500) Subject: added: doc info for tagged unions X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9dcf9d893c264ca6b620bb6a0fc562b79b543259;p=onyx.git added: doc info for tagged unions --- diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index b96266a5..1cbbccf4 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1745,6 +1745,7 @@ typedef struct OnyxDocInfo { bh_arr(AstBinding *) structures; bh_arr(AstBinding *) enumerations; bh_arr(AstBinding *) distinct_types; + bh_arr(AstBinding *) unions; Table(u32) file_ids; u32 next_file_id; diff --git a/compiler/src/doc.c b/compiler/src/doc.c index 742c1c59..1328bbc2 100644 --- a/compiler/src/doc.c +++ b/compiler/src/doc.c @@ -204,6 +204,14 @@ void onyx_docs_submit(OnyxDocInfo *docs, AstBinding *binding) { if (node->kind == Ast_Kind_Distinct_Type) { bh_arr_push(docs->distinct_types, binding); } + + if (node->kind == Ast_Kind_Union_Type) { + bh_arr_push(docs->unions, binding); + } + + if (node->kind == Ast_Kind_Poly_Union_Type) { + bh_arr_push(docs->unions, binding); + } } #define Doc_Magic_Bytes "ODOC" @@ -711,6 +719,76 @@ static b32 write_doc_structure(bh_buffer *buffer, AstBinding *binding, AstNode * return 1; } +static b32 write_doc_union_type(bh_buffer *buffer, AstBinding *binding, AstNode *node) { + Scope *method_scope = NULL; + + if (node->kind == Ast_Kind_Union_Type) { + AstUnionType *union_node = (void *) node; + method_scope = get_scope_from_node((AstNode *) union_node); + + write_entity_header(buffer, binding, node->token->pos); + + Type *union_type = union_node->utcache; + assert(union_type); + + bh_buffer_write_u32(buffer, bh_arr_length(union_type->Union.variants_ordered)); + bh_arr_each(UnionVariant*, puv, union_type->Union.variants_ordered) { + UnionVariant* uv = *puv; + + write_cstring(buffer, uv->name); + write_cstring(buffer, type_get_name(uv->type)); + } + + // Polymorph parameters + bh_buffer_write_u32(buffer, 0); + + // Constraints + write_doc_constraints(buffer, &union_node->constraints, NULL); + } + else if (node->kind == Ast_Kind_Poly_Union_Type) { + AstPolyUnionType *poly_union_node = (void *) node; + method_scope = get_scope_from_node((AstNode *) poly_union_node); + + AstUnionType *union_node = poly_union_node->base_union; + + write_entity_header(buffer, binding, node->token->pos); + + bh_buffer type_buf; + bh_buffer_init(&type_buf, global_scratch_allocator, 256); + + bh_buffer_write_u32(buffer, bh_arr_length(union_node->variants)); + bh_arr_each(AstUnionVariant*, puv, union_node->variants) { + AstUnionVariant* uv = *puv; + + bh_buffer_clear(&type_buf); + write_type_node(&type_buf, uv->type_node); + + write_string(buffer, uv->token->length, uv->token->text); + write_string(buffer, type_buf.length, type_buf.data); + } + + // Polymorph parameters + bh_buffer_write_u32(buffer, bh_arr_length(poly_union_node->poly_params)); + bh_arr_each(AstPolyStructParam, param, poly_union_node->poly_params) { + bh_buffer_clear(&type_buf); + write_type_node(&type_buf, param->type_node); + + write_string(buffer, param->token->length, param->token->text); + write_string(buffer, type_buf.length, type_buf.data); + write_cstring(buffer, ""); + } + + // Constraints + write_doc_constraints(buffer, &union_node->constraints, NULL); + + bh_buffer_free(&type_buf); + } + + write_doc_methods(buffer, method_scope); + + return 1; +} + static b32 write_doc_enum(bh_buffer *buffer, AstBinding *binding, AstNode *node) { AstEnumType *enum_node = (void *) node; @@ -792,6 +870,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 @@ -843,10 +922,16 @@ void onyx_docs_emit_odoc(const char *dest) { write_doc_entity_array(&doc_buffer, context.doc_info->distinct_types, write_doc_distinct_type, offset_table_index + 16); + // + // Union Info + // + write_doc_entity_array(&doc_buffer, context.doc_info->unions, write_doc_union_type, offset_table_index + 20); + + // // File Info // - *((u32 *) bh_pointer_add(doc_buffer.data, offset_table_index + 20)) = doc_buffer.length; + *((u32 *) bh_pointer_add(doc_buffer.data, offset_table_index + 24)) = doc_buffer.length; bh_buffer_write_u32(&doc_buffer, shlenu(context.doc_info->file_ids)); fori (i, 0, shlen(context.doc_info->file_ids)) { diff --git a/core/doc/doc.onyx b/core/doc/doc.onyx index 44ebe7a4..8335e5a0 100644 --- a/core/doc/doc.onyx +++ b/core/doc/doc.onyx @@ -9,6 +9,7 @@ Doc :: struct { structures: [] Doc_Structure; enumerations: [] Doc_Enum; distinct_types: [] Doc_Distinct_Type; + unions: [] Doc_Union; files: [] Doc_File; } @@ -27,6 +28,7 @@ Doc_Header :: struct { structures_info_start: u32; enumerations_info_start: u32; distinct_types_info_start: u32; + union_types_info_start: u32; files_info_start: u32; } @@ -141,3 +143,18 @@ Doc_Distinct_Type :: struct { methods: [] Doc_Procedure; } +Doc_Union :: struct { + use base: Doc_Entity; + + variants: [] Doc_Union_Variant; + polymorph_arguments: [] Doc_Param; + + constraints: [] str; + + methods: [] Doc_Procedure; +} + +Doc_Union_Variant :: struct { + name: str; + type: str; // TODO: Make this not a string +}