authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-14 18:10:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-14 18:10:25-07:00
logf17e20d5fefeb9ce30a08e85c10b9449cf672112
tree2907eae05aca64bbab1e7ca0c9c526c1f204848d
parent7dd29291853a30973c19867385514b6d807cb644

instead of *mut and *const, & and &const

closes #33

14 files changed, 62 insertions(+), 53 deletions(-)

doc/langref.md+2-2
......@@ -60,7 +60,7 @@ ParamDecl : token(Symbol) token(Colon) Type | token(Ellipsis)
6060
6161Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType
6262
63PointerType : token(Star) (token(Const) | token(Mut)) Type
63PointerType : token(Ampersand) option(token(Const)) Type
6464
6565ArrayType : token(LBracket) Type token(Semicolon) Expression token(RBracket)
6666
......@@ -114,7 +114,7 @@ BinaryOrExpression : BinaryXorExpression token(BinOr) BinaryOrExpression | Binar
114114
115115BinaryXorExpression : BinaryAndExpression token(BinXor) BinaryXorExpression | BinaryAndExpression
116116
117BinaryAndExpression : BitShiftExpression token(BinAnd) BinaryAndExpression | BitShiftExpression
117BinaryAndExpression : BitShiftExpression token(Ampersand) BinaryAndExpression | BitShiftExpression
118118
119119BitShiftExpression : AdditionExpression BitShiftOperator BitShiftExpression | AdditionExpression
120120
example/arrays/arrays.zig+2-2
......@@ -2,7 +2,7 @@ export executable "arrays";
22
33use "std.zig";
44
5export fn main(argc: isize, argv: *mut *mut u8, env: *mut *mut u8) -> i32 {
5export fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
66 let mut array : [i32; 5];
77
88 let mut i : i32 = 0;
......@@ -30,7 +30,7 @@ loop_2_start:
3030loop_2_end:
3131
3232 if accumulator == 15 {
33 print_str("OK" as string);
33 print_str("OK\n" as string);
3434 }
3535
3636
example/expressions/expressions.zig+1-1
......@@ -2,7 +2,7 @@ export executable "expressions";
22
33#link("c")
44extern {
5 fn puts(s: *const u8) -> i32;
5 fn puts(s: &const u8) -> i32;
66 fn exit(code: i32) -> unreachable;
77}
88
example/hello_world/hello.zig+1-1
......@@ -2,7 +2,7 @@ export executable "hello";
22
33use "std.zig";
44
5export fn main(argc: isize, argv: *mut *mut u8, env: *mut *mut u8) -> i32 {
5export fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
66 // TODO implicit coercion from array to string
77 print_str("Hello, world!\n" as string);
88 return 0;
example/hello_world/hello_libc.zig+1-1
......@@ -2,7 +2,7 @@ export executable "hello";
22
33#link("c")
44extern {
5 fn printf(__format: *const u8, ...) -> i32;
5 fn printf(__format: &const u8, ...) -> i32;
66 fn exit(__status: i32) -> unreachable;
77}
88
example/multiple_files/libc.zig+1-1
......@@ -1,5 +1,5 @@
11#link("c")
22extern {
3 pub fn puts(s: *const u8) -> i32;
3 pub fn puts(s: &const u8) -> i32;
44 pub fn exit(code: i32) -> unreachable;
55}
example/structs/structs.zig+1-1
......@@ -2,7 +2,7 @@ export executable "structs";
22
33use "std.zig";
44
5export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
5export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
66 let mut foo : Foo;
77
88 foo.a = foo.a + 1;
src/analyze.cpp+1-1
......@@ -111,7 +111,7 @@ TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool
111111 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdPointer);
112112 entry->type_ref = LLVMPointerType(child_type->type_ref, 0);
113113 buf_resize(&entry->name, 0);
114 buf_appendf(&entry->name, "*%s %s", is_const ? "const" : "mut", buf_ptr(&child_type->name));
114 buf_appendf(&entry->name, "&%s%s", is_const ? "const " : "", buf_ptr(&child_type->name));
115115 entry->size_in_bits = g->pointer_size_bytes * 8;
116116 entry->align_in_bits = g->pointer_size_bytes * 8;
117117 entry->di_type = LLVMZigCreateDebugPointerType(g->dbuilder, child_type->di_type,
src/parseh.cpp+4-4
......@@ -250,9 +250,9 @@ static Buf *to_zig_type(ParseH *p, CXType raw_type) {
250250 CXType pointee_type = clang_getArrayElementType(raw_type);
251251 Buf *pointee_buf = to_zig_type(p, pointee_type);
252252 if (clang_isConstQualifiedType(pointee_type)) {
253 return buf_sprintf("*const %s", buf_ptr(pointee_buf));
253 return buf_sprintf("&const %s", buf_ptr(pointee_buf));
254254 } else {
255 return buf_sprintf("*mut %s", buf_ptr(pointee_buf));
255 return buf_sprintf("&%s", buf_ptr(pointee_buf));
256256 }
257257 }
258258 case CXType_Pointer:
......@@ -265,9 +265,9 @@ static Buf *to_zig_type(ParseH *p, CXType raw_type) {
265265 pointee_buf = to_zig_type(p, pointee_type);
266266 }
267267 if (clang_isConstQualifiedType(pointee_type)) {
268 return buf_sprintf("*const %s", buf_ptr(pointee_buf));
268 return buf_sprintf("&const %s", buf_ptr(pointee_buf));
269269 } else {
270 return buf_sprintf("*mut %s", buf_ptr(pointee_buf));
270 return buf_sprintf("&%s", buf_ptr(pointee_buf));
271271 }
272272 }
273273 case CXType_Record:
src/parser.cpp+24-14
......@@ -781,6 +781,7 @@ static AstNode *ast_parse_expression(ParseContext *pc, int *token_index, bool ma
781781static AstNode *ast_parse_block(ParseContext *pc, int *token_index, bool mandatory);
782782static AstNode *ast_parse_if_expr(ParseContext *pc, int *token_index, bool mandatory);
783783static AstNode *ast_parse_block_expr(ParseContext *pc, int *token_index, bool mandatory);
784static AstNode *ast_parse_type(ParseContext *pc, int *token_index);
784785
785786static void ast_expect_token(ParseContext *pc, Token *token, TokenId token_id) {
786787 if (token->id != token_id) {
......@@ -841,10 +842,21 @@ static void ast_parse_directives(ParseContext *pc, int *token_index,
841842 zig_unreachable();
842843}
843844
845static void ast_parse_type_assume_amp(ParseContext *pc, int *token_index, AstNode *node) {
846 node->data.type.type = AstNodeTypeTypePointer;
847 Token *first_type_token = &pc->tokens->at(*token_index);
848 if (first_type_token->id == TokenIdKeywordConst) {
849 node->data.type.is_const = true;
850 *token_index += 1;
851 first_type_token = &pc->tokens->at(*token_index);
852 }
853
854 node->data.type.child_type = ast_parse_type(pc, token_index);
855}
844856
845857/*
846858Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType
847PointerType : token(Star) (token(Const) | token(Mut)) Type
859PointerType : token(Ampersand) option(token(Const)) Type
848860ArrayType : token(LBracket) Type token(Semicolon) token(Number) token(RBracket)
849861*/
850862static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
......@@ -862,19 +874,17 @@ static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
862874 } else if (token->id == TokenIdSymbol) {
863875 node->data.type.type = AstNodeTypeTypePrimitive;
864876 ast_buf_from_token(pc, token, &node->data.type.primitive_name);
865 } else if (token->id == TokenIdStar) {
877 } else if (token->id == TokenIdAmpersand) {
878 ast_parse_type_assume_amp(pc, token_index, node);
879 } else if (token->id == TokenIdBoolAnd) {
880 // Pretend that we got 2 ampersand tokens
866881 node->data.type.type = AstNodeTypeTypePointer;
867 Token *const_or_mut = &pc->tokens->at(*token_index);
868 *token_index += 1;
869 if (const_or_mut->id == TokenIdKeywordMut) {
870 node->data.type.is_const = false;
871 } else if (const_or_mut->id == TokenIdKeywordConst) {
872 node->data.type.is_const = true;
873 } else {
874 ast_invalid_token_error(pc, const_or_mut);
875 }
876882
877 node->data.type.child_type = ast_parse_type(pc, token_index);
883 node->data.type.child_type = ast_create_node_no_line_info(pc, NodeTypeType);
884 node->data.type.child_type->line = token->start_line;
885 node->data.type.child_type->column = token->start_column + 1;
886
887 ast_parse_type_assume_amp(pc, token_index, node->data.type.child_type);
878888 } else if (token->id == TokenIdLBracket) {
879889 node->data.type.type = AstNodeTypeTypeArray;
880890
......@@ -1341,7 +1351,7 @@ static AstNode *ast_parse_bit_shift_expr(ParseContext *pc, int *token_index, boo
13411351
13421352
13431353/*
1344BinaryAndExpression : BitShiftExpression token(BinAnd) BinaryAndExpression | BitShiftExpression
1354BinaryAndExpression : BitShiftExpression token(Ampersand) BinaryAndExpression | BitShiftExpression
13451355*/
13461356static AstNode *ast_parse_bin_and_expr(ParseContext *pc, int *token_index, bool mandatory) {
13471357 AstNode *operand_1 = ast_parse_bit_shift_expr(pc, token_index, mandatory);
......@@ -1350,7 +1360,7 @@ static AstNode *ast_parse_bin_and_expr(ParseContext *pc, int *token_index, bool
13501360
13511361 while (true) {
13521362 Token *token = &pc->tokens->at(*token_index);
1353 if (token->id != TokenIdBinAnd)
1363 if (token->id != TokenIdAmpersand)
13541364 return operand_1;
13551365 *token_index += 1;
13561366
src/tokenizer.cpp+2-2
......@@ -320,7 +320,7 @@ void tokenize(Buf *buf, Tokenization *out) {
320320 t.state = TokenizeStateSawDash;
321321 break;
322322 case '&':
323 begin_token(&t, TokenIdBinAnd);
323 begin_token(&t, TokenIdAmpersand);
324324 t.state = TokenizeStateSawAmpersand;
325325 break;
326326 case '^':
......@@ -832,7 +832,7 @@ static const char * token_name(Token *token) {
832832 case TokenIdDash: return "Dash";
833833 case TokenIdNumberSign: return "NumberSign";
834834 case TokenIdBinOr: return "BinOr";
835 case TokenIdBinAnd: return "BinAnd";
835 case TokenIdAmpersand: return "Ampersand";
836836 case TokenIdBinXor: return "BinXor";
837837 case TokenIdBoolOr: return "BoolOr";
838838 case TokenIdBoolAnd: return "BoolAnd";
src/tokenizer.hpp+1-1
......@@ -52,7 +52,7 @@ enum TokenId {
5252 TokenIdBoolOr,
5353 TokenIdBoolAnd,
5454 TokenIdBinOr,
55 TokenIdBinAnd,
55 TokenIdAmpersand,
5656 TokenIdBinXor,
5757 TokenIdEq,
5858 TokenIdTimesEq,
std/std.zig+1-1
......@@ -14,7 +14,7 @@ fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
1414}
1515
1616// TODO constants for SYS_write and stdout_fileno
17pub fn write(fd: isize, buf: *const u8, count: usize) -> isize {
17pub fn write(fd: isize, buf: &const u8, count: usize) -> isize {
1818 let SYS_write : isize = 1;
1919 return syscall3(SYS_write, fd, buf as isize, count as isize);
2020}
test/run_tests.cpp+20-21
......@@ -99,7 +99,7 @@ static void add_compiling_test_cases(void) {
9999 add_simple_case("hello world with libc", R"SOURCE(
100100 #link("c")
101101 extern {
102 fn puts(s: *const u8) -> i32;
102 fn puts(s: &const u8) -> i32;
103103 fn exit(code: i32) -> unreachable;
104104 }
105105
......@@ -112,7 +112,7 @@ static void add_compiling_test_cases(void) {
112112 add_simple_case("function call", R"SOURCE(
113113 #link("c")
114114 extern {
115 fn puts(s: *const u8) -> i32;
115 fn puts(s: &const u8) -> i32;
116116 fn exit(code: i32) -> unreachable;
117117 }
118118
......@@ -134,7 +134,7 @@ static void add_compiling_test_cases(void) {
134134 add_simple_case("comments", R"SOURCE(
135135 #link("c")
136136 extern {
137 fn puts(s: *const u8) -> i32;
137 fn puts(s: &const u8) -> i32;
138138 fn exit(code: i32) -> unreachable;
139139 }
140140
......@@ -169,7 +169,7 @@ static void add_compiling_test_cases(void) {
169169 add_source_file(tc, "libc.zig", R"SOURCE(
170170 #link("c")
171171 extern {
172 pub fn puts(s: *const u8) -> i32;
172 pub fn puts(s: &const u8) -> i32;
173173 pub fn exit(code: i32) -> unreachable;
174174 }
175175 )SOURCE");
......@@ -192,7 +192,7 @@ static void add_compiling_test_cases(void) {
192192 add_simple_case("if statements", R"SOURCE(
193193 #link("c")
194194 extern {
195 fn puts(s: *const u8) -> i32;
195 fn puts(s: &const u8) -> i32;
196196 fn exit(code: i32) -> unreachable;
197197 }
198198
......@@ -217,7 +217,7 @@ static void add_compiling_test_cases(void) {
217217 add_simple_case("params", R"SOURCE(
218218 #link("c")
219219 extern {
220 fn puts(s: *const u8) -> i32;
220 fn puts(s: &const u8) -> i32;
221221 fn exit(code: i32) -> unreachable;
222222 }
223223
......@@ -236,7 +236,7 @@ static void add_compiling_test_cases(void) {
236236 add_simple_case("goto", R"SOURCE(
237237 #link("c")
238238 extern {
239 fn puts(s: *const u8) -> i32;
239 fn puts(s: &const u8) -> i32;
240240 fn exit(code: i32) -> unreachable;
241241 }
242242
......@@ -260,7 +260,7 @@ static void add_compiling_test_cases(void) {
260260 add_simple_case("local variables", R"SOURCE(
261261#link("c")
262262extern {
263 fn puts(s: *const u8) -> i32;
263 fn puts(s: &const u8) -> i32;
264264 fn exit(code: i32) -> unreachable;
265265}
266266
......@@ -277,7 +277,7 @@ export fn _start() -> unreachable {
277277 add_simple_case("bool literals", R"SOURCE(
278278#link("c")
279279extern {
280 fn puts(s: *const u8) -> i32;
280 fn puts(s: &const u8) -> i32;
281281 fn exit(code: i32) -> unreachable;
282282}
283283
......@@ -293,7 +293,7 @@ export fn _start() -> unreachable {
293293 add_simple_case("separate block scopes", R"SOURCE(
294294#link("c")
295295extern {
296 fn puts(s: *const u8) -> i32;
296 fn puts(s: &const u8) -> i32;
297297 fn exit(code: i32) -> unreachable;
298298}
299299
......@@ -315,7 +315,7 @@ export fn _start() -> unreachable {
315315 add_simple_case("void parameters", R"SOURCE(
316316#link("c")
317317extern {
318 fn puts(s: *const u8) -> i32;
318 fn puts(s: &const u8) -> i32;
319319 fn exit(code: i32) -> unreachable;
320320}
321321
......@@ -335,7 +335,7 @@ fn void_fun(a : i32, b : void, c : i32) {
335335 add_simple_case("mutable local variables", R"SOURCE(
336336#link("c")
337337extern {
338 fn puts(s: *const u8) -> i32;
338 fn puts(s: &const u8) -> i32;
339339 fn exit(code: i32) -> unreachable;
340340}
341341
......@@ -359,7 +359,7 @@ done:
359359 add_simple_case("arrays", R"SOURCE(
360360#link("c")
361361extern {
362 fn puts(s: *const u8) -> i32;
362 fn puts(s: &const u8) -> i32;
363363 fn exit(code: i32) -> unreachable;
364364}
365365
......@@ -402,7 +402,7 @@ loop_2_end:
402402 add_simple_case("hello world without libc", R"SOURCE(
403403use "std.zig";
404404
405export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
405export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
406406 print_str("Hello, world!\n" as string);
407407 return 0;
408408}
......@@ -412,7 +412,7 @@ export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
412412 add_simple_case("a + b + c", R"SOURCE(
413413use "std.zig";
414414
415export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
415export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
416416 if false || false || false { print_str("BAD 1\n" as string); }
417417 if true && true && false { print_str("BAD 2\n" as string); }
418418 if 1 | 2 | 4 != 7 { print_str("BAD 3\n" as string); }
......@@ -434,7 +434,7 @@ export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
434434 add_simple_case("short circuit", R"SOURCE(
435435use "std.zig";
436436
437export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
437export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
438438 if true || { print_str("BAD 1\n" as string); false } {
439439 print_str("OK 1\n" as string);
440440 }
......@@ -457,7 +457,7 @@ export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
457457 add_simple_case("modify operators", R"SOURCE(
458458use "std.zig";
459459
460export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
460export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
461461 let mut i : i32 = 0;
462462 i += 5; if i != 5 { print_str("BAD +=\n" as string); }
463463 i -= 2; if i != 3 { print_str("BAD -=\n" as string); }
......@@ -480,7 +480,7 @@ export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
480480 add_simple_case("structs", R"SOURCE(
481481use "std.zig";
482482
483export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
483export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
484484 let mut foo : Foo;
485485 foo.a = foo.a + 1;
486486 foo.b = foo.a == 1;
......@@ -542,7 +542,7 @@ fn a() -> bogus {}
542542 )SOURCE", 1, ".tmp_source.zig:2:11: error: invalid type name: 'bogus'");
543543
544544 add_compile_fail_case("pointer to unreachable", R"SOURCE(
545fn a() -> *mut unreachable {}
545fn a() -> &unreachable {}
546546 )SOURCE", 1, ".tmp_source.zig:2:11: error: pointer to unreachable not allowed");
547547
548548 add_compile_fail_case("unreachable code", R"SOURCE(
......@@ -605,7 +605,7 @@ fn f() -> i32 {
605605 let a = c"a";
606606 a
607607}
608 )SOURCE", 1, ".tmp_source.zig:2:15: error: expected type 'i32', got '*const u8'");
608 )SOURCE", 1, ".tmp_source.zig:2:15: error: expected type 'i32', got '&const u8'");
609609
610610 add_compile_fail_case("if condition is bool, not int", R"SOURCE(
611611fn f() {
......@@ -682,7 +682,6 @@ fn f() {
682682 add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(
683683fn f(...) {}
684684 )SOURCE", 1, ".tmp_source.zig:2:1: error: variadic arguments only allowed in extern functions");
685
686685}
687686
688687static void print_compiler_invocation(TestCase *test_case) {