Commit 2182d28cb0

Andrew Kelley <andrew@ziglang.org>
2020-03-16 01:55:07
slicing with comptime start and end results in array
implements #863
1 parent e3c92d0
Changed files (3)
src/analyze.cpp
@@ -791,7 +791,10 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, Zi
         return existing_entry->value;
     }
 
-    assert(type_is_resolved(child_type, ResolveStatusSizeKnown));
+    Error err;
+    if ((err = type_resolve(g, child_type, ResolveStatusSizeKnown))) {
+        codegen_report_errors_and_exit(g);
+    }
 
     ZigType *entry = new_type_table_entry(ZigTypeIdArray);
 
src/codegen.cpp
@@ -5422,8 +5422,22 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
 
     bool want_runtime_safety = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);
 
-    ZigType *res_slice_ptr_type = instruction->base.value->type->data.structure.fields[slice_ptr_index]->type_entry;
-    ZigValue *sentinel = res_slice_ptr_type->data.pointer.sentinel;
+    ZigType *result_type = instruction->base.value->type;
+    if (!type_has_bits(g, result_type)) {
+        return nullptr;
+    }
+
+    ZigValue *sentinel = nullptr;
+    if (result_type->id == ZigTypeIdPointer) {
+        ZigType *result_array_type = result_type->data.pointer.child_type;
+        ir_assert(result_array_type->id == ZigTypeIdArray, &instruction->base);
+        sentinel = result_array_type->data.array.sentinel;
+    } else if (result_type->id == ZigTypeIdStruct) {
+        ZigType *res_slice_ptr_type = result_type->data.structure.fields[slice_ptr_index]->type_entry;
+        sentinel = res_slice_ptr_type->data.pointer.sentinel;
+    } else {
+        zig_unreachable();
+    }
 
     if (array_type->id == ZigTypeIdArray ||
         (array_type->id == ZigTypeIdPointer && array_type->data.pointer.ptr_len == PtrLenSingle))
@@ -5466,18 +5480,24 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
             return tmp_struct_ptr;
         }
 
-
-        LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
         LLVMValueRef indices[] = {
             LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
             start_val,
         };
         LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
-        gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
+        if (result_type->id == ZigTypeIdPointer) {
+            LLVMTypeRef result_ptr_type = get_llvm_type(g, result_type);
+            LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, slice_start_ptr, result_ptr_type, "");
+            gen_store_untyped(g, bitcasted, tmp_struct_ptr, 0, false);
+            return slice_start_ptr;
+        } else {
+            LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
+            gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
 
-        LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_len_index, "");
-        LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
-        gen_store_untyped(g, len_value, len_field_ptr, 0, false);
+            LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_len_index, "");
+            LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
+            gen_store_untyped(g, len_value, len_field_ptr, 0, false);
+        }
 
         return tmp_struct_ptr;
     } else if (array_type->id == ZigTypeIdPointer) {
@@ -5493,14 +5513,21 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
             }
         }
 
+        LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, &start_val, 1, "");
+        if (result_type->id == ZigTypeIdPointer) {
+            LLVMTypeRef result_ptr_type = get_llvm_type(g, result_type);
+            LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, slice_start_ptr, result_ptr_type, "");
+            gen_store_untyped(g, bitcasted, tmp_struct_ptr, 0, false);
+            return bitcasted;
+        }
+
         if (type_has_bits(g, array_type)) {
-            size_t gen_ptr_index = instruction->base.value->type->data.structure.fields[slice_ptr_index]->gen_index;
+            size_t gen_ptr_index = result_type->data.structure.fields[slice_ptr_index]->gen_index;
             LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_ptr_index, "");
-            LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, &start_val, 1, "");
             gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
         }
 
-        size_t gen_len_index = instruction->base.value->type->data.structure.fields[slice_len_index]->gen_index;
+        size_t gen_len_index = result_type->data.structure.fields[slice_len_index]->gen_index;
         LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_len_index, "");
         LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
         gen_store_untyped(g, len_value, len_field_ptr, 0, false);
