| ... | ... | @@ -12,6 +12,7 @@ |
| 12 | 12 | #include "os.hpp" |
| 13 | 13 | #include "parseh.hpp" |
| 14 | 14 | #include "config.h" |
| 15 | #include "ast_render.hpp" |
| 15 | 16 | |
| 16 | 17 | static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, BlockContext *context, |
| 17 | 18 | TypeTableEntry *expected_type, AstNode *node); |
| ... | ... | @@ -26,6 +27,7 @@ static TypeTableEntry *analyze_error_literal_expr(CodeGen *g, ImportTableEntry * |
| 26 | 27 | static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context, |
| 27 | 28 | TypeTableEntry *expected_type, AstNode *node); |
| 28 | 29 | static TypeTableEntry *resolve_expr_const_val_as_void(CodeGen *g, AstNode *node); |
| 30 | static void detect_top_level_decl_deps(CodeGen *g, ImportTableEntry *import, AstNode *node); |
| 29 | 31 | |
| 30 | 32 | static AstNode *first_executing_node(AstNode *node) { |
| 31 | 33 | switch (node->type) { |
| ... | ... | @@ -87,6 +89,8 @@ static AstNode *first_executing_node(AstNode *node) { |
| 87 | 89 | } |
| 88 | 90 | |
| 89 | 91 | ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg) { |
| 92 | assert(!node->owner->c_import_node); |
| 93 | |
| 90 | 94 | ErrorMsg *err = err_msg_create_with_line(node->owner->path, node->line, node->column, |
| 91 | 95 | node->owner->source_code, node->owner->line_offsets, msg); |
| 92 | 96 | |
| ... | ... | @@ -1056,11 +1060,15 @@ static void resolve_c_import_decl(CodeGen *g, ImportTableEntry *parent_import, A |
| 1056 | 1060 | |
| 1057 | 1061 | find_libc_path(g); |
| 1058 | 1062 | |
| 1059 | | ImportTableEntry child_import = {0}; |
| 1063 | ImportTableEntry *child_import = allocate<ImportTableEntry>(1); |
| 1064 | child_import->fn_table.init(32); |
| 1065 | child_import->fn_type_table.init(32); |
| 1066 | child_import->c_import_node = node; |
| 1067 | |
| 1060 | 1068 | ZigList<ErrorMsg *> errors = {0}; |
| 1061 | 1069 | |
| 1062 | 1070 | int err; |
| 1063 | | if ((err = parse_h_buf(&child_import, &errors, child_context->c_import_buf, g->clang_argv, g->clang_argv_len, |
| 1071 | if ((err = parse_h_buf(child_import, &errors, child_context->c_import_buf, g->clang_argv, g->clang_argv_len, |
| 1064 | 1072 | buf_ptr(g->libc_include_path)))) |
| 1065 | 1073 | { |
| 1066 | 1074 | zig_panic("unable to parse h file: %s\n", err_str(err)); |
| ... | ... | @@ -1075,7 +1083,24 @@ static void resolve_c_import_decl(CodeGen *g, ImportTableEntry *parent_import, A |
| 1075 | 1083 | return; |
| 1076 | 1084 | } |
| 1077 | 1085 | |
| 1078 | | zig_panic("TODO integrate the AST"); |
| 1086 | if (g->verbose) { |
| 1087 | fprintf(stderr, "\nc_import:\n"); |
| 1088 | fprintf(stderr, "-----------\n"); |
| 1089 | ast_render(stderr, child_import->root, 4); |
| 1090 | } |
| 1091 | |
| 1092 | child_import->di_file = parent_import->di_file; |
| 1093 | child_import->block_context = new_block_context(child_import->root, nullptr); |
| 1094 | child_import->importers.append({parent_import, node}); |
| 1095 | |
| 1096 | detect_top_level_decl_deps(g, child_import, child_import->root); |
| 1097 | } |
| 1098 | |
| 1099 | static void satisfy_dep(CodeGen *g, AstNode *node) { |
| 1100 | Buf *name = get_resolved_top_level_decl(node)->name; |
| 1101 | if (name) { |
| 1102 | g->unresolved_top_level_decls.maybe_remove(name); |
| 1103 | } |
| 1079 | 1104 | } |
| 1080 | 1105 | |
| 1081 | 1106 | static void resolve_top_level_decl(CodeGen *g, ImportTableEntry *import, AstNode *node) { |
| ... | ... | @@ -1085,7 +1110,7 @@ static void resolve_top_level_decl(CodeGen *g, ImportTableEntry *import, AstNode |
| 1085 | 1110 | break; |
| 1086 | 1111 | case NodeTypeRootExportDecl: |
| 1087 | 1112 | // handled earlier |
| 1088 | | break; |
| 1113 | return; |
| 1089 | 1114 | case NodeTypeStructDecl: |
| 1090 | 1115 | { |
| 1091 | 1116 | TypeTableEntry *type_entry = node->data.struct_decl.type_entry; |
| ... | ... | @@ -1115,7 +1140,7 @@ static void resolve_top_level_decl(CodeGen *g, ImportTableEntry *import, AstNode |
| 1115 | 1140 | break; |
| 1116 | 1141 | case NodeTypeImport: |
| 1117 | 1142 | // nothing to do here |
| 1118 | | break; |
| 1143 | return; |
| 1119 | 1144 | case NodeTypeCImport: |
| 1120 | 1145 | resolve_c_import_decl(g, import, node); |
| 1121 | 1146 | break; |
| ... | ... | @@ -1159,6 +1184,9 @@ static void resolve_top_level_decl(CodeGen *g, ImportTableEntry *import, AstNode |
| 1159 | 1184 | case NodeTypeErrorType: |
| 1160 | 1185 | zig_unreachable(); |
| 1161 | 1186 | } |
| 1187 | |
| 1188 | |
| 1189 | satisfy_dep(g, node); |
| 1162 | 1190 | } |
| 1163 | 1191 | |
| 1164 | 1192 | static FnTableEntry *get_context_fn_entry(BlockContext *context) { |
| ... | ... | @@ -4512,6 +4540,12 @@ static TypeTableEntryId container_to_type(ContainerKind kind) { |
| 4512 | 4540 | |
| 4513 | 4541 | static void detect_top_level_decl_deps(CodeGen *g, ImportTableEntry *import, AstNode *node) { |
| 4514 | 4542 | switch (node->type) { |
| 4543 | case NodeTypeRoot: |
| 4544 | for (int i = 0; i < import->root->data.root.top_level_decls.length; i += 1) { |
| 4545 | AstNode *child = import->root->data.root.top_level_decls.at(i); |
| 4546 | detect_top_level_decl_deps(g, import, child); |
| 4547 | } |
| 4548 | break; |
| 4515 | 4549 | case NodeTypeStructDecl: |
| 4516 | 4550 | { |
| 4517 | 4551 | Buf *name = &node->data.struct_decl.name; |
| ... | ... | @@ -4664,7 +4698,6 @@ static void detect_top_level_decl_deps(CodeGen *g, ImportTableEntry *import, Ast |
| 4664 | 4698 | case NodeTypeParamDecl: |
| 4665 | 4699 | case NodeTypeFnDecl: |
| 4666 | 4700 | case NodeTypeReturnExpr: |
| 4667 | | case NodeTypeRoot: |
| 4668 | 4701 | case NodeTypeBlock: |
| 4669 | 4702 | case NodeTypeBinOpExpr: |
| 4670 | 4703 | case NodeTypeUnwrapErrorExpr: |
| ... | ... | @@ -4732,7 +4765,6 @@ static void recursive_resolve_decl(CodeGen *g, ImportTableEntry *import, AstNode |
| 4732 | 4765 | } |
| 4733 | 4766 | |
| 4734 | 4767 | resolve_top_level_decl(g, import, node); |
| 4735 | | g->unresolved_top_level_decls.remove(get_resolved_top_level_decl(node)->name); |
| 4736 | 4768 | } |
| 4737 | 4769 | |
| 4738 | 4770 | static void resolve_top_level_declarations_root(CodeGen *g, ImportTableEntry *import, AstNode *node) { |
| ... | ... | @@ -4823,10 +4855,7 @@ void semantic_analyze(CodeGen *g) { |
| 4823 | 4855 | |
| 4824 | 4856 | ImportTableEntry *import = entry->value; |
| 4825 | 4857 | |
| 4826 | | for (int i = 0; i < import->root->data.root.top_level_decls.length; i += 1) { |
| 4827 | | AstNode *child = import->root->data.root.top_level_decls.at(i); |
| 4828 | | detect_top_level_decl_deps(g, import, child); |
| 4829 | | } |
| 4858 | detect_top_level_decl_deps(g, import, import->root); |
| 4830 | 4859 | } |
| 4831 | 4860 | } |
| 4832 | 4861 | |