authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-30 22:53:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-30 22:53:37-07:00
logcd68969115f19b09aeb72923426e722725ba9451
tree291d34566589752492f1d3ac9b63aa78e72dcd39
parent55b8472374eede496b59396dbe253b05b16063e1

closer to multiple files working


8 files changed, 70 insertions(+), 21 deletions(-)

example/multiple_files/foo.zig+7-1
......@@ -1,5 +1,11 @@
11use "libc.zig";
22
3fn print_text() {
3// purposefully conflicting function with main.zig
4// but it's private so it should be OK
5fn private_function() {
46 puts("it works!");
57}
8
9fn print_text() {
10 private_function();
11}
example/multiple_files/main.zig+4
......@@ -4,6 +4,10 @@ use "libc.zig";
44use "foo.zig";
55
66fn _start() -> unreachable {
7 private_function();
8}
9
10fn private_function() -> unreachable {
711 print_text();
812 exit(0);
913}
src/analyze.cpp+20-5
......@@ -9,6 +9,7 @@
99#include "semantic_info.hpp"
1010#include "error.hpp"
1111#include "zig_llvm.hpp"
12#include "os.hpp"
1213
1314static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
1415 g->errors.add_one();
......@@ -212,7 +213,7 @@ static void preview_function_declarations(CodeGen *g, ImportTableEntry *import,
212213 }
213214 break;
214215 case NodeTypeUse:
215 zig_panic("TODO use");
216 // nothing to do here
216217 break;
217218 case NodeTypeDirective:
218219 case NodeTypeParamDecl:
......@@ -383,9 +384,16 @@ static void analyze_top_level_declaration(CodeGen *g, AstNode *node) {
383384
384385 case NodeTypeRootExportDecl:
385386 case NodeTypeExternBlock:
386 case NodeTypeUse:
387387 // already looked at these in the preview pass
388388 break;
389 case NodeTypeUse:
390 for (int i = 0; i < node->data.use.directives->length; i += 1) {
391 AstNode *directive_node = node->data.use.directives->at(i);
392 Buf *name = &directive_node->data.directive.name;
393 add_node_error(g, directive_node,
394 buf_sprintf("invalid directive: '%s'", buf_ptr(name)));
395 }
396 break;
389397 case NodeTypeDirective:
390398 case NodeTypeParamDecl:
391399 case NodeTypeFnProto:
......@@ -429,7 +437,14 @@ static void analyze_root(CodeGen *g, ImportTableEntry *import, AstNode *node) {
429437 }
430438}
431439
432void semantic_analyze(CodeGen *g, ImportTableEntry *import_table_entry) {
433 analyze_root(g, import_table_entry, import_table_entry->root);
434}
440void semantic_analyze(CodeGen *g) {
441 auto it = g->import_table.entry_iterator();
442 for (;;) {
443 auto *entry = it.next();
444 if (!entry)
445 break;
435446
447 ImportTableEntry *import = entry->value;
448 analyze_root(g, import, import->root);
449 }
450}
src/analyze.hpp+1-2
......@@ -9,8 +9,7 @@
99#define ZIG_ANALYZE_HPP
1010
1111struct CodeGen;
12struct ImportTableEntry;
1312
14void semantic_analyze(CodeGen *g, ImportTableEntry *entry);
13void semantic_analyze(CodeGen *g);
1514
1615#endif
src/codegen.cpp+31-10
......@@ -661,12 +661,7 @@ static void init(CodeGen *g, Buf *source_path) {
661661
662662}
663663
664void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
665 if (!g->initialized) {
666 g->initialized = true;
667 init(g, source_path);
668 }
669
664static void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
670665 Buf full_path = BUF_INIT;
671666 os_path_join(g->root_source_dir, source_path, &full_path);
672667
......@@ -693,20 +688,46 @@ void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
693688 }
694689
695690 ImportTableEntry *import_entry = allocate<ImportTableEntry>(1);
691 import_entry->fn_table.init(32);
696692 import_entry->root = ast_parse(source_code, tokens);
697693 assert(import_entry->root);
698694 if (g->verbose) {
699695 ast_print(import_entry->root, 0);
700
701 fprintf(stderr, "\nSemantic Analysis:\n");
702 fprintf(stderr, "--------------------\n");
703696 }
704697
705698 import_entry->path = source_path;
706699 import_entry->di_file = LLVMZigCreateFile(g->dbuilder, buf_ptr(&basename), buf_ptr(&dirname));
707700 g->import_table.put(source_path, import_entry);
708701
709 semantic_analyze(g, import_entry);
702
703 assert(import_entry->root->type == NodeTypeRoot);
704 for (int decl_i = 0; decl_i < import_entry->root->data.root.top_level_decls.length; decl_i += 1) {
705 AstNode *top_level_decl = import_entry->root->data.root.top_level_decls.at(decl_i);
706 if (top_level_decl->type != NodeTypeUse)
707 continue;
708
709 auto entry = g->import_table.maybe_get(&top_level_decl->data.use.path);
710 if (!entry) {
711 Buf full_path = BUF_INIT;
712 os_path_join(g->root_source_dir, &top_level_decl->data.use.path, &full_path);
713 Buf import_code = BUF_INIT;
714 os_fetch_file_path(&full_path, &import_code);
715 codegen_add_code(g, &top_level_decl->data.use.path, &import_code);
716 }
717 }
718}
719
720void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) {
721 init(g, source_path);
722
723 codegen_add_code(g, source_path, source_code);
724
725
726 if (g->verbose) {
727 fprintf(stderr, "\nSemantic Analysis:\n");
728 fprintf(stderr, "--------------------\n");
729 }
730 semantic_analyze(g);
710731
711732 if (g->errors.length == 0) {
712733 if (g->verbose) {
src/codegen.hpp+1-1
......@@ -42,7 +42,7 @@ void codegen_set_verbose(CodeGen *codegen, bool verbose);
4242void codegen_set_out_type(CodeGen *codegen, OutType out_type);
4343void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
4444
45void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code);
45void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code);
4646
4747void codegen_link(CodeGen *g, const char *out_file);
4848
src/main.cpp+1-1
......@@ -73,7 +73,7 @@ static int build(const char *arg0, Build *b) {
7373 if (b->out_name)
7474 codegen_set_out_name(g, buf_create_from_str(b->out_name));
7575 codegen_set_verbose(g, b->verbose);
76 codegen_add_code(g, &root_source_name, &root_source_code);
76 codegen_add_root_code(g, &root_source_name, &root_source_code);
7777 codegen_link(g, b->out_file);
7878
7979 return 0;
src/semantic_info.hpp+5-1
......@@ -12,6 +12,8 @@
1212#include "hash_map.hpp"
1313#include "zig_llvm.hpp"
1414
15struct FnTableEntry;
16
1517struct TypeTableEntry {
1618 LLVMTypeRef type_ref;
1719 LLVMZigDIType *di_type;
......@@ -28,6 +30,9 @@ struct ImportTableEntry {
2830 AstNode *root;
2931 Buf *path; // relative to root_source_dir
3032 LLVMZigDIFile *di_file;
33
34 // reminder: hash tables must be initialized before use
35 HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table;
3136};
3237
3338struct FnTableEntry {
......@@ -81,7 +86,6 @@ struct CodeGen {
8186 int version_minor;
8287 int version_patch;
8388 bool verbose;
84 bool initialized;
8589};
8690
8791struct TypeNode {