@@ -5510,7 +5537,9 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
         assert(array_type->data.structure.special == StructSpecialSlice);
         assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind);
         assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(array_ptr))) == LLVMStructTypeKind);
-        assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(tmp_struct_ptr))) == LLVMStructTypeKind);
+        if (result_type->id != ZigTypeIdPointer) {
+            assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(tmp_struct_ptr))) == LLVMStructTypeKind);
+        }
 
         size_t ptr_index = array_type->data.structure.fields[slice_ptr_index]->gen_index;
         assert(ptr_index != SIZE_MAX);
@@ -5547,15 +5576,22 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
             }
         }
 
-        LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)ptr_index, "");
         LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, src_ptr, &start_val, 1, "");
-        gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
+        if (result_type->id == ZigTypeIdPointer) {
+            LLVMTypeRef result_ptr_type = get_llvm_type(g, result_type);
+            LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, slice_start_ptr, result_ptr_type, "");
+            gen_store_untyped(g, bitcasted, tmp_struct_ptr, 0, false);
+            return bitcasted;
+        } else {
+            LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)ptr_index, "");
+            gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
 
-        LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)len_index, "");
-        LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
-        gen_store_untyped(g, len_value, len_field_ptr, 0, false);
+            LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)len_index, "");
+            LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
+            gen_store_untyped(g, len_value, len_field_ptr, 0, false);
 
-        return tmp_struct_ptr;
+            return tmp_struct_ptr;
+        }
     } else {
         zig_unreachable();
     }
src/ir.cpp
@@ -849,11 +849,6 @@ static bool is_slice(ZigType *type) {
     return type->id == ZigTypeIdStruct && type->data.structure.special == StructSpecialSlice;
 }
 
-static bool slice_is_const(ZigType *type) {
-    assert(is_slice(type));
-    return type->data.structure.fields[slice_ptr_index]->type_entry->data.pointer.is_const;
-}
-
 // This function returns true when you can change the type of a ZigValue and the
 // value remains meaningful.
 static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expected, ZigType *actual) {
@@ -26206,7 +26201,6 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
         return ira->codegen->invalid_inst_gen;
     }
 
-    ZigType *return_type;
     ZigValue *sentinel_val = nullptr;
     if (instruction->sentinel) {
         IrInstGen *uncasted_sentinel = instruction->sentinel->child;
@@ -26218,6 +26212,46 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
         sentinel_val = ir_resolve_const(ira, sentinel, UndefBad);
         if (sentinel_val == nullptr)
             return ira->codegen->invalid_inst_gen;
+    }
+
+    // If start index and end index are both comptime known, then the result type is a pointer to array
+    // not a slice.
+    ZigType *return_type;
+
+    if (value_is_comptime(casted_start->value) &&
+        ((end != nullptr && value_is_comptime(end->value)) || array_type->id == ZigTypeIdArray ))
+    {
+        ZigValue *start_val = ir_resolve_const(ira, casted_start, UndefBad);
+        if (!start_val)
+            return ira->codegen->invalid_inst_gen;
+
+        uint64_t start_scalar = bigint_as_u64(&start_val->data.x_bigint);
+
+        uint64_t end_scalar;
+        if (end != nullptr) {
+            ZigValue *end_val = ir_resolve_const(ira, end, UndefBad);
+            if (!end_val)
+                return ira->codegen->invalid_inst_gen;
+            end_scalar = bigint_as_u64(&end_val->data.x_bigint);
+        } else {
+            end_scalar = array_type->data.array.len;
+        }
+        ZigValue *array_sentinel = (array_type->id == ZigTypeIdArray && end_scalar == array_type->data.array.len)
+            ? sentinel_val : nullptr;
+
+        if (start_scalar > end_scalar) {
+            ir_add_error(ira, &instruction->base.base, buf_sprintf("out of bounds slice"));
+            return ira->codegen->invalid_inst_gen;
+        }
+
+        ZigType *return_array_type = get_array_type(ira->codegen, elem_type, end_scalar - start_scalar,
+                array_sentinel);
+        return_type = get_pointer_to_type_extra(ira->codegen, return_array_type,
+                non_sentinel_slice_ptr_type->data.pointer.is_const,
+                non_sentinel_slice_ptr_type->data.pointer.is_volatile,
+                PtrLenSingle,
+                0, 0, 0, false);
+    } else if (sentinel_val != nullptr) {
         ZigType *slice_ptr_type = adjust_ptr_sentinel(ira->codegen, non_sentinel_slice_ptr_type, sentinel_val);
         return_type = get_slice_type(ira->codegen, slice_ptr_type);
     } else {
@@ -26401,15 +26435,25 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
         }
 
         IrInstGen *result = ir_const(ira, &instruction->base.base, return_type);
