Commit c8a7ab7eff

Andrew Kelley <superjoe30@gmail.com>
2016-12-26 22:04:14
IR: pass cStringConcatenation test
1 parent 7504be9
Changed files (4)
src/ir.cpp
@@ -6173,8 +6173,9 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
     TypeTableEntry *result_type;
     ConstExprValue *out_array_val;
     size_t new_len = (op1_array_end - op1_array_index) + (op2_array_end - op2_array_index);
+    TypeTableEntry *out_array_type = get_array_type(ira->codegen, child_type, new_len);
     if (op1_canon_type->id == TypeTableEntryIdArray || op2_canon_type->id == TypeTableEntryIdArray) {
-        result_type = get_array_type(ira->codegen, child_type, new_len);
+        result_type = out_array_type;
 
         out_array_val = out_val;
     } else {
@@ -6182,7 +6183,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
 
         out_array_val = allocate<ConstExprValue>(1);
         out_array_val->special = ConstValSpecialStatic;
-        out_array_val->type = result_type;
+        out_array_val->type = out_array_type;
         out_val->data.x_ptr.base_ptr = out_array_val;
         out_val->data.x_ptr.index = 0;
         out_val->data.x_ptr.special = ConstPtrSpecialCStr;
src/ir_print.cpp
@@ -100,6 +100,8 @@ static void ir_print_const_value(IrPrint *irp, ConstExprValue *const_val) {
             fprintf(irp->f, "&");
             if (const_val->data.x_ptr.special == ConstPtrSpecialRuntime) {
                 fprintf(irp->f, "(runtime pointer value)");
+            } else if (const_val->data.x_ptr.special == ConstPtrSpecialCStr) {
+                fprintf(irp->f, "(c str lit)");
             } else {
                 ir_print_const_value(irp, const_ptr_pointee(const_val));
             }
test/cases/misc.zig
@@ -436,6 +436,28 @@ fn ptrEql(a: &[]const u8, b: &[]const u8) -> bool {
 }
 
 
+fn cStringConcatenation() {
+    @setFnTest(this);
+
+    const a = c"OK" ++ c" IT " ++ c"WORKED";
+    const b = c"OK IT WORKED";
+
+    const len = cstrlen(b);
+    const len_with_null = len + 1;
+    {var i: u32 = 0; while (i < len_with_null; i += 1) {
+        assert(a[i] == b[i]);
+    }}
+    assert(a[len] == 0);
+    assert(b[len] == 0);
+}
+
+// TODO import from std.cstr
+pub fn cstrlen(ptr: &const u8) -> usize {
+    var count: usize = 0;
+    while (ptr[count] != 0; count += 1) {}
+    return count;
+}
+
 // TODO import from std.str
 pub fn memeql(a: []const u8, b: []const u8) -> bool {
     sliceEql(u8, a, b)
test/self_hosted.zig
@@ -58,22 +58,6 @@ fn returnsTen() -> %i32 {
     10
 }
 
-// TODO not passing
-fn cStringConcatenation() {
-    @setFnTest(this, true);
-
-    const a = c"OK" ++ c" IT " ++ c"WORKED";
-    const b = c"OK IT WORKED";
-
-    const len = cstrlen(b);
-    const len_with_null = len + 1;
-    {var i: u32 = 0; while (i < len_with_null; i += 1) {
-        assert(a[i] == b[i]);
-    }}
-    assert(a[len] == 0);
-    assert(b[len] == 0);
-}
-
 // TODO not passing
 fn castSliceToU8Slice() {
     @setFnTest(this);