From d9eabde319bc035f710b106808022e9b5872f728 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 13 Sep 2017 14:30:57 -0400 Subject: [PATCH] add Child property of slice type also rename child field to Child for pointer and array --- cmake/Findllvm.cmake | 4 +++- src/ir.cpp | 15 +++++++++++++-- std/fmt/index.zig | 2 +- test/cases/array.zig | 2 +- test/cases/misc.zig | 2 +- test/cases/slice.zig | 6 ++++++ 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/cmake/Findllvm.cmake b/cmake/Findllvm.cmake index b57107ca3a7dacab1bcd6d733bfbfbb35076ea40..f76735d3bf1d6a9bb549ae2caecf7897c38216d9 100644 --- a/cmake/Findllvm.cmake +++ b/cmake/Findllvm.cmake @@ -104,9 +104,11 @@ else() set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS}) - if(LLVM_LIBRARY) + if(LLVM_LIBRARY AND NOT LLVM_LIBRARIES) set(LLVM_LIBRARIES ${LLVM_LIBRARY}) endif() + + link_directories("${CMAKE_PREFIX_PATH}/lib") endif() diff --git a/src/ir.cpp b/src/ir.cpp index a06fc261c4c6fc8b6539201afae4d477bd7158c6..6b887a6d8385389da6ecfd24aae7023bcb7d5ec9 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11432,6 +11432,17 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru if (type_is_invalid(child_type)) { return ira->codegen->builtin_types.entry_invalid; } else if (is_container(child_type)) { + if (is_slice(child_type) && buf_eql_str(field_name, "Child")) { + bool ptr_is_const = true; + bool ptr_is_volatile = false; + TypeStructField *ptr_field = &child_type->data.structure.fields[slice_ptr_index]; + assert(ptr_field->type_entry->id == TypeTableEntryIdPointer); + TypeTableEntry *child_type = ptr_field->type_entry->data.pointer.child_type; + return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, + create_const_type(ira->codegen, child_type), + ira->codegen->builtin_types.entry_type, + ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile); + } if (child_type->id == TypeTableEntryIdEnum) { ensure_complete_type(ira->codegen, child_type); if (child_type->data.enumeration.is_invalid) @@ -11522,7 +11533,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru return ira->codegen->builtin_types.entry_invalid; } } else if (child_type->id == TypeTableEntryIdPointer) { - if (buf_eql_str(field_name, "child")) { + if (buf_eql_str(field_name, "Child")) { bool ptr_is_const = true; bool ptr_is_volatile = false; return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, @@ -11544,7 +11555,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru return ira->codegen->builtin_types.entry_invalid; } } else if (child_type->id == TypeTableEntryIdArray) { - if (buf_eql_str(field_name, "child")) { + if (buf_eql_str(field_name, "Child")) { bool ptr_is_const = true; bool ptr_is_volatile = false; return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, diff --git a/std/fmt/index.zig b/std/fmt/index.zig index a026789663ba9a48012ea635a82d4ca862c0fe3d..0993bd5e2bb726678e3defa8a0dfca44b4f3a002 100644 --- a/std/fmt/index.zig +++ b/std/fmt/index.zig @@ -208,7 +208,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons return output(context, @errorName(value)); }, builtin.TypeId.Pointer => { - if (@typeId(T.child) == builtin.TypeId.Array and T.child.child == u8) { + if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) { return output(context, (*value)[0..]); } else { @compileError("Unable to format type '" ++ @typeName(T) ++ "'"); diff --git a/test/cases/array.zig b/test/cases/array.zig index 9e9d83c2e1858560e29e476d16a9ad85f7216b35..a6fa07b0048b845ba36127080d0790d83cba95fd 100644 --- a/test/cases/array.zig +++ b/test/cases/array.zig @@ -89,7 +89,7 @@ test "array literal with specified size" { test "array child property" { var x: [5]i32 = undefined; - assert(@typeOf(x).child == i32); + assert(@typeOf(x).Child == i32); } test "array len property" { diff --git a/test/cases/misc.zig b/test/cases/misc.zig index 47e7ea5e1b1f9fd450c1c56bef51da7b559da72c..76e1b829ee43ab0741df7542603c67ef85326941 100644 --- a/test/cases/misc.zig +++ b/test/cases/misc.zig @@ -536,7 +536,7 @@ export fn writeToVRam() { } test "pointer child field" { - assert((&u32).child == u32); + assert((&u32).Child == u32); } const OpaqueA = @OpaqueType(); diff --git a/test/cases/slice.zig b/test/cases/slice.zig index 53a5b5f64b9f5f55ab55e092e1b4f8fe985dd449..1498f948eaab21a7728cab3b7f45f2cde598d9de 100644 --- a/test/cases/slice.zig +++ b/test/cases/slice.zig @@ -9,3 +9,9 @@ test "compile time slice of pointer to hard coded address" { assert(@ptrToInt(y.ptr) == 0x1100); assert(y.len == 0x400); } + +test "slice child property" { + var array: [5]i32 = undefined; + var slice = array[0..]; + assert(@typeOf(slice).Child == i32); +} -- 2.54.0