diff options
Diffstat (limited to 'src/codegen.cpp')
| -rw-r--r-- | src/codegen.cpp | 134 |
1 files changed, 68 insertions, 66 deletions
diff --git a/src/codegen.cpp b/src/codegen.cpp index 3a9efc9fa9..650cbee946 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -48,16 +48,17 @@ static void init_darwin_native(CodeGen *g) { } } -static PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path) { - PackageTableEntry *entry = allocate<PackageTableEntry>(1); +static ZigPackage *new_package(const char *root_src_dir, const char *root_src_path, const char *pkg_path) { + ZigPackage *entry = allocate<ZigPackage>(1); entry->package_table.init(4); buf_init_from_str(&entry->root_src_dir, root_src_dir); buf_init_from_str(&entry->root_src_path, root_src_path); + buf_init_from_str(&entry->pkg_path, pkg_path); return entry; } -PackageTableEntry *new_anonymous_package(void) { - return new_package("", ""); +ZigPackage *new_anonymous_package() { + return new_package("", "", ""); } static const char *symbols_that_llvm_depends_on[] = { @@ -141,11 +142,11 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out exit(1); } - g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename)); - g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig"); + g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename), ""); + g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig", "std"); g->root_package->package_table.put(buf_create_from_str("std"), g->std_package); } else { - g->root_package = new_package(".", ""); + g->root_package = new_package(".", "", ""); } g->zig_std_special_dir = buf_alloc(); @@ -621,7 +622,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) { if (scope->di_scope) return scope->di_scope; - ImportTableEntry *import = get_scope_import(scope); + ZigType *import = get_scope_import(scope); switch (scope->id) { case ScopeIdCImport: zig_unreachable(); @@ -644,7 +645,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) { assert(fn_di_scope != nullptr); ZigLLVMDISubprogram *subprogram = ZigLLVMCreateFunction(g->dbuilder, fn_di_scope, buf_ptr(&fn_table_entry->symbol_name), "", - import->di_file, line_number, + import->data.structure.root_struct->di_file, line_number, fn_table_entry->type_entry->data.fn.raw_di_type, is_internal_linkage, is_definition, scope_line, flags, is_optimized, nullptr); @@ -658,7 +659,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) { assert(decls_scope->container_type); scope->di_scope = ZigLLVMTypeToScope(decls_scope->container_type->di_type); } else { - scope->di_scope = ZigLLVMFileToScope(import->di_file); + scope->di_scope = ZigLLVMFileToScope(import->data.structure.root_struct->di_file); } return scope->di_scope; case ScopeIdBlock: @@ -668,7 +669,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) { assert(scope->parent); ZigLLVMDILexicalBlock *di_block = ZigLLVMCreateLexicalBlock(g->dbuilder, get_di_scope(g, scope->parent), - import->di_file, + import->data.structure.root_struct->di_file, (unsigned)scope->source_node->line + 1, (unsigned)scope->source_node->column + 1); scope->di_scope = ZigLLVMLexicalBlockToScope(di_block); @@ -2197,7 +2198,7 @@ var_ok: if (dest_ty != nullptr && var->decl_node) { // arg index + 1 because the 0 index is return value var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope), - buf_ptr(&var->name), fn_walk->data.vars.import->di_file, + buf_ptr(&var->name), fn_walk->data.vars.import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1), dest_ty->di_type, !g->strip_debug_symbols, 0, di_arg_index + 1); } @@ -5801,7 +5802,6 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con case ZigTypeIdNull: case ZigTypeIdErrorUnion: case ZigTypeIdErrorSet: - case ZigTypeIdNamespace: case ZigTypeIdBoundFn: case ZigTypeIdArgTuple: case ZigTypeIdVoid: @@ -6401,7 +6401,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c case ZigTypeIdComptimeInt: case ZigTypeIdUndefined: case ZigTypeIdNull: - case ZigTypeIdNamespace: case ZigTypeIdBoundFn: case ZigTypeIdArgTuple: case ZigTypeIdOpaque: @@ -6507,12 +6506,12 @@ static void gen_global_var(CodeGen *g, ZigVar *var, LLVMValueRef init_val, assert(var->gen_is_const); assert(type_entry); - ImportTableEntry *import = get_scope_import(var->parent_scope); + ZigType *import = get_scope_import(var->parent_scope); assert(import); bool is_local_to_unit = true; ZigLLVMCreateGlobalVariable(g->dbuilder, get_di_scope(g, var->parent_scope), buf_ptr(&var->name), - buf_ptr(&var->name), import->di_file, + buf_ptr(&var->name), import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1), type_entry->di_type, is_local_to_unit); @@ -6768,7 +6767,7 @@ static void do_code_gen(CodeGen *g) { *slot = build_alloca(g, slot_type, "", alignment_bytes); } - ImportTableEntry *import = get_scope_import(&fn_table_entry->fndef_scope->base); + ZigType *import = get_scope_import(&fn_table_entry->fndef_scope->base); unsigned gen_i_init = want_first_arg_sret(g, fn_type_id) ? 1 : 0; @@ -6800,7 +6799,7 @@ static void do_code_gen(CodeGen *g) { var->value_ref = build_alloca(g, var->var_type, buf_ptr(&var->name), var->align_bytes); var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope), - buf_ptr(&var->name), import->di_file, (unsigned)(var->decl_node->line + 1), + buf_ptr(&var->name), import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1), var->var_type->di_type, !g->strip_debug_symbols, 0); } else if (is_c_abi) { @@ -6824,7 +6823,7 @@ static void do_code_gen(CodeGen *g) { } if (var->decl_node) { var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope), - buf_ptr(&var->name), import->di_file, + buf_ptr(&var->name), import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1), gen_type->di_type, !g->strip_debug_symbols, 0, (unsigned)(var->gen_arg_index + 1)); } @@ -6973,12 +6972,6 @@ static void define_builtin_types(CodeGen *g) { g->builtin_types.entry_invalid = entry; } { - ZigType *entry = new_type_table_entry(ZigTypeIdNamespace); - buf_init_from_str(&entry->name, "(namespace)"); - entry->zero_bits = true; - g->builtin_types.entry_namespace = entry; - } - { ZigType *entry = new_type_table_entry(ZigTypeIdComptimeFloat); buf_init_from_str(&entry->name, "comptime_float"); entry->zero_bits = true; @@ -7131,7 +7124,8 @@ static void define_builtin_types(CodeGen *g) { g->builtin_types.entry_i64 = get_int_type(g, true, 64); { - g->builtin_types.entry_c_void = get_opaque_type(g, nullptr, nullptr, "c_void"); + g->builtin_types.entry_c_void = get_opaque_type(g, nullptr, nullptr, "c_void", + buf_create_from_str("c_void")); g->primitive_type_table.put(&g->builtin_types.entry_c_void->name, g->builtin_types.entry_c_void); } @@ -7471,7 +7465,6 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { " Enum: Enum,\n" " Union: Union,\n" " Fn: Fn,\n" - " Namespace: void,\n" " BoundFn: Fn,\n" " ArgTuple: void,\n" " Opaque: void,\n" @@ -7752,12 +7745,12 @@ static Error define_builtin_compile_vars(CodeGen *g) { assert(g->root_package); assert(g->std_package); - g->compile_var_package = new_package(buf_ptr(this_dir), builtin_zig_basename); + g->compile_var_package = new_package(buf_ptr(this_dir), builtin_zig_basename, "builtin"); g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); g->std_package->package_table.put(buf_create_from_str("std"), g->std_package); - g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents); - scan_import(g, g->compile_var_import); + g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents, + SourceKindPkgMain); return ErrorNone; } @@ -7949,17 +7942,20 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) { Buf *src_dirname = buf_alloc(); os_path_split(full_path, src_dirname, src_basename); - ImportTableEntry *import = allocate<ImportTableEntry>(1); - import->source_code = nullptr; - import->path = full_path; - g->root_import = import; - import->decls_scope = create_decls_scope(g, nullptr, nullptr, nullptr, import); + Buf noextname = BUF_INIT; + os_path_extname(src_basename, &noextname, nullptr); detect_libc(g); init(g); - import->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname)); + RootStruct *root_struct = allocate<RootStruct>(1); + root_struct->source_code = nullptr; + root_struct->path = full_path; + root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname)); + + ZigType *import = get_root_container_type(g, buf_ptr(&noextname), &noextname, root_struct); + g->root_import = import; ZigList<ErrorMsg *> errors = {0}; Error err = parse_h_file(import, &errors, buf_ptr(full_path), g, nullptr); @@ -7978,7 +7974,7 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) { } } -static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package, const char *basename) { +static ZigType *add_special_code(CodeGen *g, ZigPackage *package, const char *basename) { Buf *code_basename = buf_create_from_str(basename); Buf path_to_code_src = BUF_INIT; os_path_join(g->zig_std_special_dir, code_basename, &path_to_code_src); @@ -7992,21 +7988,21 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err)); } - return add_source_file(g, package, resolved_path, import_code); + return add_source_file(g, package, resolved_path, import_code, SourceKindPkgMain); } -static PackageTableEntry *create_bootstrap_pkg(CodeGen *g, PackageTableEntry *pkg_with_main) { - PackageTableEntry *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig"); +static ZigPackage *create_bootstrap_pkg(CodeGen *g, ZigPackage *pkg_with_main) { + ZigPackage *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig", "std.special"); package->package_table.put(buf_create_from_str("@root"), pkg_with_main); return package; } -static PackageTableEntry *create_test_runner_pkg(CodeGen *g) { - return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig"); +static ZigPackage *create_test_runner_pkg(CodeGen *g) { + return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig", "std.special"); } -static PackageTableEntry *create_panic_pkg(CodeGen *g) { - return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig"); +static ZigPackage *create_panic_pkg(CodeGen *g) { + return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig", "std.special"); } static void create_test_compile_var_and_add_test_runner(CodeGen *g) { @@ -8090,24 +8086,24 @@ static void gen_root_source(CodeGen *g) { exit(1); } - g->root_import = add_source_file(g, g->root_package, resolved_path, source_code); + ZigType *root_import_alias = add_source_file(g, g->root_package, resolved_path, source_code, SourceKindRoot); + assert(root_import_alias == g->root_import); assert(g->root_out_name); assert(g->out_type != OutTypeUnknown); { // Zig has lazy top level definitions. Here we semantically analyze the panic function. - ImportTableEntry *import_with_panic; + ZigType *import_with_panic; if (g->have_pub_panic) { import_with_panic = g->root_import; } else { g->panic_package = create_panic_pkg(g); import_with_panic = add_special_code(g, g->panic_package, "panic.zig"); } - scan_import(g, import_with_panic); - Tld *panic_tld = find_decl(g, &import_with_panic->decls_scope->base, buf_create_from_str("panic")); + Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic")); assert(panic_tld != nullptr); - resolve_top_level_decl(g, panic_tld, false, nullptr); + resolve_top_level_decl(g, panic_tld, nullptr); } @@ -8359,7 +8355,6 @@ static void prepend_c_type_to_decl_list(CodeGen *g, GenH *gen_h, ZigType *type_e case ZigTypeIdComptimeInt: case ZigTypeIdUndefined: case ZigTypeIdNull: - case ZigTypeIdNamespace: case ZigTypeIdBoundFn: case ZigTypeIdArgTuple: case ZigTypeIdErrorUnion: @@ -8507,19 +8502,19 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu case ZigTypeIdOpaque: { buf_init_from_str(out_buf, "struct "); - buf_append_buf(out_buf, &type_entry->name); + buf_append_buf(out_buf, type_h_name(type_entry)); return; } case ZigTypeIdUnion: { buf_init_from_str(out_buf, "union "); - buf_append_buf(out_buf, &type_entry->name); + buf_append_buf(out_buf, type_h_name(type_entry)); return; } case ZigTypeIdEnum: { buf_init_from_str(out_buf, "enum "); - buf_append_buf(out_buf, &type_entry->name); + buf_append_buf(out_buf, type_h_name(type_entry)); return; } case ZigTypeIdArray: @@ -8542,7 +8537,6 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu case ZigTypeIdInvalid: case ZigTypeIdMetaType: case ZigTypeIdBoundFn: - case ZigTypeIdNamespace: case ZigTypeIdComptimeFloat: case ZigTypeIdComptimeInt: case ZigTypeIdUndefined: @@ -8615,10 +8609,17 @@ static void gen_h_file(CodeGen *g) { Buf return_type_c = BUF_INIT; get_c_type(g, gen_h, fn_type_id->return_type, &return_type_c); + Buf *symbol_name; + if (fn_table_entry->export_list.length == 0) { + symbol_name = &fn_table_entry->symbol_name; + } else { + FnExport *fn_export = &fn_table_entry->export_list.items[0]; + symbol_name = &fn_export->name; + } buf_appendf(&h_buf, "%s %s %s(", buf_ptr(export_macro), buf_ptr(&return_type_c), - buf_ptr(&fn_table_entry->symbol_name)); + buf_ptr(symbol_name)); Buf param_type_c = BUF_INIT; if (fn_type_id->param_count > 0) { @@ -8694,7 +8695,6 @@ static void gen_h_file(CodeGen *g) { case ZigTypeIdNull: case ZigTypeIdErrorUnion: case ZigTypeIdErrorSet: - case ZigTypeIdNamespace: case ZigTypeIdBoundFn: case ZigTypeIdArgTuple: case ZigTypeIdOptional: @@ -8704,7 +8704,7 @@ static void gen_h_file(CodeGen *g) { zig_unreachable(); case ZigTypeIdEnum: if (type_entry->data.enumeration.layout == ContainerLayoutExtern) { - fprintf(out_h, "enum %s {\n", buf_ptr(&type_entry->name)); + fprintf(out_h, "enum %s {\n", buf_ptr(type_h_name(type_entry))); for (uint32_t field_i = 0; field_i < type_entry->data.enumeration.src_field_count; field_i += 1) { TypeEnumField *enum_field = &type_entry->data.enumeration.fields[field_i]; Buf *value_buf = buf_alloc(); @@ -8717,12 +8717,12 @@ static void gen_h_file(CodeGen *g) { } fprintf(out_h, "};\n\n"); } else { - fprintf(out_h, "enum %s;\n", buf_ptr(&type_entry->name)); + fprintf(out_h, "enum %s;\n", buf_ptr(type_h_name(type_entry))); } break; case ZigTypeIdStruct: if (type_entry->data.structure.layout == ContainerLayoutExtern) { - fprintf(out_h, "struct %s {\n", buf_ptr(&type_entry->name)); + fprintf(out_h, "struct %s {\n", buf_ptr(type_h_name(type_entry))); for (uint32_t field_i = 0; field_i < type_entry->data.structure.src_field_count; field_i += 1) { TypeStructField *struct_field = &type_entry->data.structure.fields[field_i]; @@ -8740,12 +8740,12 @@ static void gen_h_file(CodeGen *g) { } fprintf(out_h, "};\n\n"); } else { - fprintf(out_h, "struct %s;\n", buf_ptr(&type_entry->name)); + fprintf(out_h, "struct %s;\n", buf_ptr(type_h_name(type_entry))); } break; case ZigTypeIdUnion: if (type_entry->data.unionation.layout == ContainerLayoutExtern) { - fprintf(out_h, "union %s {\n", buf_ptr(&type_entry->name)); + fprintf(out_h, "union %s {\n", buf_ptr(type_h_name(type_entry))); for (uint32_t field_i = 0; field_i < type_entry->data.unionation.src_field_count; field_i += 1) { TypeUnionField *union_field = &type_entry->data.unionation.fields[field_i]; @@ -8755,11 +8755,11 @@ static void gen_h_file(CodeGen *g) { } fprintf(out_h, "};\n\n"); } else { - fprintf(out_h, "union %s;\n", buf_ptr(&type_entry->name)); + fprintf(out_h, "union %s;\n", buf_ptr(type_h_name(type_entry))); } break; case ZigTypeIdOpaque: - fprintf(out_h, "struct %s;\n\n", buf_ptr(&type_entry->name)); + fprintf(out_h, "struct %s;\n\n", buf_ptr(type_h_name(type_entry))); break; } } @@ -8793,7 +8793,7 @@ void codegen_add_time_event(CodeGen *g, const char *name) { g->timing_events.append({os_get_time(), name}); } -static void add_cache_pkg(CodeGen *g, CacheHash *ch, PackageTableEntry *pkg) { +static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) { if (buf_len(&pkg->root_src_path) == 0) return; @@ -9047,9 +9047,11 @@ void codegen_build_and_link(CodeGen *g) { codegen_add_time_event(g, "Done"); } -PackageTableEntry *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path) { +ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path, + const char *pkg_path) +{ init(g); - PackageTableEntry *pkg = new_package(root_src_dir, root_src_path); + ZigPackage *pkg = new_package(root_src_dir, root_src_path, pkg_path); if (g->std_package != nullptr) { assert(g->compile_var_package != nullptr); pkg->package_table.put(buf_create_from_str("std"), g->std_package); |
