Skip to content

remove the (namespace) type and make every file an empty struct #2020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 1, 2019
25 changes: 14 additions & 11 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -6785,7 +6785,6 @@ pub const TypeId = enum {
Enum,
Union,
Fn,
Namespace,
Block,
BoundFn,
ArgTuple,
Expand Down Expand Up @@ -6820,7 +6819,6 @@ pub const TypeInfo = union(TypeId) {
Enum: Enum,
Union: Union,
Fn: Fn,
Namespace: void,
BoundFn: Fn,
ArgTuple: void,
Opaque: void,
Expand Down Expand Up @@ -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#}.
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {}
Expand Down
2 changes: 1 addition & 1 deletion src-self-hosted/compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 0 additions & 12 deletions src-self-hosted/type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -89,7 +87,6 @@ pub const Type = struct {
Id.ComptimeInt,
Id.Undefined,
Id.Null,
Id.Namespace,
Id.BoundFn,
Id.ArgTuple,
Id.Opaque,
Expand Down Expand Up @@ -123,7 +120,6 @@ pub const Type = struct {
Id.ComptimeInt,
Id.Undefined,
Id.Null,
Id.Namespace,
Id.BoundFn,
Id.ArgTuple,
Id.Opaque,
Expand Down Expand Up @@ -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,

Expand Down
97 changes: 49 additions & 48 deletions src/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "libc_installation.hpp"

struct AstNode;
struct ImportTableEntry;
struct ZigFn;
struct Scope;
struct ScopeBlock;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
};

Expand All @@ -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 {
Expand Down Expand Up @@ -700,7 +697,7 @@ struct AstNodeUse {
AstNode *expr;

TldResolution resolution;
ConstExprValue *value;
ConstExprValue *using_namespace_value;
};

struct AstNodeIfBoolExpr {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1232,7 +1249,6 @@ enum ZigTypeId {
ZigTypeIdEnum,
ZigTypeIdUnion,
ZigTypeIdFn,
ZigTypeIdNamespace,
ZigTypeIdBoundFn,
ZigTypeIdArgTuple,
ZigTypeIdOpaque,
Expand All @@ -1246,6 +1262,10 @@ enum OnePossibleValue {
OnePossibleValueYes,
};

struct ZigTypeOpaque {
Buf *bare_name;
};

struct ZigType {
ZigTypeId id;
Buf name;
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -3476,6 +3475,8 @@ static const size_t stack_trace_ptr_count = 30;
#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,
Expand Down Expand Up @@ -3507,7 +3508,7 @@ struct FnWalkTypes {
};

struct FnWalkVars {
ImportTableEntry *import;
ZigType *import;
LLVMValueRef llvm_fn;
ZigFn *fn;
ZigVar *var;
Expand Down
Loading