authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-29 19:23:33-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-29 19:23:33-04:00
logcbfe4b4bae61682e367a477283e4d64a518203a8
tree058c276cb3bf8826f0ef421be01b8680109d85a7
parentd04d3ec7753af834ea7be9905377fc60baea8ccc

add implicit cast from [0]T to %[]T

closes #347 also add std.os.path.relative

4 files changed, 159 insertions(+), 6 deletions(-)

src/ir.cpp+44-4
...@@ -5963,7 +5963,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,...@@ -5963,7 +5963,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
5963 return ImplicitCastMatchResultYes;5963 return ImplicitCastMatchResultYes;
5964 }5964 }
59655965
5966 // implicit conversion from error child type to error type5966 // implicit T to %T
5967 if (expected_type->id == TypeTableEntryIdErrorUnion &&5967 if (expected_type->id == TypeTableEntryIdErrorUnion &&
5968 ir_types_match_with_implicit_cast(ira, expected_type->data.error.child_type, actual_type, value))5968 ir_types_match_with_implicit_cast(ira, expected_type->data.error.child_type, actual_type, value))
5969 {5969 {
...@@ -6012,7 +6012,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,...@@ -6012,7 +6012,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
6012 return ImplicitCastMatchResultYes;6012 return ImplicitCastMatchResultYes;
6013 }6013 }
60146014
6015 // implicit array to slice conversion6015 // implicit [N]T to []const T
6016 if (expected_type->id == TypeTableEntryIdStruct &&6016 if (expected_type->id == TypeTableEntryIdStruct &&
6017 expected_type->data.structure.is_slice &&6017 expected_type->data.structure.is_slice &&
6018 actual_type->id == TypeTableEntryIdArray)6018 actual_type->id == TypeTableEntryIdArray)
...@@ -6027,6 +6027,21 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,...@@ -6027,6 +6027,21 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
6027 }6027 }
6028 }6028 }
60296029
6030 //// implicit [N]T to %[]const T
6031 //if (expected_type->id == TypeTableEntryIdErrorUnion &&
6032 // is_slice(expected_type->data.error.child_type) &&
6033 // actual_type->id == TypeTableEntryIdArray)
6034 //{
6035 // TypeTableEntry *ptr_type =
6036 // expected_type->data.error.child_type->data.structure.fields[slice_ptr_index].type_entry;
6037 // assert(ptr_type->id == TypeTableEntryIdPointer);
6038 // if ((ptr_type->data.pointer.is_const || actual_type->data.array.len == 0) &&
6039 // types_match_const_cast_only(ptr_type->data.pointer.child_type, actual_type->data.array.child_type))
6040 // {
6041 // return ImplicitCastMatchResultYes;
6042 // }
6043 //}
6044
6030 // implicit [N]T to &const []const N6045 // implicit [N]T to &const []const N
6031 if (expected_type->id == TypeTableEntryIdPointer &&6046 if (expected_type->id == TypeTableEntryIdPointer &&
6032 expected_type->data.pointer.is_const &&6047 expected_type->data.pointer.is_const &&
...@@ -6799,6 +6814,8 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s...@@ -6799,6 +6814,8 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
6799 IrInstruction *array, TypeTableEntry *wanted_type)6814 IrInstruction *array, TypeTableEntry *wanted_type)
6800{6815{
6801 assert(is_slice(wanted_type));6816 assert(is_slice(wanted_type));
6817 // In this function we honor the const-ness of wanted_type, because
6818 // we may be casting [0]T to []const T which is perfectly valid.
68026819
6803 TypeTableEntry *array_type = array->value.type;6820 TypeTableEntry *array_type = array->value.type;
6804 assert(array_type->id == TypeTableEntryIdArray);6821 assert(array_type->id == TypeTableEntryIdArray);
...@@ -6807,6 +6824,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s...@@ -6807,6 +6824,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
6807 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,6824 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
6808 source_instr->source_node, wanted_type);6825 source_instr->source_node, wanted_type);
6809 init_const_slice(ira->codegen, &result->value, &array->value, 0, array_type->data.array.len, true);6826 init_const_slice(ira->codegen, &result->value, &array->value, 0, array_type->data.array.len, true);
6827 result->value.type = wanted_type;
6810 return result;6828 return result;
6811 }6829 }
68126830
...@@ -6822,8 +6840,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s...@@ -6822,8 +6840,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
68226840
6823 IrInstruction *result = ir_build_slice(&ira->new_irb, source_instr->scope,6841 IrInstruction *result = ir_build_slice(&ira->new_irb, source_instr->scope,
6824 source_instr->source_node, array_ptr, start, end, false);6842 source_instr->source_node, array_ptr, start, end, false);
6825 TypeTableEntry *child_type = array_type->data.array.child_type;6843 result->value.type = wanted_type;
6826 result->value.type = get_slice_type(ira->codegen, child_type, true);
6827 ir_add_alloca(ira, result, result->value.type);6844 ir_add_alloca(ira, result, result->value.type);
68286845
6829 return result;6846 return result;
...@@ -7200,6 +7217,29 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -7200,6 +7217,29 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
7200 }7217 }
7201 }7218 }
72027219
7220 // explicit cast from [N]T to %[]const T
7221 if (wanted_type->id == TypeTableEntryIdErrorUnion &&
7222 is_slice(wanted_type->data.error.child_type) &&
7223 actual_type->id == TypeTableEntryIdArray)
7224 {
7225 TypeTableEntry *ptr_type =
7226 wanted_type->data.error.child_type->data.structure.fields[slice_ptr_index].type_entry;
7227 assert(ptr_type->id == TypeTableEntryIdPointer);
7228 if ((ptr_type->data.pointer.is_const || actual_type->data.array.len == 0) &&
7229 types_match_const_cast_only(ptr_type->data.pointer.child_type, actual_type->data.array.child_type))
7230 {
7231 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_type->data.error.child_type, value);
7232 if (type_is_invalid(cast1->value.type))
7233 return ira->codegen->invalid_instruction;
7234
7235 IrInstruction *cast2 = ir_analyze_cast(ira, source_instr, wanted_type, cast1);
7236 if (type_is_invalid(cast2->value.type))
7237 return ira->codegen->invalid_instruction;
7238
7239 return cast2;
7240 }
7241 }
7242
7203 // explicit cast from pure error to error union type7243 // explicit cast from pure error to error union type
7204 if (wanted_type->id == TypeTableEntryIdErrorUnion &&7244 if (wanted_type->id == TypeTableEntryIdErrorUnion &&
7205 actual_type->id == TypeTableEntryIdPureError)7245 actual_type->id == TypeTableEntryIdPureError)
std/mem.zig+23-1
...@@ -10,8 +10,10 @@ error NoMem;...@@ -10,8 +10,10 @@ error NoMem;
10pub const Allocator = struct {10pub const Allocator = struct {
11 allocFn: fn (self: &Allocator, n: usize) -> %[]u8,11 allocFn: fn (self: &Allocator, n: usize) -> %[]u8,
12 /// Note that old_mem may be a slice of length 0, in which case reallocFn12 /// Note that old_mem may be a slice of length 0, in which case reallocFn
13 /// should simply call allocFn13 /// should simply call allocFn.
14 reallocFn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8,14 reallocFn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8,
15 /// Note that mem may be a slice of length 0, in which case freeFn
16 /// should do nothing.
15 freeFn: fn (self: &Allocator, mem: []u8),17 freeFn: fn (self: &Allocator, mem: []u8),
1618
17 /// Aborts the program if an allocation fails.19 /// Aborts the program if an allocation fails.
...@@ -228,6 +230,10 @@ pub fn eql_slice_u8(a: []const u8, b: []const u8) -> bool {...@@ -228,6 +230,10 @@ pub fn eql_slice_u8(a: []const u8, b: []const u8) -> bool {
228 return eql(u8, a, b);230 return eql(u8, a, b);
229}231}
230232
233/// Returns an iterator that iterates over the slices of ::s that are not
234/// the byte ::c.
235/// split(" abc def ghi ")
236/// Will return slices for "abc", "def", "ghi", null, in that order.
231pub fn split(s: []const u8, c: u8) -> SplitIterator {237pub fn split(s: []const u8, c: u8) -> SplitIterator {
232 SplitIterator {238 SplitIterator {
233 .index = 0,239 .index = 0,
...@@ -236,6 +242,14 @@ pub fn split(s: []const u8, c: u8) -> SplitIterator {...@@ -236,6 +242,14 @@ pub fn split(s: []const u8, c: u8) -> SplitIterator {
236 }242 }
237}243}
238244
245test "mem.split" {
246 var it = split(" abc def ghi ", ' ');
247 assert(eql(u8, ??it.next(), "abc"));
248 assert(eql(u8, ??it.next(), "def"));
249 assert(eql(u8, ??it.next(), "ghi"));
250 assert(it.next() == null);
251}
252
239pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) -> bool {253pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) -> bool {
240 return if (needle.len > haystack.len) false else eql(T, haystack[0...needle.len], needle);254 return if (needle.len > haystack.len) false else eql(T, haystack[0...needle.len], needle);
241}255}
...@@ -259,6 +273,14 @@ const SplitIterator = struct {...@@ -259,6 +273,14 @@ const SplitIterator = struct {
259273
260 return self.s[start...end];274 return self.s[start...end];
261 }275 }
276
277 /// Returns a slice of the remaining bytes. Does not affect iterator state.
278 pub fn rest(self: &const SplitIterator) -> []const u8 {
279 // move to beginning of token
280 var index: usize = self.index;
281 while (index < self.s.len and self.s[index] == self.c; index += 1) {}
282 return self.s[index...];
283 }
262};284};
263285
264test "testStringEquality" {286test "testStringEquality" {
std/os/path.zig+78-1
...@@ -3,8 +3,16 @@ const assert = debug.assert;...@@ -3,8 +3,16 @@ const assert = debug.assert;
3const mem = @import("../mem.zig");3const mem = @import("../mem.zig");
4const Allocator = mem.Allocator;4const Allocator = mem.Allocator;
5const os = @import("index.zig");5const os = @import("index.zig");
6const math = @import("../math.zig");
67
7pub const sep = '/';8pub const sep = switch (@compileVar("os")) {
9 Os.windows => '\\',
10 else => '/',
11};
12pub const delimiter = switch (@compileVar("os")) {
13 Os.windows => ';',
14 else => ':',
15};
816
9/// Naively combines a series of paths with the native path seperator.17/// Naively combines a series of paths with the native path seperator.
10/// Allocates memory for the result, which must be freed by the caller.18/// Allocates memory for the result, which must be freed by the caller.
...@@ -134,6 +142,7 @@ test "os.path.resolve" {...@@ -134,6 +142,7 @@ test "os.path.resolve" {
134 assert(mem.eql(u8, testResolve("/a/b", "c", "//d", "e///"), "/d/e"));142 assert(mem.eql(u8, testResolve("/a/b", "c", "//d", "e///"), "/d/e"));
135 assert(mem.eql(u8, testResolve("/a/b/c", "..", "../"), "/a"));143 assert(mem.eql(u8, testResolve("/a/b/c", "..", "../"), "/a"));
136 assert(mem.eql(u8, testResolve("/", "..", ".."), "/"));144 assert(mem.eql(u8, testResolve("/", "..", ".."), "/"));
145 assert(mem.eql(u8, testResolve("/a/b/c/"), "/a/b/c"));
137}146}
138fn testResolve(args: ...) -> []u8 {147fn testResolve(args: ...) -> []u8 {
139 return %%resolve(&debug.global_allocator, args);148 return %%resolve(&debug.global_allocator, args);
...@@ -175,3 +184,71 @@ test "os.path.dirname" {...@@ -175,3 +184,71 @@ test "os.path.dirname" {
175fn testDirname(input: []const u8, expected_output: []const u8) {184fn testDirname(input: []const u8, expected_output: []const u8) {
176 assert(mem.eql(u8, dirname(input), expected_output));185 assert(mem.eql(u8, dirname(input), expected_output));
177}186}
187
188/// Returns the relative path from ::from to ::to. If ::from and ::to each
189/// resolve to the same path (after calling ::resolve on each), a zero-length
190/// string is returned.
191pub fn relative(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u8 {
192 const resolved_from = %return resolve(allocator, from);
193 defer allocator.free(resolved_from);
194
195 const resolved_to = %return resolve(allocator, to);
196 defer allocator.free(resolved_to);
197
198 var from_it = mem.split(resolved_from, '/');
199 var to_it = mem.split(resolved_to, '/');
200 while (true) {
201 const from_component = from_it.next() ?? return mem.dupe(allocator, u8, to_it.rest());
202 const to_rest = to_it.rest();
203 test(to_it.next()) |to_component| {
204 if (mem.eql(u8, from_component, to_component))
205 continue;
206 }
207 var up_count: usize = 1;
208 while (true) {
209 _ = from_it.next() ?? break;
210 up_count += 1;
211 }
212 const up_index_end = up_count * "../".len;
213 const result = %return allocator.alloc(u8, up_index_end + to_rest.len);
214 %defer allocator.free(result);
215
216 var result_index: usize = 0;
217 while (result_index < up_index_end) {
218 result[result_index] = '.';
219 result_index += 1;
220 result[result_index] = '.';
221 result_index += 1;
222 result[result_index] = '/';
223 result_index += 1;
224 }
225 if (to_rest.len == 0) {
226 // shave off the trailing slash
227 return result[0...result_index - 1];
228 }
229
230 mem.copy(u8, result[result_index...], to_rest);
231 return result;
232 }
233
234 return []u8{};
235}
236
237test "os.path.relative" {
238 testRelative("/var/lib", "/var", "..");
239 testRelative("/var/lib", "/bin", "../../bin");
240 testRelative("/var/lib", "/var/lib", "");
241 testRelative("/var/lib", "/var/apache", "../apache");
242 testRelative("/var/", "/var/lib", "lib");
243 testRelative("/", "/var/lib", "var/lib");
244 testRelative("/foo/test", "/foo/test/bar/package.json", "bar/package.json");
245 testRelative("/Users/a/web/b/test/mails", "/Users/a/web/b", "../..");
246 testRelative("/foo/bar/baz-quux", "/foo/bar/baz", "../baz");
247 testRelative("/foo/bar/baz", "/foo/bar/baz-quux", "../baz-quux");
248 testRelative("/baz-quux", "/baz", "../baz");
249 testRelative("/baz", "/baz-quux", "../baz-quux");
250}
251fn testRelative(from: []const u8, to: []const u8, expected_output: []const u8) {
252 const result = %%relative(&debug.global_allocator, from, to);
253 assert(mem.eql(u8, result, expected_output));
254}
test/cases/cast.zig+14
...@@ -159,3 +159,17 @@ test "implicitly cast from [N]T to ?[]const T" {...@@ -159,3 +159,17 @@ test "implicitly cast from [N]T to ?[]const T" {
159fn castToMaybeSlice() -> ?[]const u8 {159fn castToMaybeSlice() -> ?[]const u8 {
160 return "hi";160 return "hi";
161}161}
162
163
164test "implicitly cast from [0]T to %[]T" {
165 testCastZeroArrayToErrSliceMut();
166 comptime testCastZeroArrayToErrSliceMut();
167}
168
169fn testCastZeroArrayToErrSliceMut() {
170 assert((%%gimmeErrOrSlice()).len == 0);
171}
172
173fn gimmeErrOrSlice() -> %[]u8 {
174 return []u8{};
175}