added: outputting constraints in odoc files
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 8 Apr 2023 18:51:24 +0000 (13:51 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 8 Apr 2023 18:51:24 +0000 (13:51 -0500)
compiler/src/doc.c
core/doc/doc.onyx

index 5e52c7f9f056b24d684920f73995b1da408a7ec7..ec5066db254f622f824947803942c38ade1f82ed 100644 (file)
@@ -397,6 +397,60 @@ static void write_entity_header(bh_buffer *buffer, AstBinding *binding, OnyxFile
     write_doc_notes(buffer, binding);
 }
 
+static void write_doc_constraints(bh_buffer *buffer, ConstraintContext *constraints, bh_arr(AstPolyParam) poly_params) {
+    bh_buffer tmp_buffer;
+    bh_buffer_init(&tmp_buffer, global_scratch_allocator, 256);
+
+    u32 constraint_count_patch = buffer->length;
+    bh_buffer_write_u32(buffer, 0);
+
+    u32 constraint_count = 0;
+    bh_arr_each(AstConstraint *, pconstraint, constraints->constraints) {
+        AstConstraint *constraint = *pconstraint;
+
+        bh_buffer_clear(&tmp_buffer);
+        write_type_node(&tmp_buffer, constraint->interface);
+        bh_buffer_write_string(&tmp_buffer, "(");
+
+        bh_arr_each(AstType *, ptype_arg, constraint->type_args) {
+            if (ptype_arg != constraint->type_args) {
+                bh_buffer_write_string(&tmp_buffer, ", ");
+            }
+
+            AstType *type_arg = *ptype_arg;
+            write_type_node(&tmp_buffer, type_arg);
+        }
+
+        bh_buffer_write_string(&tmp_buffer, ")");
+        write_string(buffer, tmp_buffer.length, tmp_buffer.data);
+
+        constraint_count += 1;
+    }
+
+    if (poly_params) {
+        bh_arr_each(AstPolyParam, poly_param, poly_params) {
+            if (!poly_param->implicit_interface) continue;
+
+            bh_buffer_clear(&tmp_buffer);
+            write_type_node(&tmp_buffer, poly_param->implicit_interface);
+            bh_buffer_write_string(&tmp_buffer, "(");
+
+            poly_param->poly_sym->flags &= ~Ast_Flag_Symbol_Is_PolyVar;
+            write_type_node(&tmp_buffer, poly_param->poly_sym);
+            poly_param->poly_sym->flags |= Ast_Flag_Symbol_Is_PolyVar;
+
+            bh_buffer_write_string(&tmp_buffer, ")");
+            write_string(buffer, tmp_buffer.length, tmp_buffer.data);
+
+            constraint_count += 1;
+        }
+    }
+
+    *((u32 *) bh_pointer_add(buffer->data, constraint_count_patch)) = constraint_count;
+
+    bh_buffer_free(&tmp_buffer);
+}
+
 static b32 write_doc_procedure(bh_buffer *buffer, AstBinding *binding, AstNode *proc);
 
 static b32 write_doc_function(bh_buffer *buffer, AstBinding *binding, AstNode *proc) {
@@ -424,6 +478,9 @@ static b32 write_doc_function(bh_buffer *buffer, AstBinding *binding, AstNode *p
     // Overload procs
     bh_buffer_write_u32(buffer, 0);
 
+    // Constraints
+    bh_buffer_write_u32(buffer, 0);
+
     return 1;
 }
 
@@ -457,6 +514,9 @@ static b32 write_doc_overloaded_function(bh_buffer *buffer, AstBinding *binding,
 
     *((u32 *) bh_pointer_add(buffer->data, proc_count_patch)) = proc_count;
 
+    // Constraints
+    bh_buffer_write_u32(buffer, 0);
+
     bh_imap_free(&all_overloads);
     return 1;
 }
@@ -501,6 +561,8 @@ static b32 write_doc_polymorphic_proc(bh_buffer *buffer, AstBinding *binding, As
     // Overload procs
     bh_buffer_write_u32(buffer, 0);
 
+    // Constraints
+    write_doc_constraints(buffer, &func->constraints, func->poly_params);
     return 1;
 }
 
@@ -551,6 +613,9 @@ static b32 write_doc_structure(bh_buffer *buffer, AstBinding *binding, AstNode *
 
         // Polymorph parameters
         bh_buffer_write_u32(buffer, 0);
+
+        // Constraints
+        write_doc_constraints(buffer, &struct_node->constraints, NULL);
     }
     else if (node->kind == Ast_Kind_Poly_Struct_Type) {
         AstPolyStructType *poly_struct_node = (void *) node;
@@ -588,6 +653,9 @@ static b32 write_doc_structure(bh_buffer *buffer, AstBinding *binding, AstNode *
             write_cstring(buffer, "");
         }
 
+        // Constraints
+        write_doc_constraints(buffer, &struct_node->constraints, NULL);
+
         bh_buffer_free(&type_buf);
     }
 
index 8c64e9e7f38d00702cf563a08379a81bc39ee386..ea6e8aae95077b27542f303eb7c60daa72bc14fc 100644 (file)
@@ -83,6 +83,8 @@ Doc_Procedure :: struct {
 
     // Only used if this is an overloaded procedure
     overloads: [] Doc_Procedure;
+
+    constraints: [] str;
 }
 
 Doc_Param :: struct {
@@ -97,6 +99,8 @@ Doc_Structure :: struct {
     members: [] Doc_Structure_Member;
     polymorph_arguments: [] Doc_Param;
 
+    constraints: [] str;
+
     methods: [] Doc_Procedure;
 }