mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Merge remote-tracking branch 'origin/master' into type-checking
This commit is contained in:
commit
9278dbedd5
8 changed files with 70 additions and 21 deletions
|
|
@ -1,5 +1,11 @@
|
||||||
use "libc.zig";
|
use "libc.zig";
|
||||||
|
|
||||||
fn print_text() {
|
// purposefully conflicting function with main.zig
|
||||||
|
// but it's private so it should be OK
|
||||||
|
fn private_function() {
|
||||||
puts("it works!");
|
puts("it works!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn print_text() {
|
||||||
|
private_function();
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,10 @@ use "libc.zig";
|
||||||
use "foo.zig";
|
use "foo.zig";
|
||||||
|
|
||||||
fn _start() -> unreachable {
|
fn _start() -> unreachable {
|
||||||
|
private_function();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn private_function() -> unreachable {
|
||||||
print_text();
|
print_text();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
#include "semantic_info.hpp"
|
#include "semantic_info.hpp"
|
||||||
#include "error.hpp"
|
#include "error.hpp"
|
||||||
#include "zig_llvm.hpp"
|
#include "zig_llvm.hpp"
|
||||||
|
#include "os.hpp"
|
||||||
|
|
||||||
struct BlockContext {
|
struct BlockContext {
|
||||||
AstNode *node;
|
AstNode *node;
|
||||||
|
|
@ -218,7 +219,7 @@ static void preview_function_declarations(CodeGen *g, ImportTableEntry *import,
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case NodeTypeUse:
|
case NodeTypeUse:
|
||||||
zig_panic("TODO use");
|
// nothing to do here
|
||||||
break;
|
break;
|
||||||
case NodeTypeDirective:
|
case NodeTypeDirective:
|
||||||
case NodeTypeParamDecl:
|
case NodeTypeParamDecl:
|
||||||
|
|
@ -545,9 +546,16 @@ static void analyze_top_level_declaration(CodeGen *g, AstNode *node) {
|
||||||
|
|
||||||
case NodeTypeRootExportDecl:
|
case NodeTypeRootExportDecl:
|
||||||
case NodeTypeExternBlock:
|
case NodeTypeExternBlock:
|
||||||
case NodeTypeUse:
|
|
||||||
// already looked at these in the preview pass
|
// already looked at these in the preview pass
|
||||||
break;
|
break;
|
||||||
|
case NodeTypeUse:
|
||||||
|
for (int i = 0; i < node->data.use.directives->length; i += 1) {
|
||||||
|
AstNode *directive_node = node->data.use.directives->at(i);
|
||||||
|
Buf *name = &directive_node->data.directive.name;
|
||||||
|
add_node_error(g, directive_node,
|
||||||
|
buf_sprintf("invalid directive: '%s'", buf_ptr(name)));
|
||||||
|
}
|
||||||
|
break;
|
||||||
case NodeTypeDirective:
|
case NodeTypeDirective:
|
||||||
case NodeTypeParamDecl:
|
case NodeTypeParamDecl:
|
||||||
case NodeTypeFnProto:
|
case NodeTypeFnProto:
|
||||||
|
|
@ -591,7 +599,14 @@ static void analyze_root(CodeGen *g, ImportTableEntry *import, AstNode *node) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void semantic_analyze(CodeGen *g, ImportTableEntry *import_table_entry) {
|
void semantic_analyze(CodeGen *g) {
|
||||||
analyze_root(g, import_table_entry, import_table_entry->root);
|
auto it = g->import_table.entry_iterator();
|
||||||
}
|
for (;;) {
|
||||||
|
auto *entry = it.next();
|
||||||
|
if (!entry)
|
||||||
|
break;
|
||||||
|
|
||||||
|
ImportTableEntry *import = entry->value;
|
||||||
|
analyze_root(g, import, import->root);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,7 @@
|
||||||
#define ZIG_ANALYZE_HPP
|
#define ZIG_ANALYZE_HPP
|
||||||
|
|
||||||
struct CodeGen;
|
struct CodeGen;
|
||||||
struct ImportTableEntry;
|
|
||||||
|
|
||||||
void semantic_analyze(CodeGen *g, ImportTableEntry *entry);
|
void semantic_analyze(CodeGen *g);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -664,12 +664,7 @@ static void init(CodeGen *g, Buf *source_path) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
|
static void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
|
||||||
if (!g->initialized) {
|
|
||||||
g->initialized = true;
|
|
||||||
init(g, source_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
Buf full_path = BUF_INIT;
|
Buf full_path = BUF_INIT;
|
||||||
os_path_join(g->root_source_dir, source_path, &full_path);
|
os_path_join(g->root_source_dir, source_path, &full_path);
|
||||||
|
|
||||||
|
|
@ -696,20 +691,46 @@ void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ImportTableEntry *import_entry = allocate<ImportTableEntry>(1);
|
ImportTableEntry *import_entry = allocate<ImportTableEntry>(1);
|
||||||
|
import_entry->fn_table.init(32);
|
||||||
import_entry->root = ast_parse(source_code, tokens);
|
import_entry->root = ast_parse(source_code, tokens);
|
||||||
assert(import_entry->root);
|
assert(import_entry->root);
|
||||||
if (g->verbose) {
|
if (g->verbose) {
|
||||||
ast_print(import_entry->root, 0);
|
ast_print(import_entry->root, 0);
|
||||||
|
|
||||||
fprintf(stderr, "\nSemantic Analysis:\n");
|
|
||||||
fprintf(stderr, "--------------------\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
import_entry->path = source_path;
|
import_entry->path = source_path;
|
||||||
import_entry->di_file = LLVMZigCreateFile(g->dbuilder, buf_ptr(&basename), buf_ptr(&dirname));
|
import_entry->di_file = LLVMZigCreateFile(g->dbuilder, buf_ptr(&basename), buf_ptr(&dirname));
|
||||||
g->import_table.put(source_path, import_entry);
|
g->import_table.put(source_path, import_entry);
|
||||||
|
|
||||||
semantic_analyze(g, import_entry);
|
|
||||||
|
assert(import_entry->root->type == NodeTypeRoot);
|
||||||
|
for (int decl_i = 0; decl_i < import_entry->root->data.root.top_level_decls.length; decl_i += 1) {
|
||||||
|
AstNode *top_level_decl = import_entry->root->data.root.top_level_decls.at(decl_i);
|
||||||
|
if (top_level_decl->type != NodeTypeUse)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto entry = g->import_table.maybe_get(&top_level_decl->data.use.path);
|
||||||
|
if (!entry) {
|
||||||
|
Buf full_path = BUF_INIT;
|
||||||
|
os_path_join(g->root_source_dir, &top_level_decl->data.use.path, &full_path);
|
||||||
|
Buf import_code = BUF_INIT;
|
||||||
|
os_fetch_file_path(&full_path, &import_code);
|
||||||
|
codegen_add_code(g, &top_level_decl->data.use.path, &import_code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) {
|
||||||
|
init(g, source_path);
|
||||||
|
|
||||||
|
codegen_add_code(g, source_path, source_code);
|
||||||
|
|
||||||
|
|
||||||
|
if (g->verbose) {
|
||||||
|
fprintf(stderr, "\nSemantic Analysis:\n");
|
||||||
|
fprintf(stderr, "--------------------\n");
|
||||||
|
}
|
||||||
|
semantic_analyze(g);
|
||||||
|
|
||||||
if (g->errors.length == 0) {
|
if (g->errors.length == 0) {
|
||||||
if (g->verbose) {
|
if (g->verbose) {
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ void codegen_set_verbose(CodeGen *codegen, bool verbose);
|
||||||
void codegen_set_out_type(CodeGen *codegen, OutType out_type);
|
void codegen_set_out_type(CodeGen *codegen, OutType out_type);
|
||||||
void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
|
void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
|
||||||
|
|
||||||
void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code);
|
void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code);
|
||||||
|
|
||||||
void codegen_link(CodeGen *g, const char *out_file);
|
void codegen_link(CodeGen *g, const char *out_file);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ static int build(const char *arg0, Build *b) {
|
||||||
if (b->out_name)
|
if (b->out_name)
|
||||||
codegen_set_out_name(g, buf_create_from_str(b->out_name));
|
codegen_set_out_name(g, buf_create_from_str(b->out_name));
|
||||||
codegen_set_verbose(g, b->verbose);
|
codegen_set_verbose(g, b->verbose);
|
||||||
codegen_add_code(g, &root_source_name, &root_source_code);
|
codegen_add_root_code(g, &root_source_name, &root_source_code);
|
||||||
codegen_link(g, b->out_file);
|
codegen_link(g, b->out_file);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@
|
||||||
#include "hash_map.hpp"
|
#include "hash_map.hpp"
|
||||||
#include "zig_llvm.hpp"
|
#include "zig_llvm.hpp"
|
||||||
|
|
||||||
|
struct FnTableEntry;
|
||||||
|
|
||||||
struct TypeTableEntry {
|
struct TypeTableEntry {
|
||||||
LLVMTypeRef type_ref;
|
LLVMTypeRef type_ref;
|
||||||
LLVMZigDIType *di_type;
|
LLVMZigDIType *di_type;
|
||||||
|
|
@ -28,6 +30,9 @@ struct ImportTableEntry {
|
||||||
AstNode *root;
|
AstNode *root;
|
||||||
Buf *path; // relative to root_source_dir
|
Buf *path; // relative to root_source_dir
|
||||||
LLVMZigDIFile *di_file;
|
LLVMZigDIFile *di_file;
|
||||||
|
|
||||||
|
// reminder: hash tables must be initialized before use
|
||||||
|
HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct FnTableEntry {
|
struct FnTableEntry {
|
||||||
|
|
@ -81,7 +86,6 @@ struct CodeGen {
|
||||||
int version_minor;
|
int version_minor;
|
||||||
int version_patch;
|
int version_patch;
|
||||||
bool verbose;
|
bool verbose;
|
||||||
bool initialized;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TypeNode {
|
struct TypeNode {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue