authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-22 19:51:37-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-22 19:51:37-05:00
log47cf8520adb245dbd34ad60fc9206b7eaab5e0be
treee2859d84e1dbf803b63bd3ac11014f7b3709f467
parent6a5e61acd117277eb2f943f5bc02ff7043542d4b

use comptime instead of inline for var and params

See #221

22 files changed, 82 insertions(+), 79 deletions(-)

doc/langref.md+2-2
...@@ -15,7 +15,7 @@ ErrorValueDecl = "error" Symbol ";"...@@ -15,7 +15,7 @@ ErrorValueDecl = "error" Symbol ";"
1515
16GlobalVarDecl = VariableDeclaration ";"16GlobalVarDecl = VariableDeclaration ";"
1717
18VariableDeclaration = option("inline") ("var" | "const") Symbol option(":" TypeExpr) "=" Expression18VariableDeclaration = option("comptime") ("var" | "const") Symbol option(":" TypeExpr) "=" Expression
1919
20StructMember = (StructField | FnDef | GlobalVarDecl)20StructMember = (StructField | FnDef | GlobalVarDecl)
2121
...@@ -33,7 +33,7 @@ FnDef = option("inline" | "extern") FnProto Block...@@ -33,7 +33,7 @@ FnDef = option("inline" | "extern") FnProto Block
3333
34ParamDeclList = "(" list(ParamDecl, ",") ")"34ParamDeclList = "(" list(ParamDecl, ",") ")"
3535
36ParamDecl = option("noalias" | "inline") option(Symbol ":") TypeExpr | "..."36ParamDecl = option("noalias" | "comptime") option(Symbol ":") TypeExpr | "..."
3737
38Block = "{" list(option(Statement), ";") "}"38Block = "{" list(option(Statement), ";") "}"
3939
doc/vim/syntax/zig.vim+1-1
...@@ -8,7 +8,7 @@ if exists("b:current_syntax")...@@ -8,7 +8,7 @@ if exists("b:current_syntax")
8endif8endif
9let b:current_syntax = "zig"9let b:current_syntax = "zig"
1010
11syn keyword zigStorage const var extern export pub noalias inline nakedcc coldcc11syn keyword zigStorage const var extern export pub noalias inline comptime nakedcc coldcc
12syn keyword zigStructure struct enum union12syn keyword zigStructure struct enum union
13syn keyword zigStatement goto break return continue asm defer13syn keyword zigStatement goto break return continue asm defer
14syn keyword zigConditional if else switch14syn keyword zigConditional if else switch
src/analyze.cpp+1-1
...@@ -979,7 +979,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c...@@ -979,7 +979,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
979 if (param_is_inline) {979 if (param_is_inline) {
980 if (fn_type_id.is_extern) {980 if (fn_type_id.is_extern) {
981 add_node_error(g, param_node,981 add_node_error(g, param_node,
982 buf_sprintf("inline parameter not allowed in extern function"));982 buf_sprintf("comptime parameter not allowed in extern function"));
983 return g->builtin_types.entry_invalid;983 return g->builtin_types.entry_invalid;
984 }984 }
985 return get_generic_fn_type(g, &fn_type_id);985 return get_generic_fn_type(g, &fn_type_id);
src/parser.cpp+9-9
...@@ -250,7 +250,7 @@ static AstNode *ast_parse_type_expr(ParseContext *pc, size_t *token_index, bool...@@ -250,7 +250,7 @@ static AstNode *ast_parse_type_expr(ParseContext *pc, size_t *token_index, bool
250}250}
251251
252/*252/*
253ParamDecl = option("noalias" | "inline") option("Symbol" ":") TypeExpr | "..."253ParamDecl = option("noalias" | "comptime") option(Symbol ":") TypeExpr | "..."
254*/254*/
255static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) {255static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) {
256 Token *token = &pc->tokens->at(*token_index);256 Token *token = &pc->tokens->at(*token_index);
...@@ -266,7 +266,7 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) {...@@ -266,7 +266,7 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) {
266 node->data.param_decl.is_noalias = true;266 node->data.param_decl.is_noalias = true;
267 *token_index += 1;267 *token_index += 1;
268 token = &pc->tokens->at(*token_index);268 token = &pc->tokens->at(*token_index);
269 } else if (token->id == TokenIdKeywordInline) {269 } else if (token->id == TokenIdKeywordCompTime) {
270 node->data.param_decl.is_inline = true;270 node->data.param_decl.is_inline = true;
271 *token_index += 1;271 *token_index += 1;
272 token = &pc->tokens->at(*token_index);272 token = &pc->tokens->at(*token_index);
...@@ -1492,7 +1492,7 @@ static AstNode *ast_parse_defer_expr(ParseContext *pc, size_t *token_index) {...@@ -1492,7 +1492,7 @@ static AstNode *ast_parse_defer_expr(ParseContext *pc, size_t *token_index) {
1492}1492}
14931493
1494/*1494/*
1495VariableDeclaration = option("inline") ("var" | "const") Symbol option(":" TypeExpr) "=" Expression1495VariableDeclaration = option("comptime") ("var" | "const") Symbol option(":" TypeExpr) "=" Expression
1496*/1496*/
1497static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, size_t *token_index, bool mandatory,1497static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, size_t *token_index, bool mandatory,
1498 VisibMod visib_mod)1498 VisibMod visib_mod)
...@@ -1501,9 +1501,9 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, size_t *to...@@ -1501,9 +1501,9 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, size_t *to
1501 Token *var_token;1501 Token *var_token;
15021502
1503 bool is_const;1503 bool is_const;
1504 bool is_inline;1504 bool is_comptime;
1505 if (first_token->id == TokenIdKeywordInline) {1505 if (first_token->id == TokenIdKeywordCompTime) {
1506 is_inline = true;1506 is_comptime = true;
1507 var_token = &pc->tokens->at(*token_index + 1);1507 var_token = &pc->tokens->at(*token_index + 1);
15081508
1509 if (var_token->id == TokenIdKeywordVar) {1509 if (var_token->id == TokenIdKeywordVar) {
...@@ -1518,12 +1518,12 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, size_t *to...@@ -1518,12 +1518,12 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, size_t *to
15181518
1519 *token_index += 2;1519 *token_index += 2;
1520 } else if (first_token->id == TokenIdKeywordVar) {1520 } else if (first_token->id == TokenIdKeywordVar) {
1521 is_inline = false;1521 is_comptime = false;
1522 is_const = false;1522 is_const = false;
1523 var_token = first_token;1523 var_token = first_token;
1524 *token_index += 1;1524 *token_index += 1;
1525 } else if (first_token->id == TokenIdKeywordConst) {1525 } else if (first_token->id == TokenIdKeywordConst) {
1526 is_inline = false;1526 is_comptime = false;
1527 is_const = true;1527 is_const = true;
1528 var_token = first_token;1528 var_token = first_token;
1529 *token_index += 1;1529 *token_index += 1;
...@@ -1535,7 +1535,7 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, size_t *to...@@ -1535,7 +1535,7 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, size_t *to
15351535
1536 AstNode *node = ast_create_node(pc, NodeTypeVariableDeclaration, var_token);1536 AstNode *node = ast_create_node(pc, NodeTypeVariableDeclaration, var_token);
15371537
1538 node->data.variable_declaration.is_inline = is_inline;1538 node->data.variable_declaration.is_inline = is_comptime;
1539 node->data.variable_declaration.is_const = is_const;1539 node->data.variable_declaration.is_const = is_const;
1540 node->data.variable_declaration.visib_mod = visib_mod;1540 node->data.variable_declaration.visib_mod = visib_mod;
15411541
src/tokenizer.cpp+2
...@@ -110,6 +110,7 @@ static const struct ZigKeyword zig_keywords[] = {...@@ -110,6 +110,7 @@ static const struct ZigKeyword zig_keywords[] = {
110 {"asm", TokenIdKeywordAsm},110 {"asm", TokenIdKeywordAsm},
111 {"break", TokenIdKeywordBreak},111 {"break", TokenIdKeywordBreak},
112 {"coldcc", TokenIdKeywordColdCC},112 {"coldcc", TokenIdKeywordColdCC},
113 {"comptime", TokenIdKeywordCompTime},
113 {"const", TokenIdKeywordConst},114 {"const", TokenIdKeywordConst},
114 {"continue", TokenIdKeywordContinue},115 {"continue", TokenIdKeywordContinue},
115 {"defer", TokenIdKeywordDefer},116 {"defer", TokenIdKeywordDefer},
...@@ -1475,6 +1476,7 @@ const char * token_name(TokenId id) {...@@ -1475,6 +1476,7 @@ const char * token_name(TokenId id) {
1475 case TokenIdKeywordError: return "error";1476 case TokenIdKeywordError: return "error";
1476 case TokenIdKeywordType: return "type";1477 case TokenIdKeywordType: return "type";
1477 case TokenIdKeywordInline: return "inline";1478 case TokenIdKeywordInline: return "inline";
1479 case TokenIdKeywordCompTime: return "comptime";
1478 case TokenIdKeywordDefer: return "defer";1480 case TokenIdKeywordDefer: return "defer";
1479 case TokenIdKeywordColdCC: return "coldcc";1481 case TokenIdKeywordColdCC: return "coldcc";
1480 case TokenIdKeywordNakedCC: return "nakedcc";1482 case TokenIdKeywordNakedCC: return "nakedcc";
src/tokenizer.hpp+1
...@@ -43,6 +43,7 @@ enum TokenId {...@@ -43,6 +43,7 @@ enum TokenId {
43 TokenIdKeywordError,43 TokenIdKeywordError,
44 TokenIdKeywordType,44 TokenIdKeywordType,
45 TokenIdKeywordInline,45 TokenIdKeywordInline,
46 TokenIdKeywordCompTime,
46 TokenIdKeywordDefer,47 TokenIdKeywordDefer,
47 TokenIdKeywordThis,48 TokenIdKeywordThis,
48 TokenIdKeywordColdCC,49 TokenIdKeywordColdCC,
std/bootstrap.zig+1-1
...@@ -16,7 +16,7 @@ var argv: &&u8 = undefined;...@@ -16,7 +16,7 @@ var argv: &&u8 = undefined;
16export nakedcc fn _start() -> unreachable {16export nakedcc fn _start() -> unreachable {
17 @setFnVisible(this, want_start_symbol);17 @setFnVisible(this, want_start_symbol);
1818
19 inline switch (@compileVar("arch")) {19 switch (@compileVar("arch")) {
20 Arch.x86_64 => {20 Arch.x86_64 => {
21 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));21 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
22 argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));22 argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
std/debug.zig+1-1
...@@ -241,7 +241,7 @@ fn parseFormValueRefLen(in_stream: &io.InStream, size: usize) -> %FormValue {...@@ -241,7 +241,7 @@ fn parseFormValueRefLen(in_stream: &io.InStream, size: usize) -> %FormValue {
241 return FormValue.Ref { buf };241 return FormValue.Ref { buf };
242}242}
243243
244fn parseFormValueRef(in_stream: &io.InStream, inline T: type) -> %FormValue {244fn parseFormValueRef(in_stream: &io.InStream, comptime T: type) -> %FormValue {
245 const block_len = %return in_stream.readIntLe(T);245 const block_len = %return in_stream.readIntLe(T);
246 return parseFormValueRefLen(in_stream, block_len);246 return parseFormValueRefLen(in_stream, block_len);
247}247}
std/endian.zig+4-4
...@@ -1,16 +1,16 @@...@@ -1,16 +1,16 @@
1pub inline fn swapIfLe(inline T: type, x: T) -> T {1pub inline fn swapIfLe(comptime T: type, x: T) -> T {
2 swapIf(false, T, x)2 swapIf(false, T, x)
3}3}
44
5pub inline fn swapIfBe(inline T: type, x: T) -> T {5pub inline fn swapIfBe(comptime T: type, x: T) -> T {
6 swapIf(true, T, x)6 swapIf(true, T, x)
7}7}
88
9pub inline fn swapIf(is_be: bool, inline T: type, x: T) -> T {9pub inline fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
10 if (@compileVar("is_big_endian") == is_be) swap(T, x) else x10 if (@compileVar("is_big_endian") == is_be) swap(T, x) else x
11}11}
1212
13pub fn swap(inline T: type, x: T) -> T {13pub fn swap(comptime T: type, x: T) -> T {
14 const x_slice = ([]u8)((&const x)[0...1]);14 const x_slice = ([]u8)((&const x)[0...1]);
15 var result: T = undefined;15 var result: T = undefined;
16 const result_slice = ([]u8)((&result)[0...1]);16 const result_slice = ([]u8)((&result)[0...1]);
std/hash_map.zig+2-2
...@@ -7,8 +7,8 @@ const Allocator = mem.Allocator;...@@ -7,8 +7,8 @@ const Allocator = mem.Allocator;
7const want_modification_safety = !@compileVar("is_release");7const want_modification_safety = !@compileVar("is_release");
8const debug_u32 = if (want_modification_safety) u32 else void;8const debug_u32 = if (want_modification_safety) u32 else void;
99
10pub fn HashMap(inline K: type, inline V: type, inline hash: fn(key: K)->u32,10pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn(key: K)->u32,
11 inline eql: fn(a: K, b: K)->bool) -> type11 comptime eql: fn(a: K, b: K)->bool) -> type
12{12{
13 struct {13 struct {
14 entries: []Entry,14 entries: []Entry,
std/io.zig+9-9
...@@ -105,7 +105,7 @@ pub const OutStream = struct {...@@ -105,7 +105,7 @@ pub const OutStream = struct {
105 return byte_count;105 return byte_count;
106 }106 }
107107
108 pub fn printInt(self: &OutStream, inline T: type, x: T) -> %usize {108 pub fn printInt(self: &OutStream, comptime T: type, x: T) -> %usize {
109 // TODO replace max_u64_base10_digits with math.log10(math.pow(2, @sizeOf(T)))109 // TODO replace max_u64_base10_digits with math.log10(math.pow(2, @sizeOf(T)))
110 if (self.index + max_u64_base10_digits >= self.buffer.len) {110 if (self.index + max_u64_base10_digits >= self.buffer.len) {
111 %return self.flush();111 %return self.flush();
...@@ -255,22 +255,22 @@ pub const InStream = struct {...@@ -255,22 +255,22 @@ pub const InStream = struct {
255 return result[0];255 return result[0];
256 }256 }
257257
258 pub fn readIntLe(is: &InStream, inline T: type) -> %T {258 pub fn readIntLe(is: &InStream, comptime T: type) -> %T {
259 is.readInt(false, T)259 is.readInt(false, T)
260 }260 }
261261
262 pub fn readIntBe(is: &InStream, inline T: type) -> %T {262 pub fn readIntBe(is: &InStream, comptime T: type) -> %T {
263 is.readInt(true, T)263 is.readInt(true, T)
264 }264 }
265265
266 pub fn readInt(is: &InStream, is_be: bool, inline T: type) -> %T {266 pub fn readInt(is: &InStream, is_be: bool, comptime T: type) -> %T {
267 var result: T = undefined;267 var result: T = undefined;
268 const result_slice = ([]u8)((&result)[0...1]);268 const result_slice = ([]u8)((&result)[0...1]);
269 %return is.readNoEof(result_slice);269 %return is.readNoEof(result_slice);
270 return endian.swapIf(!is_be, T, result);270 return endian.swapIf(!is_be, T, result);
271 }271 }
272272
273 pub fn readVarInt(is: &InStream, is_be: bool, inline T: type, size: usize) -> %T {273 pub fn readVarInt(is: &InStream, is_be: bool, comptime T: type, size: usize) -> %T {
274 assert(size <= @sizeOf(T));274 assert(size <= @sizeOf(T));
275 assert(size <= 8);275 assert(size <= 8);
276 var input_buf: [8]u8 = undefined;276 var input_buf: [8]u8 = undefined;
...@@ -355,7 +355,7 @@ pub const InStream = struct {...@@ -355,7 +355,7 @@ pub const InStream = struct {
355 }355 }
356};356};
357357
358pub fn parseUnsigned(inline T: type, buf: []u8, radix: u8) -> %T {358pub fn parseUnsigned(comptime T: type, buf: []u8, radix: u8) -> %T {
359 var x: T = 0;359 var x: T = 0;
360360
361 for (buf) |c| {361 for (buf) |c| {
...@@ -381,11 +381,11 @@ fn charToDigit(c: u8, radix: u8) -> %u8 {...@@ -381,11 +381,11 @@ fn charToDigit(c: u8, radix: u8) -> %u8 {
381 return if (value >= radix) error.InvalidChar else value;381 return if (value >= radix) error.InvalidChar else value;
382}382}
383383
384pub fn bufPrintInt(inline T: type, out_buf: []u8, x: T) -> usize {384pub fn bufPrintInt(comptime T: type, out_buf: []u8, x: T) -> usize {
385 if (T.is_signed) bufPrintSigned(T, out_buf, x) else bufPrintUnsigned(T, out_buf, x)385 if (T.is_signed) bufPrintSigned(T, out_buf, x) else bufPrintUnsigned(T, out_buf, x)
386}386}
387387
388fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize {388fn bufPrintSigned(comptime T: type, out_buf: []u8, x: T) -> usize {
389 const uint = @intType(false, T.bit_count);389 const uint = @intType(false, T.bit_count);
390 if (x < 0) {390 if (x < 0) {
391 out_buf[0] = '-';391 out_buf[0] = '-';
...@@ -395,7 +395,7 @@ fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize {...@@ -395,7 +395,7 @@ fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize {
395 }395 }
396}396}
397397
398fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize {398fn bufPrintUnsigned(comptime T: type, out_buf: []u8, x: T) -> usize {
399 var buf: [max_u64_base10_digits]u8 = undefined;399 var buf: [max_u64_base10_digits]u8 = undefined;
400 var a = x;400 var a = x;
401 var index: usize = buf.len;401 var index: usize = buf.len;
std/list.zig+1-1
...@@ -3,7 +3,7 @@ const assert = debug.assert;...@@ -3,7 +3,7 @@ const assert = debug.assert;
3const mem = @import("mem.zig");3const mem = @import("mem.zig");
4const Allocator = mem.Allocator;4const Allocator = mem.Allocator;
55
6pub fn List(inline T: type) -> type{6pub fn List(comptime T: type) -> type{
7 struct {7 struct {
8 const Self = this;8 const Self = this;
99
std/math.zig+4-4
...@@ -13,19 +13,19 @@ pub fn max(x: var, y: var) -> @typeOf(x + y) {...@@ -13,19 +13,19 @@ pub fn max(x: var, y: var) -> @typeOf(x + y) {
13}13}
1414
15error Overflow;15error Overflow;
16pub fn mulOverflow(inline T: type, a: T, b: T) -> %T {16pub fn mulOverflow(comptime T: type, a: T, b: T) -> %T {
17 var answer: T = undefined;17 var answer: T = undefined;
18 if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer18 if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer
19}19}
20pub fn addOverflow(inline T: type, a: T, b: T) -> %T {20pub fn addOverflow(comptime T: type, a: T, b: T) -> %T {
21 var answer: T = undefined;21 var answer: T = undefined;
22 if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer22 if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer
23}23}
24pub fn subOverflow(inline T: type, a: T, b: T) -> %T {24pub fn subOverflow(comptime T: type, a: T, b: T) -> %T {
25 var answer: T = undefined;25 var answer: T = undefined;
26 if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer26 if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer
27}27}
28pub fn shlOverflow(inline T: type, a: T, b: T) -> %T {28pub fn shlOverflow(comptime T: type, a: T, b: T) -> %T {
29 var answer: T = undefined;29 var answer: T = undefined;
30 if (@shlWithOverflow(T, a, b, &answer)) error.Overflow else answer30 if (@shlWithOverflow(T, a, b, &answer)) error.Overflow else answer
31}31}
std/mem.zig+8-8
...@@ -15,7 +15,7 @@ pub const Allocator = struct {...@@ -15,7 +15,7 @@ pub const Allocator = struct {
15 context: ?&Context,15 context: ?&Context,
1616
17 /// Aborts the program if an allocation fails.17 /// Aborts the program if an allocation fails.
18 fn checkedAlloc(self: &Allocator, inline T: type, n: usize) -> []T {18 fn checkedAlloc(self: &Allocator, comptime T: type, n: usize) -> []T {
19 alloc(self, T, n) %% |err| {19 alloc(self, T, n) %% |err| {
20 // TODO var args printf20 // TODO var args printf
21 %%io.stderr.write("allocation failure: ");21 %%io.stderr.write("allocation failure: ");
...@@ -25,37 +25,37 @@ pub const Allocator = struct {...@@ -25,37 +25,37 @@ pub const Allocator = struct {
25 }25 }
26 }26 }
2727
28 fn alloc(self: &Allocator, inline T: type, n: usize) -> %[]T {28 fn alloc(self: &Allocator, comptime T: type, n: usize) -> %[]T {
29 const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n);29 const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n);
30 ([]T)(%return self.allocFn(self, byte_count))30 ([]T)(%return self.allocFn(self, byte_count))
31 }31 }
3232
33 fn realloc(self: &Allocator, inline T: type, old_mem: []T, n: usize) -> %[]T {33 fn realloc(self: &Allocator, comptime T: type, old_mem: []T, n: usize) -> %[]T {
34 const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n);34 const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n);
35 ([]T)(%return self.reallocFn(self, ([]u8)(old_mem), byte_count))35 ([]T)(%return self.reallocFn(self, ([]u8)(old_mem), byte_count))
36 }36 }
3737
38 // TODO mem: []var and get rid of 2nd param38 // TODO mem: []var and get rid of 2nd param
39 fn free(self: &Allocator, inline T: type, mem: []T) {39 fn free(self: &Allocator, comptime T: type, mem: []T) {
40 self.freeFn(self, ([]u8)(mem));40 self.freeFn(self, ([]u8)(mem));
41 }41 }
42};42};
4343
44/// Copy all of source into dest at position 0.44/// Copy all of source into dest at position 0.
45/// dest.len must be >= source.len.45/// dest.len must be >= source.len.
46pub fn copy(inline T: type, dest: []T, source: []const T) {46pub fn copy(comptime T: type, dest: []T, source: []const T) {
47 @setDebugSafety(this, false);47 @setDebugSafety(this, false);
48 assert(dest.len >= source.len);48 assert(dest.len >= source.len);
49 for (source) |s, i| dest[i] = s;49 for (source) |s, i| dest[i] = s;
50}50}
5151
52pub fn set(inline T: type, dest: []T, value: T) {52pub fn set(comptime T: type, dest: []T, value: T) {
53 for (dest) |*d| *d = value;53 for (dest) |*d| *d = value;
54}54}
5555
56/// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than,56/// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than,
57/// memory b, respectively.57/// memory b, respectively.
58pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {58pub fn cmp(comptime T: type, a: []const T, b: []const T) -> Cmp {
59 const n = math.min(a.len, b.len);59 const n = math.min(a.len, b.len);
60 var i: usize = 0;60 var i: usize = 0;
61 while (i < n; i += 1) {61 while (i < n; i += 1) {
...@@ -66,7 +66,7 @@ pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {...@@ -66,7 +66,7 @@ pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {
66 return if (a.len > b.len) Cmp.Greater else if (a.len < b.len) Cmp.Less else Cmp.Equal;66 return if (a.len > b.len) Cmp.Greater else if (a.len < b.len) Cmp.Less else Cmp.Equal;
67}67}
6868
69pub fn sliceAsInt(buf: []u8, is_be: bool, inline T: type) -> T {69pub fn sliceAsInt(buf: []u8, is_be: bool, comptime T: type) -> T {
70 var result: T = undefined;70 var result: T = undefined;
71 const result_slice = ([]u8)((&result)[0...1]);71 const result_slice = ([]u8)((&result)[0...1]);
72 set(u8, result_slice, 0);72 set(u8, result_slice, 0);
std/rand.zig+9-9
...@@ -29,7 +29,7 @@ pub const Rand = struct {...@@ -29,7 +29,7 @@ pub const Rand = struct {
29 }29 }
3030
31 /// Get an integer with random bits.31 /// Get an integer with random bits.
32 pub fn scalar(r: &Rand, inline T: type) -> T {32 pub fn scalar(r: &Rand, comptime T: type) -> T {
33 if (T == usize) {33 if (T == usize) {
34 return r.rng.get();34 return r.rng.get();
35 } else {35 } else {
...@@ -59,7 +59,7 @@ pub const Rand = struct {...@@ -59,7 +59,7 @@ pub const Rand = struct {
59 /// Get a random unsigned integer with even distribution between `start`59 /// Get a random unsigned integer with even distribution between `start`
60 /// inclusive and `end` exclusive.60 /// inclusive and `end` exclusive.
61 // TODO support signed integers and then rename to "range"61 // TODO support signed integers and then rename to "range"
62 pub fn rangeUnsigned(r: &Rand, inline T: type, start: T, end: T) -> T {62 pub fn rangeUnsigned(r: &Rand, comptime T: type, start: T, end: T) -> T {
63 const range = end - start;63 const range = end - start;
64 const leftover = @maxValue(T) % range;64 const leftover = @maxValue(T) % range;
65 const upper_bound = @maxValue(T) - leftover;65 const upper_bound = @maxValue(T) - leftover;
...@@ -75,7 +75,7 @@ pub const Rand = struct {...@@ -75,7 +75,7 @@ pub const Rand = struct {
75 }75 }
7676
77 /// Get a floating point value in the range 0.0..1.0.77 /// Get a floating point value in the range 0.0..1.0.
78 pub fn float(r: &Rand, inline T: type) -> T {78 pub fn float(r: &Rand, comptime T: type) -> T {
79 // TODO Implement this way instead:79 // TODO Implement this way instead:
80 // const int = @int_type(false, @sizeOf(T) * 8);80 // const int = @int_type(false, @sizeOf(T) * 8);
81 // const mask = ((1 << @float_mantissa_bit_count(T)) - 1);81 // const mask = ((1 << @float_mantissa_bit_count(T)) - 1);
...@@ -94,12 +94,12 @@ pub const Rand = struct {...@@ -94,12 +94,12 @@ pub const Rand = struct {
94};94};
9595
96fn MersenneTwister(96fn MersenneTwister(
97 inline int: type, inline n: usize, inline m: usize, inline r: int,97 comptime int: type, comptime n: usize, comptime m: usize, comptime r: int,
98 inline a: int,98 comptime a: int,
99 inline u: int, inline d: int,99 comptime u: int, comptime d: int,
100 inline s: int, inline b: int,100 comptime s: int, comptime b: int,
101 inline t: int, inline c: int,101 comptime t: int, comptime c: int,
102 inline l: int, inline f: int) -> type102 comptime l: int, comptime f: int) -> type
103{103{
104 struct {104 struct {
105 const Self = this;105 const Self = this;
std/sort.zig+2-2
...@@ -5,13 +5,13 @@ const math = @import("math.zig");...@@ -5,13 +5,13 @@ const math = @import("math.zig");
55
6pub const Cmp = math.Cmp;6pub const Cmp = math.Cmp;
77
8pub fn sort(inline T: type, array: []T, inline cmp: fn(a: &const T, b: &const T)->Cmp) {8pub fn sort(comptime T: type, array: []T, comptime cmp: fn(a: &const T, b: &const T)->Cmp) {
9 if (array.len > 0) {9 if (array.len > 0) {
10 quicksort(T, array, 0, array.len - 1, cmp);10 quicksort(T, array, 0, array.len - 1, cmp);
11 }11 }
12}12}
1313
14fn quicksort(inline T: type, array: []T, left: usize, right: usize, inline cmp: fn(a: &const T, b: &const T)->Cmp) {14fn quicksort(comptime T: type, array: []T, left: usize, right: usize, comptime cmp: fn(a: &const T, b: &const T)->Cmp) {
15 var i = left;15 var i = left;
16 var j = right;16 var j = right;
17 const p = (i + j) / 2;17 const p = (i + j) / 2;
std/str.zig+1-1
...@@ -4,7 +4,7 @@ pub fn eql(a: []const u8, b: []const u8) -> bool {...@@ -4,7 +4,7 @@ pub fn eql(a: []const u8, b: []const u8) -> bool {
4 sliceEql(u8, a, b)4 sliceEql(u8, a, b)
5}5}
66
7pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {7pub fn sliceEql(comptime T: type, a: []const T, b: []const T) -> bool {
8 if (a.len != b.len) return false;8 if (a.len != b.len) return false;
9 for (a) |item, index| {9 for (a) |item, index| {
10 if (b[index] != item) return false;10 if (b[index] != item) return false;
test/cases/eval.zig+4-4
...@@ -25,17 +25,17 @@ fn testStaticAddOne() {...@@ -25,17 +25,17 @@ fn testStaticAddOne() {
25fn inlinedLoop() {25fn inlinedLoop() {
26 @setFnTest(this);26 @setFnTest(this);
2727
28 inline var i = 0;28 comptime var i = 0;
29 inline var sum = 0;29 comptime var sum = 0;
30 inline while (i <= 5; i += 1)30 inline while (i <= 5; i += 1)
31 sum += i;31 sum += i;
32 assert(sum == 15);32 assert(sum == 15);
33}33}
3434
35fn gimme1or2(inline a: bool) -> i32 {35fn gimme1or2(comptime a: bool) -> i32 {
36 const x: i32 = 1;36 const x: i32 = 1;
37 const y: i32 = 2;37 const y: i32 = 2;
38 inline var z: i32 = if (a) x else y;38 comptime var z: i32 = if (a) x else y;
39 return z;39 return z;
40}40}
41fn inlineVariableGetsResultOfConstIf() {41fn inlineVariableGetsResultOfConstIf() {
test/cases/generics.zig+8-8
...@@ -8,11 +8,11 @@ fn simpleGenericFn() {...@@ -8,11 +8,11 @@ fn simpleGenericFn() {
8 assert(add(2, 3) == 5);8 assert(add(2, 3) == 5);
9}9}
1010
11fn max(inline T: type, a: T, b: T) -> T {11fn max(comptime T: type, a: T, b: T) -> T {
12 return if (a > b) a else b;12 return if (a > b) a else b;
13}13}
1414
15fn add(inline a: i32, b: i32) -> i32 {15fn add(comptime a: i32, b: i32) -> i32 {
16 return @staticEval(a) + b;16 return @staticEval(a) + b;
17}17}
1818
...@@ -67,11 +67,11 @@ fn max_f64(a: f64, b: f64) -> f64 {...@@ -67,11 +67,11 @@ fn max_f64(a: f64, b: f64) -> f64 {
67}67}
6868
6969
70pub fn List(inline T: type) -> type {70pub fn List(comptime T: type) -> type {
71 SmallList(T, 8)71 SmallList(T, 8)
72}72}
7373
74pub fn SmallList(inline T: type, inline STATIC_SIZE: usize) -> type {74pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) -> type {
75 struct {75 struct {
76 items: []T,76 items: []T,
77 length: usize,77 length: usize,
...@@ -100,7 +100,7 @@ fn genericStruct() {...@@ -100,7 +100,7 @@ fn genericStruct() {
100 assert(a1.value == a1.getVal());100 assert(a1.value == a1.getVal());
101 assert(b1.getVal());101 assert(b1.getVal());
102}102}
103fn GenNode(inline T: type) -> type {103fn GenNode(comptime T: type) -> type {
104 struct {104 struct {
105 value: T,105 value: T,
106 next: ?&GenNode(T),106 next: ?&GenNode(T),
...@@ -113,7 +113,7 @@ fn constDeclsInStruct() {...@@ -113,7 +113,7 @@ fn constDeclsInStruct() {
113113
114 assert(GenericDataThing(3).count_plus_one == 4);114 assert(GenericDataThing(3).count_plus_one == 4);
115}115}
116fn GenericDataThing(inline count: isize) -> type {116fn GenericDataThing(comptime count: isize) -> type {
117 struct {117 struct {
118 const count_plus_one = count + 1;118 const count_plus_one = count + 1;
119 }119 }
...@@ -125,7 +125,7 @@ fn useGenericParamInGenericParam() {...@@ -125,7 +125,7 @@ fn useGenericParamInGenericParam() {
125125
126 assert(aGenericFn(i32, 3, 4) == 7);126 assert(aGenericFn(i32, 3, 4) == 7);
127}127}
128fn aGenericFn(inline T: type, inline a: T, b: T) -> T {128fn aGenericFn(comptime T: type, comptime a: T, b: T) -> T {
129 return a + b;129 return a + b;
130}130}
131131
...@@ -137,6 +137,6 @@ fn genericFnWithImplicitCast() {...@@ -137,6 +137,6 @@ fn genericFnWithImplicitCast() {
137 assert(getFirstByte(u16, []u16 {0, 13}) == 0);137 assert(getFirstByte(u16, []u16 {0, 13}) == 0);
138}138}
139fn getByte(ptr: ?&u8) -> u8 {*??ptr}139fn getByte(ptr: ?&u8) -> u8 {*??ptr}
140fn getFirstByte(inline T: type, mem: []T) -> u8 {140fn getFirstByte(comptime T: type, mem: []T) -> u8 {
141 getByte((&u8)(&mem[0]))141 getByte((&u8)(&mem[0]))
142}142}
test/cases/misc.zig+2-2
...@@ -292,10 +292,10 @@ fn genericMallocFree() {...@@ -292,10 +292,10 @@ fn genericMallocFree() {
292 memFree(u8, a);292 memFree(u8, a);
293}293}
294const some_mem : [100]u8 = undefined;294const some_mem : [100]u8 = undefined;
295fn memAlloc(inline T: type, n: usize) -> %[]T {295fn memAlloc(comptime T: type, n: usize) -> %[]T {
296 return (&T)(&some_mem[0])[0...n];296 return (&T)(&some_mem[0])[0...n];
297}297}
298fn memFree(inline T: type, mem: []T) { }298fn memFree(comptime T: type, mem: []T) { }
299299
300300
301fn castUndefined() {301fn castUndefined() {
test/cases/this.zig+1-1
...@@ -2,7 +2,7 @@ const assert = @import("std").debug.assert;...@@ -2,7 +2,7 @@ const assert = @import("std").debug.assert;
22
3const module = this;3const module = this;
44
5fn Point(inline T: type) -> type {5fn Point(comptime T: type) -> type {
6 struct {6 struct {
7 const Self = this;7 const Self = this;
8 x: T,8 x: T,
test/run_tests.cpp+9-9
...@@ -1195,7 +1195,7 @@ const invalid = foo > foo;...@@ -1195,7 +1195,7 @@ const invalid = foo > foo;
1195 )SOURCE", 1, ".tmp_source.zig:3:21: error: operator not allowed for type 'fn()'");1195 )SOURCE", 1, ".tmp_source.zig:3:21: error: operator not allowed for type 'fn()'");
11961196
1197 add_compile_fail_case("generic function instance with non-constant expression", R"SOURCE(1197 add_compile_fail_case("generic function instance with non-constant expression", R"SOURCE(
1198fn foo(inline x: i32, y: i32) -> i32 { return x + y; }1198fn foo(comptime x: i32, y: i32) -> i32 { return x + y; }
1199fn test1(a: i32, b: i32) -> i32 {1199fn test1(a: i32, b: i32) -> i32 {
1200 return foo(a, b);1200 return foo(a, b);
1201}1201}
...@@ -1407,18 +1407,18 @@ fn f() {...@@ -1407,18 +1407,18 @@ fn f() {
1407}1407}
1408 )SOURCE", 1, ".tmp_source.zig:3:13: error: unable to evaluate constant expression");1408 )SOURCE", 1, ".tmp_source.zig:3:13: error: unable to evaluate constant expression");
14091409
1410 add_compile_fail_case("export function with inline parameter", R"SOURCE(1410 add_compile_fail_case("export function with comptime parameter", R"SOURCE(
1411export fn foo(inline x: i32, y: i32) -> i32{1411export fn foo(comptime x: i32, y: i32) -> i32{
1412 x + y1412 x + y
1413}1413}
1414 )SOURCE", 1, ".tmp_source.zig:2:15: error: inline parameter not allowed in extern function");1414 )SOURCE", 1, ".tmp_source.zig:2:15: error: comptime parameter not allowed in extern function");
14151415
1416 add_compile_fail_case("extern function with inline parameter", R"SOURCE(1416 add_compile_fail_case("extern function with comptime parameter", R"SOURCE(
1417extern fn foo(inline x: i32, y: i32) -> i32;1417extern fn foo(comptime x: i32, y: i32) -> i32;
1418fn f() -> i32 {1418fn f() -> i32 {
1419 foo(1, 2)1419 foo(1, 2)
1420}1420}
1421 )SOURCE", 1, ".tmp_source.zig:2:15: error: inline parameter not allowed in extern function");1421 )SOURCE", 1, ".tmp_source.zig:2:15: error: comptime parameter not allowed in extern function");
14221422
1423 add_compile_fail_case("convert fixed size array to slice with invalid size", R"SOURCE(1423 add_compile_fail_case("convert fixed size array to slice with invalid size", R"SOURCE(
1424fn f() {1424fn f() {
...@@ -1429,12 +1429,12 @@ fn f() {...@@ -1429,12 +1429,12 @@ fn f() {
14291429
1430 add_compile_fail_case("non-pure function returns type", R"SOURCE(1430 add_compile_fail_case("non-pure function returns type", R"SOURCE(
1431var a: u32 = 0;1431var a: u32 = 0;
1432pub fn List(inline T: type) -> type {1432pub fn List(comptime T: type) -> type {
1433 a += 1;1433 a += 1;
1434 SmallList(T, 8)1434 SmallList(T, 8)
1435}1435}
14361436
1437pub fn SmallList(inline T: type, inline STATIC_SIZE: usize) -> type {1437pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) -> type {
1438 struct {1438 struct {
1439 items: []T,1439 items: []T,
1440 length: usize,1440 length: usize,