added: doc info for tagged unions
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 24 May 2023 13:43:02 +0000 (08:43 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 24 May 2023 13:43:02 +0000 (08:43 -0500)
compiler/include/astnodes.h
compiler/src/doc.c
core/doc/doc.onyx

index b96266a5de0ea916270534a3fdc01594b9dfc07d..1cbbccf4e2f6b1a0e0d9fc4e10e3467eb06d9f49 100644 (file)
@@ -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;
index 742c1c59367656c1915316e203a13f94144631a5..1328bbc20ef0162ae87d9284dcc046ba0b7a7820 100644 (file)
@@ -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)) {
index 44ebe7a48120df2fd095c1d6bff56b48a300dc29..8335e5a0ab264ae49952b7273cbb932216f76cbd 100644 (file)
@@ -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
+}