From c19bdc2d370f3a72ef492e2f4e6264e2a0dd91ec Mon Sep 17 00:00:00 2001 From: Shawn Landden Date: Mon, 6 May 2019 11:41:15 -0500 Subject: stage1: add @hasField() built-in This was quite straight-forward Closes: #1439 --- src/ir.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 91f931462e..fdd7291de3 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -851,6 +851,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeInfo *) { return IrInstructionIdTypeInfo; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionHasField *) { + return IrInstructionIdHasField; +} + static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeId *) { return IrInstructionIdTypeId; } @@ -1280,6 +1284,20 @@ static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode * return &instruction->base; } +static IrInstruction *ir_build_has_field(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *container_type, IrInstruction *field_name_expr) +{ + IrInstructionHasField *instruction = ir_build_instruction(irb, scope, source_node); + instruction->container_type = container_type; + instruction->field_name_buffer = nullptr; + instruction->field_name_expr = field_name_expr; + + ir_ref_instruction(container_type, irb->current_basic_block); + ir_ref_instruction(field_name_expr, irb->current_basic_block); + + return &instruction->base; +} + static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *struct_ptr, TypeStructField *field) { @@ -4598,6 +4616,21 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return ir_build_load_ptr(irb, scope, node, ptr_instruction); } + case BuiltinFnIdHasField: + { + AstNode *arg0_node = node->data.fn_call_expr.params.at(0); + IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); + if (arg0_value == irb->codegen->invalid_instruction) + return arg0_value; + + AstNode *arg1_node = node->data.fn_call_expr.params.at(1); + IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope); + if (arg1_value == irb->codegen->invalid_instruction) + return arg1_value; + + IrInstruction *type_info = ir_build_has_field(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, type_info, lval); + } case BuiltinFnIdTypeInfo: { AstNode *arg0_node = node->data.fn_call_expr.params.at(0); @@ -20578,6 +20611,40 @@ static IrInstruction *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInstr } } +static IrInstruction *ir_analyze_instruction_has_field(IrAnalyze *ira, IrInstructionHasField *instruction) { + Error err; + IrInstruction *container_type_value = instruction->container_type->child; + ZigType *container_type = ir_resolve_type(ira, container_type_value); + if (type_is_invalid(container_type)) + return ira->codegen->invalid_instruction; + + if ((err = ensure_complete_type(ira->codegen, container_type))) + return ira->codegen->invalid_instruction; + + Buf *field_name = instruction->field_name_buffer; + if (!field_name) { + IrInstruction *field_name_expr = instruction->field_name_expr->child; + field_name = ir_resolve_str(ira, field_name_expr); + if (!field_name) + return ira->codegen->invalid_instruction; + } + + bool result; + if (container_type->id == ZigTypeIdStruct) + result = (bool)find_struct_type_field(container_type, field_name); + else if (container_type->id == ZigTypeIdEnum) + result = (bool)find_enum_type_field(container_type, field_name); + else if (container_type->id == ZigTypeIdUnion) + result = (bool)find_union_type_field(container_type, field_name); + else { + ir_add_error(ira, container_type_value, + buf_sprintf("type '%s' does not support @memberName", buf_ptr(&container_type->name))); + return ira->codegen->invalid_instruction; + } + return ir_build_const_bool(&ira->new_irb, + instruction->base.scope, instruction->base.source_node, result); +} + static IrInstruction *ir_analyze_instruction_breakpoint(IrAnalyze *ira, IrInstructionBreakpoint *instruction) { IrInstruction *result = ir_build_breakpoint(&ira->new_irb, instruction->base.scope, instruction->base.source_node); @@ -23068,6 +23135,8 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio return ir_analyze_instruction_bit_offset_of(ira, (IrInstructionBitOffsetOf *)instruction); case IrInstructionIdTypeInfo: return ir_analyze_instruction_type_info(ira, (IrInstructionTypeInfo *) instruction); + case IrInstructionIdHasField: + return ir_analyze_instruction_has_field(ira, (IrInstructionHasField *) instruction); case IrInstructionIdTypeId: return ir_analyze_instruction_type_id(ira, (IrInstructionTypeId *)instruction); case IrInstructionIdSetEvalBranchQuota: @@ -23355,6 +23424,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdByteOffsetOf: case IrInstructionIdBitOffsetOf: case IrInstructionIdTypeInfo: + case IrInstructionIdHasField: case IrInstructionIdTypeId: case IrInstructionIdAlignCast: case IrInstructionIdOpaqueType: -- cgit v1.2.3 From df11512f85e5228ee2393e9dc5fc42294ecba1c9 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 2 Jul 2019 16:52:55 -0400 Subject: fixups --- doc/langref.html.in | 23 +++++++++++--------- src/all_types.hpp | 3 +-- src/ir.cpp | 45 +++++++++++++++++---------------------- src/ir_print.cpp | 2 +- test/compile_errors.zig | 9 ++++++++ test/stage1/behavior/hasfield.zig | 11 ++++++++-- 6 files changed, 52 insertions(+), 41 deletions(-) (limited to 'src/ir.cpp') diff --git a/doc/langref.html.in b/doc/langref.html.in index 78c2723148..fda741930b 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -6801,6 +6801,19 @@ test "@hasDecl" { assert(!@hasDecl(Foo, "nope1234")); } {#code_end#} + {#see_also|@hasField#} + {#header_close#} + + {#header_open|@hasField#} +
{#syntax#}@hasField(comptime T: type, comptime name: []const u8) bool{#endsyntax#}
+

Returns whether the field name of a struct, union, or enum exists.

+

+ The result is a compile time constant. +

+

+ It does not include functions, variables, or constants. +

+ {#see_also|@hasDecl#} {#header_close#} {#header_open|@import#} @@ -6940,16 +6953,6 @@ fn add(a: i32, b: i32) i32 { return a + b; } It does not include functions, variables, or constants.

{#header_close#} - {#header_open|@hasField#} -
{#syntax#}@hasField(comptime T: type, comptime name: []const u8) bool{#endsyntax#}
-

Returns if the field name of a struct, union, or enum exists.

-

- The result is a compile time constant. -

-

- It does not include functions, variables, constants. -

- {#header_close#} {#header_open|@memberType#}
{#syntax#}@memberType(comptime T: type, comptime index: usize) type{#endsyntax#}

Returns the field type of a struct or union.

diff --git a/src/all_types.hpp b/src/all_types.hpp index ada08983a6..a9304fdd0f 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -3339,8 +3339,7 @@ struct IrInstructionHasField { IrInstruction base; IrInstruction *container_type; - Buf *field_name_buffer; - IrInstruction *field_name_expr; + IrInstruction *field_name; }; struct IrInstructionTypeId { diff --git a/src/ir.cpp b/src/ir.cpp index 6f3ea2220b..e6cc5f2342 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1380,15 +1380,14 @@ static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode * } static IrInstruction *ir_build_has_field(IrBuilder *irb, Scope *scope, AstNode *source_node, - IrInstruction *container_type, IrInstruction *field_name_expr) + IrInstruction *container_type, IrInstruction *field_name) { IrInstructionHasField *instruction = ir_build_instruction(irb, scope, source_node); instruction->container_type = container_type; - instruction->field_name_buffer = nullptr; - instruction->field_name_expr = field_name_expr; + instruction->field_name = field_name; ir_ref_instruction(container_type, irb->current_basic_block); - ir_ref_instruction(field_name_expr, irb->current_basic_block); + ir_ref_instruction(field_name, irb->current_basic_block); return &instruction->base; } @@ -5132,7 +5131,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return arg1_value; IrInstruction *type_info = ir_build_has_field(irb, scope, node, arg0_value, arg1_value); - return ir_lval_wrap(irb, scope, type_info, lval); + return ir_lval_wrap(irb, scope, type_info, lval, result_loc); } case BuiltinFnIdTypeInfo: { @@ -22655,36 +22654,30 @@ static IrInstruction *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInstr static IrInstruction *ir_analyze_instruction_has_field(IrAnalyze *ira, IrInstructionHasField *instruction) { Error err; - IrInstruction *container_type_value = instruction->container_type->child; - ZigType *container_type = ir_resolve_type(ira, container_type_value); + ZigType *container_type = ir_resolve_type(ira, instruction->container_type->child); if (type_is_invalid(container_type)) return ira->codegen->invalid_instruction; - if ((err = ensure_complete_type(ira->codegen, container_type))) + if ((err = type_resolve(ira->codegen, container_type, ResolveStatusZeroBitsKnown))) return ira->codegen->invalid_instruction; - Buf *field_name = instruction->field_name_buffer; - if (!field_name) { - IrInstruction *field_name_expr = instruction->field_name_expr->child; - field_name = ir_resolve_str(ira, field_name_expr); - if (!field_name) - return ira->codegen->invalid_instruction; - } + Buf *field_name = ir_resolve_str(ira, instruction->field_name->child); + if (field_name == nullptr) + return ira->codegen->invalid_instruction; bool result; - if (container_type->id == ZigTypeIdStruct) - result = (bool)find_struct_type_field(container_type, field_name); - else if (container_type->id == ZigTypeIdEnum) - result = (bool)find_enum_type_field(container_type, field_name); - else if (container_type->id == ZigTypeIdUnion) - result = (bool)find_union_type_field(container_type, field_name); - else { - ir_add_error(ira, container_type_value, - buf_sprintf("type '%s' does not support @memberName", buf_ptr(&container_type->name))); + if (container_type->id == ZigTypeIdStruct) { + result = find_struct_type_field(container_type, field_name) != nullptr; + } else if (container_type->id == ZigTypeIdEnum) { + result = find_enum_type_field(container_type, field_name) != nullptr; + } else if (container_type->id == ZigTypeIdUnion) { + result = find_union_type_field(container_type, field_name) != nullptr; + } else { + ir_add_error(ira, instruction->container_type, + buf_sprintf("type '%s' does not support @hasField", buf_ptr(&container_type->name))); return ira->codegen->invalid_instruction; } - return ir_build_const_bool(&ira->new_irb, - instruction->base.scope, instruction->base.source_node, result); + return ir_const_bool(ira, &instruction->base, result); } static IrInstruction *ir_analyze_instruction_breakpoint(IrAnalyze *ira, IrInstructionBreakpoint *instruction) { diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 3b6ee3273e..31b7e608b7 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1274,7 +1274,7 @@ static void ir_print_has_field(IrPrint *irp, IrInstructionHasField *instruction) fprintf(irp->f, "@hasField("); ir_print_other_instruction(irp, instruction->container_type); fprintf(irp->f, ","); - ir_print_other_instruction(irp, instruction->field_name_expr); + ir_print_other_instruction(irp, instruction->field_name); fprintf(irp->f, ")"); } diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 94cd152eb7..e0ac76bbf0 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,15 @@ const tests = @import("tests.zig"); const builtin = @import("builtin"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add( + "wrong type to @hasField", + \\export fn entry() bool { + \\ return @hasField(i32, "hi"); + \\} + , + "tmp.zig:2:22: error: type 'i32' does not support @hasField", + ); + cases.add( "slice passed as array init type with elems", \\export fn entry() void { diff --git a/test/stage1/behavior/hasfield.zig b/test/stage1/behavior/hasfield.zig index 0e18cb19b5..c179fedd56 100644 --- a/test/stage1/behavior/hasfield.zig +++ b/test/stage1/behavior/hasfield.zig @@ -5,26 +5,33 @@ test "@hasField" { const struc = struct { a: i32, b: []u8, + + pub const nope = 1; }; expect(@hasField(struc, "a") == true); expect(@hasField(struc, "b") == true); expect(@hasField(struc, "non-existant") == false); + expect(@hasField(struc, "nope") == false); const unin = union { a: u64, b: []u16, + + pub const nope = 1; }; expect(@hasField(unin, "a") == true); expect(@hasField(unin, "b") == true); expect(@hasField(unin, "non-existant") == false); + expect(@hasField(unin, "nope") == false); const enm = enum { a, b, + + pub const nope = 1; }; expect(@hasField(enm, "a") == true); expect(@hasField(enm, "b") == true); expect(@hasField(enm, "non-existant") == false); - - expect(@hasField(builtin, "os") == true); + expect(@hasField(enm, "nope") == false); } -- cgit v1.2.3