still working on the bugfix
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 5 Feb 2023 05:15:45 +0000 (23:15 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 5 Feb 2023 05:15:45 +0000 (23:15 -0600)
compiler/src/types.c

index a510549b0d4783daa706260f51f49ed7000705e8..2e816dc75a467d3d4cbd71c6d1a5e707bcf0dc77 100644 (file)
@@ -258,13 +258,13 @@ u32 type_alignment_of(Type* type) {
 
 // If this function returns NULL, then the caller MUST yield because the type may still be constructed in the future.
 // If there was an error constructing the type, then this function will report that directly.
-Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
+Type* type_build_from_ast_inner(bh_allocator alloc, AstType* type_node, b32 accept_partial_types) {
     if (type_node == NULL) return NULL;
 
     switch (type_node->kind) {
         case Ast_Kind_Pointer_Type: {
-            // ((AstPointerType *) type_node)->elem->flags |= type_node->flags & Ast_Flag_Header_Check_No_Error;
-            Type* ptr_type = type_make_pointer(alloc, type_build_from_ast(alloc, ((AstPointerType *) type_node)->elem));
+            Type *inner_type = type_build_from_ast_inner(alloc, ((AstPointerType *) type_node)->elem, 1);
+            Type *ptr_type = type_make_pointer(alloc, inner_type);
             if (ptr_type) ptr_type->ast_type = type_node;
             return ptr_type;
         }
@@ -273,7 +273,7 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
             AstFunctionType* ftype_node = (AstFunctionType *) type_node;
             u64 param_count = ftype_node->param_count;
 
-            Type* return_type = type_build_from_ast(alloc, ftype_node->return_type);
+            Type* return_type = type_build_from_ast_inner(alloc, ftype_node->return_type, 1);
             if (return_type == NULL) return NULL;
 
             Type* func_type = type_create(Type_Kind_Function, alloc, param_count);
@@ -285,7 +285,7 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
 
             if (param_count > 0) {
                 fori (i, 0, (i64) param_count) {
-                    func_type->Function.params[i] = type_build_from_ast(alloc, ftype_node->params[i]);
+                    func_type->Function.params[i] = type_build_from_ast_inner(alloc, ftype_node->params[i], 1);
 
                     // LEAK LEAK LEAK
                     if (func_type->Function.params[i] == NULL) return NULL;
@@ -313,13 +313,13 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
         case Ast_Kind_Array_Type: {
             AstArrayType* a_node = (AstArrayType *) type_node;
 
-            Type *elem_type = type_build_from_ast(alloc, a_node->elem);
+            Type *elem_type = type_build_from_ast_inner(alloc, a_node->elem, 1);
             if (elem_type == NULL)  return NULL;
 
             u32 count = 0;
             if (a_node->count_expr) {
                 if (a_node->count_expr->type == NULL)
-                    a_node->count_expr->type = type_build_from_ast(alloc, a_node->count_expr->type_node);
+                    a_node->count_expr->type = type_build_from_ast_inner(alloc, a_node->count_expr->type_node, 1);
 
                 if (node_is_auto_cast((AstNode *) a_node->count_expr)) {
                     a_node->count_expr = ((AstUnaryOp *) a_node)->expr;
@@ -389,13 +389,13 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
 
                 if ((*member)->type == NULL) {
                     s_node->pending_type_is_valid = 0;
-                    return NULL;
+                    return accept_partial_types ? s_node->pending_type : NULL;
                 }
 
                 if ((*member)->type->kind == Type_Kind_Struct
                     && (*member)->type->Struct.status == SPS_Start) {
                     s_node->pending_type_is_valid = 0;
-                    return NULL;
+                    return accept_partial_types ? s_node->pending_type : NULL;
                 }
 
                 mem_alignment = type_alignment_of((*member)->type);
@@ -478,19 +478,19 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
         }
 
         case Ast_Kind_Slice_Type: {
-            Type* slice_type = type_make_slice(alloc, type_build_from_ast(alloc, ((AstSliceType *) type_node)->elem));
+            Type* slice_type = type_make_slice(alloc, type_build_from_ast_inner(alloc, ((AstSliceType *) type_node)->elem, 1));
             if (slice_type) slice_type->ast_type = type_node;
             return slice_type;
         }
 
         case Ast_Kind_DynArr_Type: {
-            Type* dynarr_type = type_make_dynarray(alloc, type_build_from_ast(alloc, ((AstDynArrType *) type_node)->elem));
+            Type* dynarr_type = type_make_dynarray(alloc, type_build_from_ast_inner(alloc, ((AstDynArrType *) type_node)->elem, 1));
             if (dynarr_type) dynarr_type->ast_type = type_node;
             return dynarr_type;
         }
 
         case Ast_Kind_VarArg_Type: {
-            Type* va_type = type_make_varargs(alloc, type_build_from_ast(alloc, ((AstVarArgType *) type_node)->elem));
+            Type* va_type = type_make_varargs(alloc, type_build_from_ast_inner(alloc, ((AstVarArgType *) type_node)->elem, 1));
             if (va_type) va_type->ast_type = type_node;
             return va_type;
         }
@@ -500,7 +500,7 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
         }
 
         case Ast_Kind_Type_Alias: {
-            Type* type = type_build_from_ast(alloc, ((AstTypeAlias *) type_node)->to);
+            Type* type = type_build_from_ast_inner(alloc, ((AstTypeAlias *) type_node)->to, 1);
             if (type && type->ast_type) type_node->type_id = type->id;
             return type;
         }
@@ -539,7 +539,7 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
             bh_arr_new(global_heap_allocator, slns, bh_arr_length(pc_type->params));
             bh_arr_each(AstNode *, given, pc_type->params) {
                 if (node_is_type(*given)) {
-                    Type* param_type = type_build_from_ast(alloc, (AstType *) *given);
+                    Type* param_type = type_build_from_ast_inner(alloc, (AstType *) *given, 1);
 
                     // LEAK LEAK LEAK
                     if (param_type == NULL) return NULL;
@@ -579,7 +579,7 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
 
             fori (i, 0, type_count) {
                 assert(ctype->types[i] != NULL);
-                comp_type->Compound.types[i] = type_build_from_ast(alloc, ctype->types[i]);
+                comp_type->Compound.types[i] = type_build_from_ast_inner(alloc, ctype->types[i], 1);
 
                 // LEAK LEAK LEAK
                 if (comp_type->Compound.types[i] == NULL) return NULL;
@@ -636,6 +636,10 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
     return NULL;
 }
 
+Type *type_build_from_ast(bh_allocator alloc, AstType* type_node) {
+    return type_build_from_ast_inner(alloc, type_node, 0);
+}
+
 // CLEANUP: This needs to be merged with the very similar code from up above.
 Type* type_build_function_type(bh_allocator alloc, AstFunction* func) {
     u64 param_count = bh_arr_length(func->params);