Merge pull request #2020 from ziglang/kill-namespace-type

remove the (namespace) type and make every file an empty struct
This commit is contained in:
Andrew Kelley 2019-03-01 18:07:46 -05:00 committed by GitHub
commit bed81089e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 1347 additions and 1269 deletions

View File

@ -6785,7 +6785,6 @@ pub const TypeId = enum {
Enum,
Union,
Fn,
Namespace,
Block,
BoundFn,
ArgTuple,
@ -6820,7 +6819,6 @@ pub const TypeInfo = union(TypeId) {
Enum: Enum,
Union: Union,
Fn: Fn,
Namespace: void,
BoundFn: Fn,
ArgTuple: void,
Opaque: void,
@ -8167,17 +8165,18 @@ coding style.
</p>
<ul>
<li>
If {#syntax#}x{#endsyntax#} is a {#syntax#}struct{#endsyntax#} (or an alias of a {#syntax#}struct{#endsyntax#}),
then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}.
If {#syntax#}x{#endsyntax#} is a {#syntax#}type{#endsyntax#}
then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}, unless it
is a {#syntax#}struct{#endsyntax#} with 0 fields and is never meant to be instantiated,
in which case it is considered to be a "namespace" and uses {#syntax#}snake_case{#endsyntax#}.
</li>
<li>
If {#syntax#}x{#endsyntax#} otherwise identifies a type, {#syntax#}x{#endsyntax#} should have {#syntax#}snake_case{#endsyntax#}.
If {#syntax#}x{#endsyntax#} is callable, and {#syntax#}x{#endsyntax#}'s return type is
{#syntax#}type{#endsyntax#}, then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}.
</li>
<li>
If {#syntax#}x{#endsyntax#} is callable, and {#syntax#}x{#endsyntax#}'s return type is {#syntax#}type{#endsyntax#}, then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}.
</li>
<li>
If {#syntax#}x{#endsyntax#} is otherwise callable, then {#syntax#}x{#endsyntax#} should be {#syntax#}camelCase{#endsyntax#}.
If {#syntax#}x{#endsyntax#} is otherwise callable, then {#syntax#}x{#endsyntax#} should
be {#syntax#}camelCase{#endsyntax#}.
</li>
<li>
Otherwise, {#syntax#}x{#endsyntax#} should be {#syntax#}snake_case{#endsyntax#}.
@ -8203,7 +8202,9 @@ const const_name = 42;
const primitive_type_alias = f32;
const string_alias = []u8;
const StructName = struct {};
const StructName = struct {
field: i32,
};
const StructAlias = StructName;
fn functionName(param_name: TypeName) void {
@ -8231,7 +8232,9 @@ const xml_document =
\\<document>
\\</document>
;
const XmlParser = struct {};
const XmlParser = struct {
field: i32,
};
// The initials BE (Big Endian) are just another word in Zig identifier names.
fn readU32Be() u32 {}

View File

@ -47,7 +47,7 @@ pub const ZigCompiler = struct {
var lazy_init_targets = std.lazyInit(void);
fn init(loop: *event.Loop) !ZigCompiler {
pub fn init(loop: *event.Loop) !ZigCompiler {
lazy_init_targets.get() orelse {
Target.initializeAll();
lazy_init_targets.resolve();

View File

@ -39,7 +39,6 @@ pub const Type = struct {
Id.ErrorSet => @fieldParentPtr(ErrorSet, "base", base).destroy(comp),
Id.Enum => @fieldParentPtr(Enum, "base", base).destroy(comp),
Id.Union => @fieldParentPtr(Union, "base", base).destroy(comp),
Id.Namespace => @fieldParentPtr(Namespace, "base", base).destroy(comp),
Id.BoundFn => @fieldParentPtr(BoundFn, "base", base).destroy(comp),
Id.ArgTuple => @fieldParentPtr(ArgTuple, "base", base).destroy(comp),
Id.Opaque => @fieldParentPtr(Opaque, "base", base).destroy(comp),
@ -73,7 +72,6 @@ pub const Type = struct {
Id.ErrorSet => return @fieldParentPtr(ErrorSet, "base", base).getLlvmType(allocator, llvm_context),
Id.Enum => return @fieldParentPtr(Enum, "base", base).getLlvmType(allocator, llvm_context),
Id.Union => return @fieldParentPtr(Union, "base", base).getLlvmType(allocator, llvm_context),
Id.Namespace => unreachable,
Id.BoundFn => return @fieldParentPtr(BoundFn, "base", base).getLlvmType(allocator, llvm_context),
Id.ArgTuple => unreachable,
Id.Opaque => return @fieldParentPtr(Opaque, "base", base).getLlvmType(allocator, llvm_context),
@ -89,7 +87,6 @@ pub const Type = struct {
Id.ComptimeInt,
Id.Undefined,
Id.Null,
Id.Namespace,
Id.BoundFn,
Id.ArgTuple,
Id.Opaque,
@ -123,7 +120,6 @@ pub const Type = struct {
Id.ComptimeInt,
Id.Undefined,
Id.Null,
Id.Namespace,
Id.BoundFn,
Id.ArgTuple,
Id.Opaque,
@ -1020,14 +1016,6 @@ pub const Type = struct {
}
};
pub const Namespace = struct {
base: Type,
pub fn destroy(self: *Namespace, comp: *Compilation) void {
comp.gpa().destroy(self);
}
};
pub const BoundFn = struct {
base: Type,

View File

@ -21,7 +21,6 @@
#include "libc_installation.hpp"
struct AstNode;
struct ImportTableEntry;
struct ZigFn;
struct Scope;
struct ScopeBlock;
@ -317,7 +316,6 @@ struct ConstExprValue {
ConstUnionValue x_union;
ConstArrayValue x_array;
ConstPtrValue x_ptr;
ImportTableEntry *x_import;
ConstArgTuple x_arg_tuple;
// populated if special == ConstValSpecialRuntime
@ -369,10 +367,8 @@ struct Tld {
VisibMod visib_mod;
AstNode *source_node;
ImportTableEntry *import;
ZigType *import;
Scope *parent_scope;
// set this flag temporarily to detect infinite loops
bool dep_loop_flag;
TldResolution resolution;
};
@ -382,6 +378,7 @@ struct TldVar {
ZigVar *var;
Buf *extern_lib_name;
Buf *section_name;
bool analyzing_type; // flag to detect dependency loops
};
struct TldFn {
@ -700,7 +697,7 @@ struct AstNodeUse {
AstNode *expr;
TldResolution resolution;
ConstExprValue *value;
ConstExprValue *using_namespace_value;
};
struct AstNodeIfBoolExpr {
@ -937,7 +934,7 @@ struct AstNode {
enum NodeType type;
size_t line;
size_t column;
ImportTableEntry *owner;
ZigType *owner;
union {
AstNodeFnDef fn_def;
AstNodeFnProto fn_proto;
@ -1075,12 +1072,32 @@ enum ResolveStatus {
ResolveStatusSizeKnown,
};
struct ZigPackage {
Buf root_src_dir;
Buf root_src_path; // relative to root_src_dir
Buf pkg_path; // a.b.c.d which follows the package dependency chain from the root package
// reminder: hash tables must be initialized before use
HashMap<Buf *, ZigPackage *, buf_hash, buf_eql_buf> package_table;
};
// Stuff that only applies to a struct which is the implicit root struct of a file
struct RootStruct {
ZigPackage *package;
Buf *path; // relative to root_package->root_src_dir
ZigList<size_t> *line_offsets;
Buf *source_code;
AstNode *c_import_node;
ZigLLVMDIFile *di_file;
};
struct ZigTypeStruct {
AstNode *decl_node;
TypeStructField *fields;
ScopeDecls *decls_scope;
uint64_t size_bytes;
HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
RootStruct *root_struct;
uint32_t src_field_count;
uint32_t gen_field_count;
@ -1232,7 +1249,6 @@ enum ZigTypeId {
ZigTypeIdEnum,
ZigTypeIdUnion,
ZigTypeIdFn,
ZigTypeIdNamespace,
ZigTypeIdBoundFn,
ZigTypeIdArgTuple,
ZigTypeIdOpaque,
@ -1246,6 +1262,10 @@ enum OnePossibleValue {
OnePossibleValueYes,
};
struct ZigTypeOpaque {
Buf *bare_name;
};
struct ZigType {
ZigTypeId id;
Buf name;
@ -1268,6 +1288,7 @@ struct ZigType {
ZigTypeBoundFn bound_fn;
ZigTypePromise promise;
ZigTypeVector vector;
ZigTypeOpaque opaque;
} data;
// use these fields to make sure we don't duplicate type table entries for the same type
@ -1285,29 +1306,6 @@ struct ZigType {
bool gen_h_loop_flag;
};
struct PackageTableEntry {
Buf root_src_dir;
Buf root_src_path; // relative to root_src_dir
// reminder: hash tables must be initialized before use
HashMap<Buf *, PackageTableEntry *, buf_hash, buf_eql_buf> package_table;
};
struct ImportTableEntry {
AstNode *root;
Buf *path; // relative to root_package->root_src_dir
PackageTableEntry *package;
ZigLLVMDIFile *di_file;
Buf *source_code;
ZigList<size_t> *line_offsets;
ScopeDecls *decls_scope;
AstNode *c_import_node;
bool any_imports_failed;
bool scanned;
ZigList<AstNode *> use_decls;
};
enum FnAnalState {
FnAnalStateReady,
FnAnalStateProbing,
@ -1670,7 +1668,7 @@ struct CodeGen {
LLVMValueRef return_err_fn;
// reminder: hash tables must be initialized before use
HashMap<Buf *, ImportTableEntry *, buf_hash, buf_eql_buf> import_table;
HashMap<Buf *, ZigType *, buf_hash, buf_eql_buf> import_table;
HashMap<Buf *, BuiltinFnEntry *, buf_hash, buf_eql_buf> builtin_fn_table;
HashMap<Buf *, ZigType *, buf_hash, buf_eql_buf> primitive_type_table;
HashMap<TypeId, ZigType *, type_id_hash, type_id_eql> type_table;
@ -1684,8 +1682,6 @@ struct CodeGen {
HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;
HashMap<const ZigType *, ConstExprValue *, type_ptr_hash, type_ptr_eql> type_info_cache;
ZigList<ImportTableEntry *> import_queue;
size_t import_queue_index;
ZigList<Tld *> resolve_queue;
size_t resolve_queue_index;
ZigList<AstNode *> use_queue;
@ -1699,14 +1695,14 @@ struct CodeGen {
ZigList<ErrorTableEntry *> errors_by_index;
size_t largest_err_name_len;
PackageTableEntry *std_package;
PackageTableEntry *panic_package;
PackageTableEntry *test_runner_package;
PackageTableEntry *compile_var_package;
ImportTableEntry *compile_var_import;
ImportTableEntry *root_import;
ImportTableEntry *bootstrap_import;
ImportTableEntry *test_runner_import;
ZigPackage *std_package;
ZigPackage *panic_package;
ZigPackage *test_runner_package;
ZigPackage *compile_var_package;
ZigType *compile_var_import;
ZigType *root_import;
ZigType *bootstrap_import;
ZigType *test_runner_import;
struct {
ZigType *entry_bool;
@ -1731,7 +1727,6 @@ struct CodeGen {
ZigType *entry_unreachable;
ZigType *entry_type;
ZigType *entry_invalid;
ZigType *entry_namespace;
ZigType *entry_block;
ZigType *entry_num_lit_int;
ZigType *entry_num_lit_float;
@ -1851,7 +1846,7 @@ struct CodeGen {
Buf *root_out_name;
Buf *test_filter;
Buf *test_name_prefix;
PackageTableEntry *root_package;
ZigPackage *root_package;
Buf *zig_lib_dir;
Buf *zig_std_dir;
@ -1945,13 +1940,17 @@ struct ScopeDecls {
Scope base;
HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> decl_table;
bool safety_off;
ZigList<AstNode *> use_decls;
AstNode *safety_set_node;
bool fast_math_on;
AstNode *fast_math_set_node;
ImportTableEntry *import;
ZigType *import;
// If this is a scope from a container, this is the type entry, otherwise null
ZigType *container_type;
Buf *bare_name;
bool safety_off;
bool fast_math_on;
bool any_imports_failed;
};
// This scope comes from a block expression in user code.
@ -3477,6 +3476,8 @@ static const size_t stack_trace_ptr_count = 32;
#define ERR_RET_TRACE_PTR_FIELD_NAME "err_ret_trace_ptr"
#define RESULT_PTR_FIELD_NAME "result_ptr"
#define NAMESPACE_SEP_CHAR '.'
#define NAMESPACE_SEP_STR "."
enum FloatMode {
FloatModeStrict,
@ -3508,7 +3509,7 @@ struct FnWalkTypes {
};
struct FnWalkVars {
ImportTableEntry *import;
ZigType *import;
LLVMValueRef llvm_fn;
ZigFn *fn;
ZigVar *var;

File diff suppressed because it is too large Load Diff

View File

@ -12,8 +12,9 @@
void semantic_analyze(CodeGen *g);
ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);
ErrorMsg *add_token_error(CodeGen *g, ImportTableEntry *owner, Token *token, Buf *msg);
ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg);
ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, AstNode *node, Buf *msg);
void emit_error_notes_for_ref_stack(CodeGen *g, ErrorMsg *msg);
ZigType *new_type_table_entry(ZigTypeId id);
ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const);
ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_const,
@ -29,11 +30,13 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type);
ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size);
ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type);
ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
AstNode *decl_node, const char *name, ContainerLayout layout);
AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout);
ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *bare_name,
RootStruct *root_struct);
ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);
ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type);
ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry);
ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *name);
ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *full_name, Buf *bare_name);
ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
ZigType *field_types[], size_t field_count);
ZigType *get_promise_type(CodeGen *g, ZigType *result_type);
@ -46,12 +49,18 @@ bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry);
bool ptr_allows_addr_zero(ZigType *ptr_type);
bool type_is_nonnull_ptr(ZigType *type);
ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *abs_full_path, Buf *source_code);
enum SourceKind {
SourceKindRoot,
SourceKindPkgMain,
SourceKindNonRoot,
};
ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Buf *source_code,
SourceKind source_kind);
ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope);
Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only, AstNode *source_node);
Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);
void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);
bool type_is_codegen_pointer(ZigType *type);
ZigType *get_src_ptr_type(ZigType *type);
@ -77,11 +86,11 @@ bool is_array_ref(ZigType *type_entry);
bool is_container_ref(ZigType *type_entry);
bool is_valid_vector_elem_type(ZigType *elem_type);
void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node);
void scan_import(CodeGen *g, ImportTableEntry *import);
void preview_use_decl(CodeGen *g, AstNode *node);
void resolve_use_decl(CodeGen *g, AstNode *node);
ZigFn *scope_fn_entry(Scope *scope);
ImportTableEntry *get_scope_import(Scope *scope);
ZigPackage *scope_package(Scope *scope);
ZigType *get_scope_import(Scope *scope);
void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node, Scope *parent_scope);
ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
bool is_const, ConstExprValue *init_value, Tld *src_tld, ZigType *var_type);
@ -109,7 +118,6 @@ ScopeCImport *create_cimport_scope(CodeGen *g, AstNode *node, Scope *parent);
ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent);
ScopeSuspend *create_suspend_scope(CodeGen *g, AstNode *node, Scope *parent);
ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *fn_entry);
ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ImportTableEntry *import);
Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent);
Scope *create_coro_prelude_scope(CodeGen *g, AstNode *node, Scope *parent);
Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstruction *is_comptime);
@ -186,7 +194,7 @@ LinkLib *add_link_lib(CodeGen *codegen, Buf *lib);
uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry);
ZigType *get_align_amt_type(CodeGen *g);
PackageTableEntry *new_anonymous_package(void);
ZigPackage *new_anonymous_package(void);
Buf *const_value_to_buffer(ConstExprValue *const_val);
void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, Buf *symbol_name, GlobalLinkageId linkage, bool ccc);
@ -232,4 +240,6 @@ Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_no
ConstExprValue *const_val, ZigType *wanted_type);
void typecheck_panic_fn(CodeGen *g, TldFn *tld_fn, ZigFn *panic_fn);
Buf *type_bare_name(ZigType *t);
Buf *type_h_name(ZigType *t);
#endif

View File

@ -59,7 +59,7 @@ static inline void buf_deinit(Buf *buf) {
static inline void buf_init_from_mem(Buf *buf, const char *ptr, size_t len) {
assert(len != SIZE_MAX);
buf->list.resize(len + 1);
safe_memcpy(buf_ptr(buf), ptr, len);
memcpy(buf_ptr(buf), ptr, len);
buf->list.at(buf_len(buf)) = 0;
}
@ -98,7 +98,7 @@ static inline Buf *buf_slice(Buf *in_buf, size_t start, size_t end) {
assert(end <= buf_len(in_buf));
Buf *out_buf = allocate<Buf>(1);
out_buf->list.resize(end - start + 1);
safe_memcpy(buf_ptr(out_buf), buf_ptr(in_buf) + start, end - start);
memcpy(buf_ptr(out_buf), buf_ptr(in_buf) + start, end - start);
out_buf->list.at(buf_len(out_buf)) = 0;
return out_buf;
}
@ -108,7 +108,7 @@ static inline void buf_append_mem(Buf *buf, const char *mem, size_t mem_len) {
assert(mem_len != SIZE_MAX);
size_t old_len = buf_len(buf);
buf_resize(buf, old_len + mem_len);
safe_memcpy(buf_ptr(buf) + old_len, mem, mem_len);
memcpy(buf_ptr(buf) + old_len, mem, mem_len);
buf->list.at(buf_len(buf)) = 0;
}

View File

@ -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));
}
@ -6972,12 +6971,6 @@ static void define_builtin_types(CodeGen *g) {
entry->zero_bits = true;
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");
@ -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);

View File

@ -48,7 +48,8 @@ void codegen_print_timing_report(CodeGen *g, FILE *f);
void codegen_link(CodeGen *g);
void codegen_build_and_link(CodeGen *g);
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);
void codegen_add_assembly(CodeGen *g, Buf *path);
void codegen_add_object(CodeGen *g, Buf *object_path);

View File

@ -258,7 +258,6 @@ static bool types_have_same_zig_comptime_repr(ZigType *a, ZigType *b) {
case ZigTypeIdPointer:
case ZigTypeIdUndefined:
case ZigTypeIdNull:
case ZigTypeIdNamespace:
case ZigTypeIdBoundFn:
case ZigTypeIdErrorSet:
case ZigTypeIdOpaque:
@ -1123,11 +1122,11 @@ static IrInstruction *ir_create_const_fn(IrBuilder *irb, Scope *scope, AstNode *
return &const_instruction->base;
}
static IrInstruction *ir_build_const_import(IrBuilder *irb, Scope *scope, AstNode *source_node, ImportTableEntry *import) {
static IrInstruction *ir_build_const_import(IrBuilder *irb, Scope *scope, AstNode *source_node, ZigType *import) {
IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
const_instruction->base.value.type = irb->codegen->builtin_types.entry_namespace;
const_instruction->base.value.type = irb->codegen->builtin_types.entry_type;
const_instruction->base.value.special = ConstValSpecialStatic;
const_instruction->base.value.data.x_import = import;
const_instruction->base.value.data.x_type = import;
return &const_instruction->base;
}
@ -3824,7 +3823,7 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
if (tld)
return ir_build_decl_ref(irb, scope, node, tld, lval);
if (node->owner->any_imports_failed) {
if (get_container_scope(node->owner)->any_imports_failed) {
// skip the error message since we had a failing import in this file
// if an import breaks we don't need redundant undeclared identifier errors
return irb->codegen->invalid_instruction;
@ -6609,20 +6608,32 @@ static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *o
return true;
}
static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name, AstNode *source_node) {
static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name,
Scope *scope, AstNode *source_node, Buf *out_bare_name)
{
if (exec->name) {
return exec->name;
ZigType *import = get_scope_import(scope);
Buf *namespace_name = buf_create_from_buf(&import->name);
if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
buf_append_buf(namespace_name, exec->name);
buf_init_from_buf(out_bare_name, exec->name);
return namespace_name;
} else if (exec->name_fn != nullptr) {
Buf *name = buf_alloc();
buf_append_buf(name, &exec->name_fn->symbol_name);
buf_appendf(name, "(");
render_instance_name_recursive(codegen, name, &exec->name_fn->fndef_scope->base, exec->begin_scope);
buf_appendf(name, ")");
buf_init_from_buf(out_bare_name, name);
return name;
} else {
//Note: C-imports do not have valid location information
return buf_sprintf("(anonymous %s at %s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ")", kind_name,
(source_node->owner->path != nullptr) ? buf_ptr(source_node->owner->path) : "(null)", source_node->line + 1, source_node->column + 1);
ZigType *import = get_scope_import(scope);
Buf *namespace_name = buf_create_from_buf(&import->name);
if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
buf_appendf(namespace_name, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize, kind_name,
source_node->line + 1, source_node->column + 1);
buf_init_from_buf(out_bare_name, namespace_name);
return namespace_name;
}
}
@ -6630,24 +6641,23 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope,
assert(node->type == NodeTypeContainerDecl);
ContainerKind kind = node->data.container_decl.kind;
Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), node);
VisibMod visib_mod = VisibModPub;
TldContainer *tld_container = allocate<TldContainer>(1);
init_tld(&tld_container->base, TldIdContainer, name, visib_mod, node, parent_scope);
Buf *bare_name = buf_alloc();
Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), parent_scope, node, bare_name);
ContainerLayout layout = node->data.container_decl.layout;
ZigType *container_type = get_partial_container_type(irb->codegen, parent_scope,
kind, node, buf_ptr(name), layout);
kind, node, buf_ptr(name), bare_name, layout);
ScopeDecls *child_scope = get_container_scope(container_type);
tld_container->type_entry = container_type;
tld_container->decls_scope = child_scope;
for (size_t i = 0; i < node->data.container_decl.decls.length; i += 1) {
AstNode *child_node = node->data.container_decl.decls.at(i);
scan_decls(irb->codegen, child_scope, child_node);
}
TldContainer *tld_container = allocate<TldContainer>(1);
init_tld(&tld_container->base, TldIdContainer, bare_name, VisibModPub, node, parent_scope);
tld_container->type_entry = container_type;
tld_container->decls_scope = child_scope;
irb->codegen->resolve_queue.append(&tld_container->base);
// Add this to the list to mark as invalid if analyzing this exec fails.
@ -6732,7 +6742,8 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A
uint32_t err_count = node->data.err_set_decl.decls.length;
Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error set", node);
Buf bare_name = BUF_INIT;
Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error", parent_scope, node, &bare_name);
ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
buf_init_from_buf(&err_set_type->name, type_name);
err_set_type->data.error_set.err_count = err_count;
@ -12065,7 +12076,6 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *
case ZigTypeIdErrorSet:
case ZigTypeIdFn:
case ZigTypeIdOpaque:
case ZigTypeIdNamespace:
case ZigTypeIdBoundFn:
case ZigTypeIdArgTuple:
case ZigTypeIdPromise:
@ -13407,7 +13417,6 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
case ZigTypeIdOptional:
case ZigTypeIdErrorUnion:
case ZigTypeIdErrorSet:
case ZigTypeIdNamespace:
case ZigTypeIdBoundFn:
case ZigTypeIdArgTuple:
case ZigTypeIdOpaque:
@ -13432,7 +13441,6 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
case ZigTypeIdErrorSet:
case ZigTypeIdVector:
zig_panic("TODO export const value of type %s", buf_ptr(&target->value.type->name));
case ZigTypeIdNamespace:
case ZigTypeIdBoundFn:
case ZigTypeIdArgTuple:
case ZigTypeIdOpaque:
@ -14616,7 +14624,6 @@ static IrInstruction *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_
case ZigTypeIdEnum:
case ZigTypeIdUnion:
case ZigTypeIdFn:
case ZigTypeIdNamespace:
case ZigTypeIdBoundFn:
case ZigTypeIdArgTuple:
case ZigTypeIdPromise:
@ -15368,7 +15375,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
auto entry = container_scope->decl_table.maybe_get(field_name);
Tld *tld = entry ? entry->value : nullptr;
if (tld && tld->id == TldIdFn) {
resolve_top_level_decl(ira->codegen, tld, false, source_instr->source_node);
resolve_top_level_decl(ira->codegen, tld, source_instr->source_node);
if (tld->resolution == TldResolutionInvalid)
return ira->codegen->invalid_instruction;
TldFn *tld_fn = (TldFn *)tld;
@ -15557,8 +15564,7 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,
static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
bool pointer_only = false;
resolve_top_level_decl(ira->codegen, tld, pointer_only, source_instruction->source_node);
resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);
if (tld->resolution == TldResolutionInvalid)
return ira->codegen->invalid_instruction;
@ -15735,12 +15741,17 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
}
}
ScopeDecls *container_scope = get_container_scope(child_type);
if (container_scope != nullptr) {
auto entry = container_scope->decl_table.maybe_get(field_name);
Tld *tld = entry ? entry->value : nullptr;
Tld *tld = find_container_decl(ira->codegen, container_scope, field_name);
if (tld) {
return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, tld);
if (tld->visib_mod == VisibModPrivate &&
tld->import != get_scope_import(field_ptr_instruction->base.scope))
{
ErrorMsg *msg = ir_add_error(ira, &field_ptr_instruction->base,
buf_sprintf("'%s' is private", buf_ptr(field_name)));
add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("declared here"));
return ira->codegen->invalid_instruction;
}
return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, tld);
}
if (child_type->id == ZigTypeIdUnion &&
(child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr ||
@ -15758,9 +15769,11 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
}
}
const char *container_name = (child_type == ira->codegen->root_import) ?
"root source file" : buf_ptr(buf_sprintf("container '%s'", buf_ptr(&child_type->name)));
ir_add_error(ira, &field_ptr_instruction->base,
buf_sprintf("container '%s' has no member called '%s'",
buf_ptr(&child_type->name), buf_ptr(field_name)));
buf_sprintf("%s has no member called '%s'",
container_name, buf_ptr(field_name)));
return ira->codegen->invalid_instruction;
} else if (child_type->id == ZigTypeIdErrorSet) {
ErrorTableEntry *err_entry;
@ -15971,37 +15984,6 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
buf_sprintf("type '%s' does not support field access", buf_ptr(&child_type->name)));
return ira->codegen->invalid_instruction;
}
} else if (container_type->id == ZigTypeIdNamespace) {
assert(container_ptr->value.type->id == ZigTypeIdPointer);
ConstExprValue *container_ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);
if (!container_ptr_val)
return ira->codegen->invalid_instruction;
ConstExprValue *namespace_val = const_ptr_pointee(ira, ira->codegen, container_ptr_val,
field_ptr_instruction->base.source_node);
if (namespace_val == nullptr)
return ira->codegen->invalid_instruction;
assert(namespace_val->special == ConstValSpecialStatic);
ImportTableEntry *namespace_import = namespace_val->data.x_import;
Tld *tld = find_decl(ira->codegen, &namespace_import->decls_scope->base, field_name);
if (tld) {
if (tld->visib_mod == VisibModPrivate &&
tld->import != source_node->owner)
{
ErrorMsg *msg = ir_add_error_node(ira, source_node,
buf_sprintf("'%s' is private", buf_ptr(field_name)));
add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("declared here"));
return ira->codegen->invalid_instruction;
}
return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, tld);
} else {
const char *import_name = namespace_import->path ? buf_ptr(namespace_import->path) : "(C import)";
ir_add_error_node(ira, source_node,
buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name), import_name));
return ira->codegen->invalid_instruction;
}
} else {
ir_add_error_node(ira, field_ptr_instruction->base.source_node,
buf_sprintf("type '%s' does not support field access", buf_ptr(&container_type->name)));
@ -16281,7 +16263,6 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
case ZigTypeIdEnum:
case ZigTypeIdUnion:
case ZigTypeIdFn:
case ZigTypeIdNamespace:
case ZigTypeIdBoundFn:
case ZigTypeIdPromise:
case ZigTypeIdVector:
@ -16402,7 +16383,6 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
case ZigTypeIdEnum:
case ZigTypeIdUnion:
case ZigTypeIdFn:
case ZigTypeIdNamespace:
case ZigTypeIdBoundFn:
case ZigTypeIdPromise:
case ZigTypeIdVector:
@ -16452,7 +16432,6 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira,
case ZigTypeIdComptimeInt:
case ZigTypeIdBoundFn:
case ZigTypeIdMetaType:
case ZigTypeIdNamespace:
case ZigTypeIdArgTuple:
case ZigTypeIdOpaque:
ir_add_error_node(ira, size_of_instruction->base.source_node,
@ -16860,7 +16839,6 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
case ZigTypeIdPointer:
case ZigTypeIdPromise:
case ZigTypeIdFn:
case ZigTypeIdNamespace:
case ZigTypeIdErrorSet: {
if (pointee_val) {
IrInstruction *result = ir_const(ira, &switch_target_instruction->base, nullptr);
@ -17009,25 +16987,29 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio
return ira->codegen->invalid_instruction;
AstNode *source_node = import_instruction->base.source_node;
ImportTableEntry *import = source_node->owner;
ZigType *import = source_node->owner;
Buf *import_target_path;
Buf *search_dir;
assert(import->package);
PackageTableEntry *target_package;
auto package_entry = import->package->package_table.maybe_get(import_target_str);
assert(import->data.structure.root_struct->package);
ZigPackage *target_package;
auto package_entry = import->data.structure.root_struct->package->package_table.maybe_get(import_target_str);
SourceKind source_kind;
if (package_entry) {
target_package = package_entry->value;
import_target_path = &target_package->root_src_path;
search_dir = &target_package->root_src_dir;
source_kind = SourceKindPkgMain;
} else {
// try it as a filename
target_package = import->package;
target_package = import->data.structure.root_struct->package;
import_target_path = import_target_str;
// search relative to importing file
search_dir = buf_alloc();
os_path_dirname(import->path, search_dir);
os_path_dirname(import->data.structure.root_struct->path, search_dir);
source_kind = SourceKindNonRoot;
}
Buf full_path = BUF_INIT;
@ -17041,10 +17023,7 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio
auto import_entry = ira->codegen->import_table.maybe_get(resolved_path);
if (import_entry) {
IrInstruction *result = ir_const(ira, &import_instruction->base,
ira->codegen->builtin_types.entry_namespace);
result->value.data.x_import = import_entry->value;
return result;
return ir_const_type(ira, &import_instruction->base, import_entry->value);
}
if ((err = file_fetch(ira->codegen, resolved_path, import_code))) {
@ -17059,13 +17038,9 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio
}
}
ImportTableEntry *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code);
ZigType *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code, source_kind);
scan_import(ira->codegen, target_import);
IrInstruction *result = ir_const(ira, &import_instruction->base, ira->codegen->builtin_types.entry_namespace);
result->value.data.x_import = target_import;
return result;
return ir_const_type(ira, &import_instruction->base, target_import);
}
static IrInstruction *ir_analyze_instruction_ref(IrAnalyze *ira, IrInstructionRef *ref_instruction) {
@ -17398,17 +17373,7 @@ static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira,
return ira->codegen->invalid_instruction;
ErrorMsg *msg = ir_add_error(ira, &instruction->base, msg_buf);
size_t i = ira->codegen->tld_ref_source_node_stack.length;
for (;;) {
if (i == 0)
break;
i -= 1;
AstNode *source_node = ira->codegen->tld_ref_source_node_stack.at(i);
if (source_node) {
add_error_note(ira->codegen, msg, source_node,
buf_sprintf("referenced here"));
}
}
emit_error_notes_for_ref_stack(ira->codegen, msg);
return ira->codegen->invalid_instruction;
}
@ -17665,6 +17630,12 @@ static IrInstruction *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira,
return ir_const_unsigned(ira, &instruction->base, bit_offset);
}
static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *source_instr) {
ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected"));
emit_error_notes_for_ref_stack(ira->codegen, msg);
return ira->codegen->invalid_instruction;
}
static void ensure_field_index(ZigType *type, const char *field_name, size_t index) {
Buf *field_name_buf;
@ -17711,7 +17682,9 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig
return var->const_value->data.x_type;
}
static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, ScopeDecls *decls_scope) {
static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr, ConstExprValue *out_val,
ScopeDecls *decls_scope)
{
Error err;
ZigType *type_info_definition_type = ir_type_info_get_type(ira, "Definition", nullptr);
if ((err = ensure_complete_type(ira->codegen, type_info_definition_type)))
@ -17741,7 +17714,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
while ((curr_entry = decl_it.next()) != nullptr) {
// If the definition is unresolved, force it to be resolved again.
if (curr_entry->value->resolution == TldResolutionUnresolved) {
resolve_top_level_decl(ira->codegen, curr_entry->value, false, curr_entry->value->source_node);
resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node);
if (curr_entry->value->resolution != TldResolutionOk) {
return ErrorSemanticAnalyzeFail;
}
@ -17831,6 +17804,11 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
assert(!fn_entry->is_test);
if (fn_entry->type_entry == nullptr) {
ir_error_dependency_loop(ira, source_instr);
return ErrorSemanticAnalyzeFail;
}
AstNodeFnProto *fn_node = (AstNodeFnProto *)(fn_entry->proto_node);
ConstExprValue *fn_def_val = create_const_vals(1);
@ -18036,7 +18014,9 @@ static void make_enum_field_val(IrAnalyze *ira, ConstExprValue *enum_field_val,
enum_field_val->data.x_struct.fields = inner_fields;
}
static Error ir_make_type_info_value(IrAnalyze *ira, AstNode *source_node, ZigType *type_entry, ConstExprValue **out) {
static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr, ZigType *type_entry,
ConstExprValue **out)
{
Error err;
assert(type_entry != nullptr);
assert(!type_is_invalid(type_entry));
@ -18056,7 +18036,6 @@ static Error ir_make_type_info_value(IrAnalyze *ira, AstNode *source_node, ZigTy
case ZigTypeIdComptimeInt:
case ZigTypeIdUndefined:
case ZigTypeIdNull:
case ZigTypeIdNamespace:
case ZigTypeIdArgTuple:
case ZigTypeIdOpaque:
*out = nullptr;
@ -18248,8 +18227,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, AstNode *source_node, ZigTy
}
// defs: []TypeInfo.Definition
ensure_field_index(result->type, "defs", 3);
if ((err = ir_make_type_info_defs(ira, &fields[3], type_entry->data.enumeration.decls_scope)))
if ((err = ir_make_type_info_defs(ira, source_instr, &fields[3],
type_entry->data.enumeration.decls_scope)))
{
return err;
}
break;
}
@ -18266,11 +18248,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, AstNode *source_node, ZigTy
ensure_field_index(result->type, "errors", 0);
ZigType *type_info_error_type = ir_type_info_get_type(ira, "Error", nullptr);
if (!resolve_inferred_error_set(ira->codegen, type_entry, source_node)) {
if (!resolve_inferred_error_set(ira->codegen, type_entry, source_instr->source_node)) {
return ErrorSemanticAnalyzeFail;
}
if (type_is_global_error_set(type_entry)) {
ir_add_error_node(ira, source_node,
ir_add_error(ira, source_instr,
buf_sprintf("TODO: compiler bug: implement @typeInfo support for anyerror. https://github.com/ziglang/zig/issues/1936"));
return ErrorSemanticAnalyzeFail;
}
@ -18412,8 +18394,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, AstNode *source_node, ZigTy
}
// defs: []TypeInfo.Definition
ensure_field_index(result->type, "defs", 3);
if ((err = ir_make_type_info_defs(ira, &fields[3], type_entry->data.unionation.decls_scope)))
if ((err = ir_make_type_info_defs(ira, source_instr, &fields[3],
type_entry->data.unionation.decls_scope)))
{
return err;
}
break;
}
@ -18487,8 +18472,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, AstNode *source_node, ZigTy
}
// defs: []TypeInfo.Definition
ensure_field_index(result->type, "defs", 2);
if ((err = ir_make_type_info_defs(ira, &fields[2], type_entry->data.structure.decls_scope)))
if ((err = ir_make_type_info_defs(ira, source_instr, &fields[2],
type_entry->data.structure.decls_scope)))
{
return err;
}
break;
}
@ -18600,7 +18588,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, AstNode *source_node, ZigTy
{
ZigType *fn_type = type_entry->data.bound_fn.fn_type;
assert(fn_type->id == ZigTypeIdFn);
if ((err = ir_make_type_info_value(ira, source_node, fn_type, &result)))
if ((err = ir_make_type_info_value(ira, source_instr, fn_type, &result)))
return err;
break;
@ -18625,7 +18613,7 @@ static IrInstruction *ir_analyze_instruction_type_info(IrAnalyze *ira,
ZigType *result_type = ir_type_info_get_type(ira, nullptr, nullptr);
ConstExprValue *payload;
if ((err = ir_make_type_info_value(ira, instruction->base.source_node, type_entry, &payload)))
if ((err = ir_make_type_info_value(ira, &instruction->base, type_entry, &payload)))
return ira->codegen->invalid_instruction;
IrInstruction *result = ir_const(ira, &instruction->base, result_type);
@ -18680,7 +18668,7 @@ static IrInstruction *ir_analyze_instruction_type_name(IrAnalyze *ira, IrInstruc
return ira->codegen->invalid_instruction;
if (!type_entry->cached_const_name_val) {
type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, &type_entry->name);
type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, type_bare_name(type_entry));
}
IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
copy_const_val(&result->value, type_entry->cached_const_name_val, true);
@ -18702,14 +18690,21 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
if (type_is_invalid(cimport_result->type))
return ira->codegen->invalid_instruction;
ImportTableEntry *child_import = allocate<ImportTableEntry>(1);
child_import->decls_scope = create_decls_scope(ira->codegen, node, nullptr, nullptr, child_import);
child_import->c_import_node = node;
child_import->package = new_anonymous_package();
child_import->package->package_table.put(buf_create_from_str("builtin"), ira->codegen->compile_var_package);
child_import->package->package_table.put(buf_create_from_str("std"), ira->codegen->std_package);
child_import->di_file = ZigLLVMCreateFile(ira->codegen->dbuilder,
ZigPackage *cur_scope_pkg = scope_package(instruction->base.scope);
Buf *namespace_name = buf_sprintf("%s.cimport:%" ZIG_PRI_usize ":%" ZIG_PRI_usize,
buf_ptr(&cur_scope_pkg->pkg_path), node->line + 1, node->column + 1);
RootStruct *root_struct = allocate<RootStruct>(1);
root_struct->package = new_anonymous_package();
root_struct->package->package_table.put(buf_create_from_str("builtin"), ira->codegen->compile_var_package);
root_struct->package->package_table.put(buf_create_from_str("std"), ira->codegen->std_package);
root_struct->c_import_node = node;
// TODO create namespace_name file in zig-cache instead of /tmp and use it
// for this DIFile
root_struct->di_file = ZigLLVMCreateFile(ira->codegen->dbuilder,
buf_ptr(buf_create_from_str("cimport.h")), buf_ptr(buf_create_from_str(".")));
ZigType *child_import = get_root_container_type(ira->codegen, buf_ptr(namespace_name),
namespace_name, root_struct);
ZigList<ErrorMsg *> errors = {0};
@ -18738,14 +18733,12 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
if (ira->codegen->verbose_cimport) {
fprintf(stderr, "\nC imports:\n");
fprintf(stderr, "-----------\n");
ast_render(ira->codegen, stderr, child_import->root, 4);
ast_render(ira->codegen, stderr, child_import->data.structure.decl_node, 4);
}
scan_decls(ira->codegen, child_import->decls_scope, child_import->root);
scan_decls(ira->codegen, get_container_scope(child_import), child_import->data.structure.decl_node);
IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_namespace);
result->value.data.x_import = child_import;
return result;
return ir_const_type(ira, &instruction->base, child_import);
}
static IrInstruction *ir_analyze_instruction_c_include(IrAnalyze *ira, IrInstructionCInclude *instruction) {
@ -18819,10 +18812,10 @@ static IrInstruction *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstru
if (!rel_file_path)
return ira->codegen->invalid_instruction;
ImportTableEntry *import = get_scope_import(instruction->base.scope);
ZigType *import = get_scope_import(instruction->base.scope);
// figure out absolute path to resource
Buf source_dir_path = BUF_INIT;
os_path_dirname(import->path, &source_dir_path);
os_path_dirname(import->data.structure.root_struct->path, &source_dir_path);
Buf *resolve_paths[] = {
&source_dir_path,
@ -20179,7 +20172,6 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
case ZigTypeIdComptimeInt:
case ZigTypeIdUndefined:
case ZigTypeIdNull:
case ZigTypeIdNamespace:
case ZigTypeIdBoundFn:
case ZigTypeIdArgTuple:
case ZigTypeIdVoid:
@ -21025,7 +21017,6 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
case ZigTypeIdOpaque:
case ZigTypeIdBoundFn:
case ZigTypeIdArgTuple:
case ZigTypeIdNamespace:
case ZigTypeIdUnreachable:
case ZigTypeIdComptimeFloat:
case ZigTypeIdComptimeInt:
@ -21185,7 +21176,6 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
case ZigTypeIdOpaque:
case ZigTypeIdBoundFn:
case ZigTypeIdArgTuple:
case ZigTypeIdNamespace:
case ZigTypeIdUnreachable:
case ZigTypeIdComptimeFloat:
case ZigTypeIdComptimeInt:
@ -21343,7 +21333,6 @@ static bool type_can_bit_cast(ZigType *t) {
case ZigTypeIdOpaque:
case ZigTypeIdBoundFn:
case ZigTypeIdArgTuple:
case ZigTypeIdNamespace:
case ZigTypeIdUnreachable:
case ZigTypeIdComptimeFloat:
case ZigTypeIdComptimeInt:
@ -21516,7 +21505,7 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
Tld *tld = instruction->tld;
LVal lval = instruction->lval;
resolve_top_level_decl(ira->codegen, tld, lval == LValPtr, instruction->base.source_node);
resolve_top_level_decl(ira->codegen, tld, instruction->base.source_node);
if (tld->resolution == TldResolutionInvalid)
return ira->codegen->invalid_instruction;
@ -21528,6 +21517,10 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
TldVar *tld_var = (TldVar *)tld;
ZigVar *var = tld_var->var;
if (var == nullptr) {
return ir_error_dependency_loop(ira, &instruction->base);
}
IrInstruction *var_ptr = ir_get_var_ptr(ira, &instruction->base, var);
if (type_is_invalid(var_ptr->value.type))
return ira->codegen->invalid_instruction;
@ -21664,9 +21657,11 @@ static IrInstruction *ir_analyze_instruction_align_cast(IrAnalyze *ira, IrInstru
}
static IrInstruction *ir_analyze_instruction_opaque_type(IrAnalyze *ira, IrInstructionOpaqueType *instruction) {
Buf *name = get_anon_type_name(ira->codegen, ira->new_irb.exec, "opaque", instruction->base.source_node);
Buf *bare_name = buf_alloc();
Buf *full_name = get_anon_type_name(ira->codegen, ira->new_irb.exec, "opaque",
instruction->base.scope, instruction->base.source_node, bare_name);
ZigType *result_type = get_opaque_type(ira->codegen, instruction->base.scope, instruction->base.source_node,
buf_ptr(name));
buf_ptr(full_name), bare_name);
return ir_const_type(ira, &instruction->base, result_type);
}

View File

@ -215,7 +215,7 @@ struct CliPkg {
CliPkg *parent;
};
static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
static void add_package(CodeGen *g, CliPkg *cli_pkg, ZigPackage *pkg) {
for (size_t i = 0; i < cli_pkg->children.length; i += 1) {
CliPkg *child_cli_pkg = cli_pkg->children.at(i);
@ -223,10 +223,11 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
Buf *basename = buf_alloc();
os_path_split(buf_create_from_str(child_cli_pkg->path), dirname, basename);
PackageTableEntry *child_pkg = codegen_create_package(g, buf_ptr(dirname), buf_ptr(basename));
ZigPackage *child_pkg = codegen_create_package(g, buf_ptr(dirname), buf_ptr(basename),
buf_ptr(buf_sprintf("%s.%s", buf_ptr(&pkg->pkg_path), child_cli_pkg->name)));
auto entry = pkg->package_table.put_unique(buf_create_from_str(child_cli_pkg->name), child_pkg);
if (entry) {
PackageTableEntry *existing_pkg = entry->value;
ZigPackage *existing_pkg = entry->value;
Buf *full_path = buf_alloc();
os_path_join(&existing_pkg->root_src_dir, &existing_pkg->root_src_path, full_path);
fprintf(stderr, "Unable to add package '%s'->'%s': already exists as '%s'\n",
@ -543,8 +544,8 @@ int main(int argc, char **argv) {
return EXIT_FAILURE;
}
PackageTableEntry *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname),
buf_ptr(&build_file_basename));
ZigPackage *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname),
buf_ptr(&build_file_basename), "std.special");
g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);
g->enable_cache = get_cache_opt(enable_cache, true);
codegen_build_and_link(g);
@ -1145,7 +1146,7 @@ int main(int argc, char **argv) {
}
} else if (cmd == CmdTranslateC) {
codegen_translate_c(g, in_file_buf);
ast_render(g, stdout, g->root_import->root, 4);
ast_render(g, stdout, g->root_import->data.structure.decl_node, 4);
if (timing_info)
codegen_print_timing_report(g, stdout);
return EXIT_SUCCESS;

View File

@ -264,7 +264,7 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {
buf_append_buf(out_full_path, basename);
}
int os_path_real(Buf *rel_path, Buf *out_abs_path) {
Error os_path_real(Buf *rel_path, Buf *out_abs_path) {
#if defined(ZIG_OS_WINDOWS)
buf_resize(out_abs_path, 4096);
if (_fullpath(buf_ptr(out_abs_path), buf_ptr(rel_path), buf_len(out_abs_path)) == nullptr) {

View File

@ -96,7 +96,7 @@ void os_path_dirname(Buf *full_path, Buf *out_dirname);
void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename);
void os_path_extname(Buf *full_path, Buf *out_basename, Buf *out_extname);
void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path);
int os_path_real(Buf *rel_path, Buf *out_abs_path);
Error os_path_real(Buf *rel_path, Buf *out_abs_path);
Buf os_path_resolve(Buf **paths_ptr, size_t paths_len);
bool os_path_is_absolute(Buf *path);

View File

@ -18,7 +18,7 @@ struct ParseContext {
Buf *buf;
size_t current_token;
ZigList<Token> *tokens;
ImportTableEntry *owner;
ZigType *owner;
ErrColor err_color;
};
@ -130,8 +130,10 @@ static void ast_error(ParseContext *pc, Token *token, const char *format, ...) {
va_end(ap);
ErrorMsg *err = err_msg_create_with_line(pc->owner->path, token->start_line, token->start_column,
pc->owner->source_code, pc->owner->line_offsets, msg);
ErrorMsg *err = err_msg_create_with_line(pc->owner->data.structure.root_struct->path,
token->start_line, token->start_column,
pc->owner->data.structure.root_struct->source_code,
pc->owner->data.structure.root_struct->line_offsets, msg);
err->line_start = token->start_line;
err->column_start = token->start_column;
@ -148,8 +150,10 @@ static void ast_asm_error(ParseContext *pc, AstNode *node, size_t offset, const
Buf *msg = buf_vprintf(format, ap);
va_end(ap);
ErrorMsg *err = err_msg_create_with_line(pc->owner->path, node->line, node->column,
pc->owner->source_code, pc->owner->line_offsets, msg);
ErrorMsg *err = err_msg_create_with_line(pc->owner->data.structure.root_struct->path,
node->line, node->column,
pc->owner->data.structure.root_struct->source_code,
pc->owner->data.structure.root_struct->line_offsets, msg);
print_err_msg(err, pc->err_color);
exit(EXIT_FAILURE);
@ -570,9 +574,7 @@ static void ast_parse_asm_template(ParseContext *pc, AstNode *node) {
}
}
AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner,
ErrColor err_color)
{
AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens, ZigType *owner, ErrColor err_color) {
ParseContext pc = {};
pc.err_color = err_color;
pc.owner = owner;

View File

@ -16,7 +16,7 @@ ATTRIBUTE_PRINTF(2, 3)
void ast_token_error(Token *token, const char *format, ...);
AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color);
AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ZigType *owner, ErrColor err_color);
void ast_print(AstNode *node, int indent);

View File

@ -76,7 +76,7 @@ struct TransScopeWhile {
};
struct Context {
ImportTableEntry *import;
ZigType *import;
ZigList<ErrorMsg *> *errors;
VisibMod visib_mod;
bool want_export;
@ -4732,7 +4732,7 @@ static void process_preprocessor_entities(Context *c, ZigClangASTUnit *zunit) {
}
}
Error parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
Error parse_h_buf(ZigType *import, ZigList<ErrorMsg *> *errors, Buf *source,
CodeGen *codegen, AstNode *source_node)
{
Error err;
@ -4748,7 +4748,7 @@ Error parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *so
return err;
}
Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,
Error parse_h_file(ZigType *import, ZigList<ErrorMsg *> *errors, const char *target_file,
CodeGen *codegen, AstNode *source_node)
{
Context context = {0};
@ -4958,7 +4958,8 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
render_macros(c);
render_aliases(c);
import->root = c->root;
import->data.structure.decl_node = c->root;
import->data.structure.decls_scope->base.source_node = c->root;
return ErrorNone;
}

View File

@ -11,10 +11,10 @@
#include "all_types.hpp"
Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,
Error parse_h_file(ZigType *import, ZigList<ErrorMsg *> *errors, const char *target_file,
CodeGen *codegen, AstNode *source_node);
Error parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
Error parse_h_buf(ZigType *import, ZigList<ErrorMsg *> *errors, Buf *source,
CodeGen *codegen, AstNode *source_node);
#endif

View File

@ -93,18 +93,6 @@ ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate(size_t count) {
return ptr;
}
template<typename T>
static inline void safe_memcpy(T *dest, const T *src, size_t count) {
#ifdef NDEBUG
memcpy(dest, src, count * sizeof(T));
#else
// manually assign every elment to trigger compile error for non-copyable structs
for (size_t i = 0; i < count; i += 1) {
dest[i] = src[i];
}
#endif
}
template<typename T>
static inline T *reallocate(T *old, size_t old_count, size_t new_count) {
T *ptr = reallocate_nonzero(old, old_count, new_count);

View File

@ -34,8 +34,8 @@ pub const Blake2s256 = Blake2s(256);
fn Blake2s(comptime out_len: usize) type {
return struct {
const Self = @This();
const block_length = 64;
const digest_length = out_len / 8;
pub const block_length = 64;
pub const digest_length = out_len / 8;
const iv = [8]u32{
0x6A09E667,

View File

@ -29,8 +29,8 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, k: usize, s: u32, t: u32) RoundPar
pub const Md5 = struct {
const Self = @This();
const block_length = 64;
const digest_length = 16;
pub const block_length = 64;
pub const digest_length = 16;
s: [4]u32,
// Streaming Cache

View File

@ -26,8 +26,8 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, e: usize, i: u32) RoundParam {
pub const Sha1 = struct {
const Self = @This();
const block_length = 64;
const digest_length = 20;
pub const block_length = 64;
pub const digest_length = 20;
s: [5]u32,
// Streaming Cache

View File

@ -78,8 +78,8 @@ pub const Sha256 = Sha2_32(Sha256Params);
fn Sha2_32(comptime params: Sha2Params32) type {
return struct {
const Self = @This();
const block_length = 64;
const digest_length = params.out_len / 8;
pub const block_length = 64;
pub const digest_length = params.out_len / 8;
s: [8]u32,
// Streaming Cache

View File

@ -13,8 +13,8 @@ pub const Sha3_512 = Keccak(512, 0x06);
fn Keccak(comptime bits: usize, comptime delim: u8) type {
return struct {
const Self = @This();
const block_length = 200;
const digest_length = bits / 8;
pub const block_length = 200;
pub const digest_length = bits / 8;
s: [200]u8,
offset: usize,

View File

@ -486,7 +486,6 @@ pub fn autoHash(key: var, comptime rng: *std.rand.Random, comptime HashInt: type
builtin.TypeId.ErrorSet => return autoHash(@errorToInt(key), rng),
builtin.TypeId.Promise, builtin.TypeId.Fn => return autoHash(@ptrToInt(key), rng),
builtin.TypeId.Namespace,
builtin.TypeId.BoundFn,
builtin.TypeId.ComptimeFloat,
builtin.TypeId.ComptimeInt,
@ -532,7 +531,6 @@ pub fn autoEql(a: var, b: @typeOf(a)) bool {
builtin.TypeId.Float,
builtin.TypeId.ComptimeFloat,
builtin.TypeId.ComptimeInt,
builtin.TypeId.Namespace,
builtin.TypeId.Promise,
builtin.TypeId.Enum,
builtin.TypeId.BoundFn,

View File

@ -12,7 +12,6 @@ const ArrayList = std.ArrayList;
// Note: most of this is based on information gathered from LLVM source code,
// documentation and/or contributors.
// https://llvm.org/docs/PDB/DbiStream.html#stream-header
pub const DbiStreamHeader = packed struct {
VersionSignature: i32,
@ -393,10 +392,9 @@ pub const LineNumberEntry = packed struct {
Flags: u32,
/// TODO runtime crash when I make the actual type of Flags this
const Flags = packed struct {
pub const Flags = packed struct {
/// Start line number
Start: u24,
/// Delta of lines to the end of the expression. Still unclear.
// TODO figure out the point of this field.
End: u7,

View File

@ -44,7 +44,6 @@ pub fn expectEqual(expected: var, actual: @typeOf(expected)) void {
TypeId.ComptimeFloat,
TypeId.ComptimeInt,
TypeId.Enum,
TypeId.Namespace,
TypeId.Fn,
TypeId.Promise,
TypeId.Vector,

View File

@ -617,7 +617,7 @@ pub const Node = struct {
pub const DeclList = Root.DeclList;
const InitArg = union(enum) {
pub const InitArg = union(enum) {
None,
Enum: ?*Node,
Type: *Node,
@ -1744,7 +1744,7 @@ pub const Node = struct {
kind: Kind,
rhs: ?*Node,
const Kind = union(enum) {
pub const Kind = union(enum) {
Break: ?*Node,
Continue: ?*Node,
Return,
@ -2022,7 +2022,7 @@ pub const Node = struct {
kind: Kind,
rparen: TokenIndex,
const Kind = union(enum) {
pub const Kind = union(enum) {
Variable: *Identifier,
Return: *Node,
};
@ -2101,9 +2101,9 @@ pub const Node = struct {
clobbers: ClobberList,
rparen: TokenIndex,
const OutputList = SegmentedList(*AsmOutput, 2);
const InputList = SegmentedList(*AsmInput, 2);
const ClobberList = SegmentedList(TokenIndex, 2);
pub const OutputList = SegmentedList(*AsmOutput, 2);
pub const InputList = SegmentedList(*AsmInput, 2);
pub const ClobberList = SegmentedList(TokenIndex, 2);
pub fn iterate(self: *Asm, index: usize) ?*Node {
var i = index;

File diff suppressed because it is too large Load Diff

View File

@ -18,6 +18,7 @@ comptime {
_ = @import("behavior/bugs/1421.zig");
_ = @import("behavior/bugs/1442.zig");
_ = @import("behavior/bugs/1486.zig");
_ = @import("behavior/bugs/1500.zig");
_ = @import("behavior/bugs/1851.zig");
_ = @import("behavior/bugs/2006.zig");
_ = @import("behavior/bugs/394.zig");
@ -25,6 +26,7 @@ comptime {
_ = @import("behavior/bugs/529.zig");
_ = @import("behavior/bugs/655.zig");
_ = @import("behavior/bugs/656.zig");
_ = @import("behavior/bugs/679.zig");
_ = @import("behavior/bugs/704.zig");
_ = @import("behavior/bugs/718.zig");
_ = @import("behavior/bugs/726.zig");

View File

@ -4,16 +4,16 @@ const expect = @import("std").testing.expect;
comptime {
if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {
asm volatile (
\\.globl aoeu;
\\.type aoeu, @function;
\\.set aoeu, derp;
\\.globl this_is_my_alias;
\\.type this_is_my_alias, @function;
\\.set this_is_my_alias, derp;
);
}
}
test "module level assembly" {
if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {
expect(aoeu() == 1234);
expect(this_is_my_alias() == 1234);
}
}
@ -85,7 +85,7 @@ test "sized integer/float in asm input" {
);
}
extern fn aoeu() i32;
extern fn this_is_my_alias() i32;
export fn derp() i32 {
return 1234;

View File

@ -0,0 +1,10 @@
const A = struct {
b: B,
};
const B = fn (A) void;
test "allow these dependencies" {
var a: A = undefined;
var b: B = undefined;
}

View File

@ -0,0 +1,17 @@
const std = @import("std");
const expect = std.testing.expect;
pub fn List(comptime T: type) type {
return u32;
}
const ElementList = List(Element);
const Element = struct {
link: ElementList,
};
test "false dependency loop in struct definition" {
const listType = ElementList;
var x: listType = 42;
expect(x == 42);
}

View File

@ -1,5 +1,6 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqualSlices = std.testing.expectEqualSlices;
const mem = std.mem;
const cstr = std.cstr;
const builtin = @import("builtin");
@ -469,7 +470,7 @@ test "@typeId" {
expect(@typeId(AUnionEnum) == Tid.Union);
expect(@typeId(AUnion) == Tid.Union);
expect(@typeId(fn () void) == Tid.Fn);
expect(@typeId(@typeOf(builtin)) == Tid.Namespace);
expect(@typeId(@typeOf(builtin)) == Tid.Type);
// TODO bound fn
// TODO arg tuple
// TODO opaque
@ -488,7 +489,7 @@ test "@typeName" {
expect(mem.eql(u8, @typeName(i64), "i64"));
expect(mem.eql(u8, @typeName(*usize), "*usize"));
// https://github.com/ziglang/zig/issues/675
expect(mem.eql(u8, @typeName(TypeFromFn(u8)), "TypeFromFn(u8)"));
expectEqualSlices(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8)));
expect(mem.eql(u8, @typeName(Struct), "Struct"));
expect(mem.eql(u8, @typeName(Union), "Union"));
expect(mem.eql(u8, @typeName(Enum), "Enum"));

View File

@ -186,7 +186,7 @@ fn testUnion() void {
expect(TypeId(typeinfo_info) == TypeId.Union);
expect(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);
expect(typeinfo_info.Union.tag_type.? == TypeId);
expect(typeinfo_info.Union.fields.len == 25);
expect(typeinfo_info.Union.fields.len == 24);
expect(typeinfo_info.Union.fields[4].enum_field != null);
expect(typeinfo_info.Union.fields[4].enum_field.?.value == 4);
expect(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int));

View File

@ -569,7 +569,7 @@ pub const CompileErrorContext = struct {
const ErrLineIter = struct {
lines: mem.SplitIterator,
const source_file = ".tmp_source.zig";
const source_file = "tmp.zig";
fn init(input: []const u8) ErrLineIter {
return ErrLineIter{ .lines = mem.separate(input, "\n") };
@ -759,7 +759,7 @@ pub const CompileErrorContext = struct {
.is_test = false,
};
tc.addSourceFile(".tmp_source.zig", source);
tc.addSourceFile("tmp.zig", source);
comptime var arg_i = 0;
inline while (arg_i < expected_lines.len) : (arg_i += 1) {
tc.addExpectedError(expected_lines[arg_i]);
@ -980,15 +980,18 @@ pub const TranslateCContext = struct {
Term.Exited => |code| {
if (code != 0) {
warn("Compilation failed with exit code {}\n", code);
printInvocation(zig_args.toSliceConst());
return error.TestFailed;
}
},
Term.Signal => |code| {
warn("Compilation failed with signal {}\n", code);
printInvocation(zig_args.toSliceConst());
return error.TestFailed;
},
else => {
warn("Compilation terminated unexpectedly\n");
printInvocation(zig_args.toSliceConst());
return error.TestFailed;
},
}