-        ZigValue *out_val = result->value;
-        out_val->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, 2);
 
-        ZigValue *ptr_val = out_val->data.x_struct.fields[slice_ptr_index];
+        ZigValue *ptr_val;
+        if (return_type->id == ZigTypeIdPointer) {
+            // pointer to array
+            ptr_val = result->value;
+        } else {
+            // slice
+            result->value->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, 2);
+
+            ptr_val = result->value->data.x_struct.fields[slice_ptr_index];
+
+            ZigValue *len_val = result->value->data.x_struct.fields[slice_len_index];
+            init_const_usize(ira->codegen, len_val, end_scalar - start_scalar);
+        }
 
+        bool return_type_is_const = non_sentinel_slice_ptr_type->data.pointer.is_const;
         if (array_val) {
             size_t index = abs_offset + start_scalar;
-            bool is_const = slice_is_const(return_type);
-            init_const_ptr_array(ira->codegen, ptr_val, array_val, index, is_const, PtrLenUnknown);
+            init_const_ptr_array(ira->codegen, ptr_val, array_val, index, return_type_is_const, PtrLenUnknown);
             if (array_type->id == ZigTypeIdArray) {
                 ptr_val->data.x_ptr.mut = ptr_ptr->value->data.x_ptr.mut;
             } else if (is_slice(array_type)) {
@@ -26419,15 +26463,15 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
             }
         } else if (ptr_is_undef) {
             ptr_val->type = get_pointer_to_type(ira->codegen, parent_ptr->type->data.pointer.child_type,
-                    slice_is_const(return_type));
+                    return_type_is_const);
             ptr_val->special = ConstValSpecialUndef;
         } else switch (parent_ptr->data.x_ptr.special) {
             case ConstPtrSpecialInvalid:
             case ConstPtrSpecialDiscard:
                 zig_unreachable();
             case ConstPtrSpecialRef:
-                init_const_ptr_ref(ira->codegen, ptr_val,
-                        parent_ptr->data.x_ptr.data.ref.pointee, slice_is_const(return_type));
+                init_const_ptr_ref(ira->codegen, ptr_val, parent_ptr->data.x_ptr.data.ref.pointee,
+                        return_type_is_const);
                 break;
             case ConstPtrSpecialBaseArray:
                 zig_unreachable();
@@ -26443,7 +26487,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
                 init_const_ptr_hard_coded_addr(ira->codegen, ptr_val,
                     parent_ptr->type->data.pointer.child_type,
                     parent_ptr->data.x_ptr.data.hard_coded_addr.addr + start_scalar,
-                    slice_is_const(return_type));
+                    return_type_is_const);
                 break;
             case ConstPtrSpecialFunction:
                 zig_panic("TODO");
@@ -26451,9 +26495,8 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
                 zig_panic("TODO");
         }
 
-        ZigValue *len_val = out_val->data.x_struct.fields[slice_len_index];
-        init_const_usize(ira->codegen, len_val, end_scalar - start_scalar);
-
+        // In the case of pointer-to-array, we must restore this because above it overwrites ptr_val->type
+        result->value->type = return_type;
         return result;
     }