authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-18 17:24:52-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-18 17:24:52-05:00
log37b13bf1512d8eed7308a2194c1935d91a33796c
tree42b6d1d4b6ddd16c7f69c4bdd15677a720609cf3
parente50ced44a2cf6268c19df901ad56b367d8d802fe

hello.zig working with all structs anonymous


10 files changed, 57 insertions(+), 34 deletions(-)

doc/langref.md+1-1
......@@ -151,7 +151,7 @@ GroupedExpression = "(" Expression ")"
151151
152152KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "zeroes" | "error" | "type" | "this"
153153
154ContainerDecl = ("struct" | "enum" | "union") "{" many(StructMember) "}"
154ContainerDecl = option("extern") ("struct" | "enum" | "union") "{" many(StructMember) "}"
155155
156156```
157157
src/all_types.hpp+4
......@@ -598,6 +598,7 @@ struct AstNodeContainerDecl {
598598 ContainerKind kind;
599599 ZigList<AstNode *> fields;
600600 ZigList<AstNode *> decls;
601 bool is_extern;
601602};
602603
603604struct AstNodeStructField {
......@@ -801,6 +802,7 @@ struct TypeStructField {
801802};
802803struct TypeTableEntryStruct {
803804 AstNode *decl_node;
805 bool is_extern;
804806 bool is_packed;
805807 uint32_t src_field_count;
806808 uint32_t gen_field_count;
......@@ -827,6 +829,7 @@ struct TypeTableEntryError {
827829
828830struct TypeTableEntryEnum {
829831 AstNode *decl_node;
832 bool is_extern;
830833 uint32_t src_field_count;
831834 uint32_t gen_field_count;
832835 TypeEnumField *fields;
......@@ -845,6 +848,7 @@ struct TypeTableEntryEnum {
845848
846849struct TypeTableEntryUnion {
847850 AstNode *decl_node;
851 bool is_extern;
848852 uint32_t src_field_count;
849853 uint32_t gen_field_count;
850854 TypeStructField *fields;
src/analyze.cpp+3
......@@ -838,12 +838,15 @@ TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKi
838838 switch (kind) {
839839 case ContainerKindStruct:
840840 entry->data.structure.decl_node = decl_node;
841 entry->data.structure.is_extern = decl_node->data.container_decl.is_extern;
841842 break;
842843 case ContainerKindEnum:
843844 entry->data.enumeration.decl_node = decl_node;
845 entry->data.enumeration.is_extern = decl_node->data.container_decl.is_extern;
844846 break;
845847 case ContainerKindUnion:
846848 entry->data.unionation.decl_node = decl_node;
849 entry->data.unionation.is_extern = decl_node->data.container_decl.is_extern;
847850 break;
848851 }
849852
src/ir.cpp+3
......@@ -8007,6 +8007,9 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru
80078007 TypeTableEntry *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields,
80088008 bool depends_on_compile_var)
80098009{
8010 if (!type_is_complete(container_type))
8011 resolve_container_type(ira->codegen, container_type);
8012
80108013 size_t actual_field_count = container_type->data.structure.src_field_count;
80118014
80128015 IrInstruction *first_non_const_instruction = nullptr;
src/parser.cpp+24-11
......@@ -684,11 +684,6 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo
684684 AstNode *node = ast_create_node(pc, NodeTypeErrorType, token);
685685 *token_index += 1;
686686 return node;
687 } else if (token->id == TokenIdKeywordExtern) {
688 *token_index += 1;
689 AstNode *node = ast_parse_fn_proto(pc, token_index, true, VisibModPrivate);
690 node->data.fn_proto.is_extern = true;
691 return node;
692687 } else if (token->id == TokenIdAtSign) {
693688 *token_index += 1;
694689 Token *name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
......@@ -742,6 +737,13 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo
742737 if (container_decl)
743738 return container_decl;
744739
740 if (token->id == TokenIdKeywordExtern) {
741 *token_index += 1;
742 AstNode *node = ast_parse_fn_proto(pc, token_index, true, VisibModPrivate);
743 node->data.fn_proto.is_extern = true;
744 return node;
745 }
746
745747 if (!mandatory)
746748 return nullptr;
747749
......@@ -2224,28 +2226,39 @@ static AstNode *ast_parse_use(ParseContext *pc, size_t *token_index, VisibMod vi
22242226}
22252227
22262228/*
2227ContainerDecl = ("struct" | "enum" | "union") "{" many(StructMember) "}"
2229ContainerDecl = option("extern") ("struct" | "enum" | "union") "{" many(StructMember) "}"
22282230StructMember = (StructField | FnDef | GlobalVarDecl)
22292231StructField = Symbol option(":" Expression) ",")
22302232*/
22312233static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index, bool mandatory) {
22322234 Token *first_token = &pc->tokens->at(*token_index);
2235 Token *container_kind_token;
2236
2237 bool is_extern;
2238 if (first_token->id == TokenIdKeywordExtern) {
2239 container_kind_token = &pc->tokens->at(*token_index + 1);
2240 is_extern = true;
2241 } else {
2242 container_kind_token = first_token;
2243 is_extern = false;
2244 }
22332245
22342246 ContainerKind kind;
2235 if (first_token->id == TokenIdKeywordStruct) {
2247 if (container_kind_token->id == TokenIdKeywordStruct) {
22362248 kind = ContainerKindStruct;
2237 } else if (first_token->id == TokenIdKeywordEnum) {
2249 } else if (container_kind_token->id == TokenIdKeywordEnum) {
22382250 kind = ContainerKindEnum;
2239 } else if (first_token->id == TokenIdKeywordUnion) {
2251 } else if (container_kind_token->id == TokenIdKeywordUnion) {
22402252 kind = ContainerKindUnion;
22412253 } else if (mandatory) {
2242 ast_invalid_token_error(pc, first_token);
2254 ast_invalid_token_error(pc, container_kind_token);
22432255 } else {
22442256 return nullptr;
22452257 }
2246 *token_index += 1;
2258 *token_index += is_extern ? 2 : 1;
22472259
22482260 AstNode *node = ast_create_node(pc, NodeTypeContainerDecl, first_token);
2261 node->data.container_decl.is_extern = is_extern;
22492262 node->data.container_decl.kind = kind;
22502263
22512264 ast_eat_token(pc, token_index, TokenIdLBrace);
std/cstr.zig+2-2
......@@ -34,7 +34,7 @@ pub fn toSlice(str: &u8) -> []u8 {
3434
3535
3636/// A buffer that allocates memory and maintains a null byte at the end.
37pub struct CBuf {
37pub const CBuf = struct {
3838 list: List(u8),
3939
4040 /// Must deinitialize with deinit.
......@@ -124,7 +124,7 @@ pub struct CBuf {
124124 pub fn startsWithCStr(self: &const CBuf, s: &const u8) -> bool {
125125 self.startsWithMem(s[0...strlen(s)])
126126 }
127}
127};
128128
129129fn testSimpleCBuf() {
130130 @setFnTest(this, true);
std/io.zig+4-4
......@@ -69,7 +69,7 @@ pub const OpenWrite = 0b0010;
6969pub const OpenCreate = 0b0100;
7070pub const OpenTruncate = 0b1000;
7171
72pub struct OutStream {
72pub const OutStream = struct {
7373 fd: i32,
7474 buffer: [buffer_size]u8,
7575 index: usize,
......@@ -153,11 +153,11 @@ pub struct OutStream {
153153 return;
154154 }
155155 }
156}
156};
157157
158158// TODO created a BufferedInStream struct and move some of this code there
159159// BufferedInStream API goes on top of minimal InStream API.
160pub struct InStream {
160pub const InStream = struct {
161161 fd: i32,
162162
163163 /// Call close to clean up.
......@@ -360,7 +360,7 @@ pub struct InStream {
360360
361361 return usize(stat.size);
362362 }
363}
363};
364364
365365pub fn parseUnsigned(inline T: type, buf: []u8, radix: u8) -> %T {
366366 var x: T = 0;
std/linux.zig+8-8
......@@ -342,31 +342,31 @@ pub const socklen_t = u32;
342342pub const in_addr = u32;
343343pub const in6_addr = [16]u8;
344344
345export struct sockaddr {
345pub const sockaddr = extern struct {
346346 family: sa_family_t,
347347 port: u16,
348348 data: [12]u8,
349}
349};
350350
351export struct sockaddr_in {
351pub const sockaddr_in = extern struct {
352352 family: sa_family_t,
353353 port: u16,
354354 addr: in_addr,
355355 zero: [8]u8,
356}
356};
357357
358export struct sockaddr_in6 {
358pub const sockaddr_in6 = extern struct {
359359 family: sa_family_t,
360360 port: u16,
361361 flowinfo: u32,
362362 addr: in6_addr,
363363 scope_id: u32,
364}
364};
365365
366export struct iovec {
366pub const iovec = extern struct {
367367 iov_base: &u8,
368368 iov_len: usize,
369}
369};
370370
371371//
372372//const IF_NAMESIZE = 16;
std/linux_x86_64.zig+6-6
......@@ -442,7 +442,7 @@ pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg
442442 : "rcx", "r11")
443443}
444444
445export struct msghdr {
445pub const msghdr = extern struct {
446446 msg_name: &u8,
447447 msg_namelen: socklen_t,
448448 msg_iov: &iovec,
......@@ -452,9 +452,9 @@ export struct msghdr {
452452 msg_controllen: socklen_t,
453453 __pad2: socklen_t,
454454 msg_flags: i32,
455}
455};
456456
457export struct stat {
457pub const stat = extern struct {
458458 dev: u64,
459459 ino: u64,
460460 nlink: usize,
......@@ -472,9 +472,9 @@ export struct stat {
472472 mtim: timespec,
473473 ctim: timespec,
474474 __unused: [3]isize,
475}
475};
476476
477export struct timespec {
477pub const timespec = extern struct {
478478 tv_sec: isize,
479479 tv_nsec: isize,
480}
480};
std/math.zig+2-2
......@@ -1,8 +1,8 @@
1pub enum Cmp {
1pub const Cmp = enum {
22 Equal,
33 Greater,
44 Less,
5}
5};
66
77pub fn min(x: var, y: var) -> @typeOf(x + y) {
88 if (x < y) x else y