authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-11-30 22:54:00-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-11-30 22:54:00-07:00
log9278dbedd5242bf4253da29c0fab36eea9dda61b
tree4bf997d327984a4772d7031de26949bc1b6e91c7
parent18cfcfe44f4b4d624f20508210e2928396e3cf10
parentcd68969115f19b09aeb72923426e722725ba9451

Merge remote-tracking branch 'origin/master' into type-checking


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
1314struct BlockContext {
1415 AstNode *node;
......@@ -218,7 +219,7 @@ static void preview_function_declarations(CodeGen *g, ImportTableEntry *import,
218219 }
219220 break;
220221 case NodeTypeUse:
221 zig_panic("TODO use");
222 // nothing to do here
222223 break;
223224 case NodeTypeDirective:
224225 case NodeTypeParamDecl:
......@@ -545,9 +546,16 @@ static void analyze_top_level_declaration(CodeGen *g, AstNode *node) {
545546
546547 case NodeTypeRootExportDecl:
547548 case NodeTypeExternBlock:
548 case NodeTypeUse:
549549 // already looked at these in the preview pass
550550 break;
551 case NodeTypeUse:
552 for (int i = 0; i < node->data.use.directives->length; i += 1) {
553 AstNode *directive_node = node->data.use.directives->at(i);
554 Buf *name = &directive_node->data.directive.name;
555 add_node_error(g, directive_node,
556 buf_sprintf("invalid directive: '%s'", buf_ptr(name)));
557 }
558 break;
551559 case NodeTypeDirective:
552560 case NodeTypeParamDecl:
553561 case NodeTypeFnProto:
......@@ -591,7 +599,14 @@ static void analyze_root(CodeGen *g, ImportTableEntry *import, AstNode *node) {
591599 }
592600}
593601
594void semantic_analyze(CodeGen *g, ImportTableEntry *import_table_entry) {
595 analyze_root(g, import_table_entry, import_table_entry->root);
596}
602void semantic_analyze(CodeGen *g) {
603 auto it = g->import_table.entry_iterator();
604 for (;;) {
605 auto *entry = it.next();
606 if (!entry)
607 break;
597608
609 ImportTableEntry *import = entry->value;
610 analyze_root(g, import, import->root);
611 }
612}
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
......@@ -664,12 +664,7 @@ static void init(CodeGen *g, Buf *source_path) {
664664
665665}
666666
667void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
668 if (!g->initialized) {
669 g->initialized = true;
670 init(g, source_path);
671 }
672
667static void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
673668 Buf full_path = BUF_INIT;
674669 os_path_join(g->root_source_dir, source_path, &full_path);
675670
......@@ -696,20 +691,46 @@ void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
696691 }
697692
698693 ImportTableEntry *import_entry = allocate<ImportTableEntry>(1);
694 import_entry->fn_table.init(32);
699695 import_entry->root = ast_parse(source_code, tokens);
700696 assert(import_entry->root);
701697 if (g->verbose) {
702698 ast_print(import_entry->root, 0);
703
704 fprintf(stderr, "\nSemantic Analysis:\n");
705 fprintf(stderr, "--------------------\n");
706699 }
707700
708701 import_entry->path = source_path;
709702 import_entry->di_file = LLVMZigCreateFile(g->dbuilder, buf_ptr(&basename), buf_ptr(&dirname));
710703 g->import_table.put(source_path, import_entry);
711704
712 semantic_analyze(g, import_entry);
705
706 assert(import_entry->root->type == NodeTypeRoot);
707 for (int decl_i = 0; decl_i < import_entry->root->data.root.top_level_decls.length; decl_i += 1) {
708 AstNode *top_level_decl = import_entry->root->data.root.top_level_decls.at(decl_i);
709 if (top_level_decl->type != NodeTypeUse)
710 continue;
711
712 auto entry = g->import_table.maybe_get(&top_level_decl->data.use.path);
713 if (!entry) {
714 Buf full_path = BUF_INIT;
715 os_path_join(g->root_source_dir, &top_level_decl->data.use.path, &full_path);
716 Buf import_code = BUF_INIT;
717 os_fetch_file_path(&full_path, &import_code);
718 codegen_add_code(g, &top_level_decl->data.use.path, &import_code);
719 }
720 }
721}
722
723void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) {
724 init(g, source_path);
725
726 codegen_add_code(g, source_path, source_code);
727
728
729 if (g->verbose) {
730 fprintf(stderr, "\nSemantic Analysis:\n");
731 fprintf(stderr, "--------------------\n");
732 }
733 semantic_analyze(g);
713734
714735 if (g->errors.length == 0) {
715736 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 {