authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-02 16:21:40-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-02 16:21:40-04:00
log140335b99fd9b26c2a827cb464fef68df7636a63
tree4a4fdf56706d4eaee8099cce8a8318744366515e
parentb05b5649df341573c0a42de503ceb025c4129473
parent5a91dbc16cbe20a95aafb6b3221c4a5a07d9b117
signature Commit is signed but in an unrecognized format.

Merge branch 'has-field' of https://github.com/shawnl/zig into shawnl-has-field


7 files changed, 134 insertions(+), 0 deletions(-)

doc/langref.html.in+10
......@@ -6940,6 +6940,16 @@ fn add(a: i32, b: i32) i32 { return a + b; }
69406940 It does not include functions, variables, or constants.
69416941 </p>
69426942 {#header_close#}
6943 {#header_open|@hasField#}
6944 <pre>{#syntax#}@hasField(comptime T: type, comptime name: []const u8) bool{#endsyntax#}</pre>
6945 <p>Returns if the field name of a struct, union, or enum exists.</p>
6946 <p>
6947 The result is a compile time constant.
6948 </p>
6949 <p>
6950 It does not include functions, variables, constants.
6951 </p>
6952 {#header_close#}
69436953 {#header_open|@memberType#}
69446954 <pre>{#syntax#}@memberType(comptime T: type, comptime index: usize) type{#endsyntax#}</pre>
69456955 <p>Returns the field type of a struct or union.</p>
src/all_types.hpp+10
......@@ -1416,6 +1416,7 @@ enum BuiltinFnId {
14161416 BuiltinFnIdMemberName,
14171417 BuiltinFnIdField,
14181418 BuiltinFnIdTypeInfo,
1419 BuiltinFnIdHasField,
14191420 BuiltinFnIdTypeof,
14201421 BuiltinFnIdAddWithOverflow,
14211422 BuiltinFnIdSubWithOverflow,
......@@ -2307,6 +2308,7 @@ enum IrInstructionId {
23072308 IrInstructionIdByteOffsetOf,
23082309 IrInstructionIdBitOffsetOf,
23092310 IrInstructionIdTypeInfo,
2311 IrInstructionIdHasField,
23102312 IrInstructionIdTypeId,
23112313 IrInstructionIdSetEvalBranchQuota,
23122314 IrInstructionIdPtrType,
......@@ -3333,6 +3335,14 @@ struct IrInstructionTypeInfo {
33333335 IrInstruction *type_value;
33343336};
33353337
3338struct IrInstructionHasField {
3339 IrInstruction base;
3340
3341 IrInstruction *container_type;
3342 Buf *field_name_buffer;
3343 IrInstruction *field_name_expr;
3344};
3345
33363346struct IrInstructionTypeId {
33373347 IrInstruction base;
33383348
src/codegen.cpp+2
......@@ -5592,6 +5592,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
55925592 case IrInstructionIdByteOffsetOf:
55935593 case IrInstructionIdBitOffsetOf:
55945594 case IrInstructionIdTypeInfo:
5595 case IrInstructionIdHasField:
55955596 case IrInstructionIdTypeId:
55965597 case IrInstructionIdSetEvalBranchQuota:
55975598 case IrInstructionIdPtrType:
......@@ -7328,6 +7329,7 @@ static void define_builtin_fns(CodeGen *g) {
73287329 create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2);
73297330 create_builtin_fn(g, BuiltinFnIdField, "field", 2);
73307331 create_builtin_fn(g, BuiltinFnIdTypeInfo, "typeInfo", 1);
7332 create_builtin_fn(g, BuiltinFnIdHasField, "hasField", 2);
73317333 create_builtin_fn(g, BuiltinFnIdTypeof, "typeOf", 1); // TODO rename to TypeOf
73327334 create_builtin_fn(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4);
73337335 create_builtin_fn(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4);
src/ir.cpp+70
......@@ -897,6 +897,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeInfo *) {
897897 return IrInstructionIdTypeInfo;
898898}
899899
900static constexpr IrInstructionId ir_instruction_id(IrInstructionHasField *) {
901 return IrInstructionIdHasField;
902}
903
900904static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeId *) {
901905 return IrInstructionIdTypeId;
902906}
......@@ -1375,6 +1379,20 @@ static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode *
13751379 return &instruction->base;
13761380}
13771381
1382static IrInstruction *ir_build_has_field(IrBuilder *irb, Scope *scope, AstNode *source_node,
1383 IrInstruction *container_type, IrInstruction *field_name_expr)
1384{
1385 IrInstructionHasField *instruction = ir_build_instruction<IrInstructionHasField>(irb, scope, source_node);
1386 instruction->container_type = container_type;
1387 instruction->field_name_buffer = nullptr;
1388 instruction->field_name_expr = field_name_expr;
1389
1390 ir_ref_instruction(container_type, irb->current_basic_block);
1391 ir_ref_instruction(field_name_expr, irb->current_basic_block);
1392
1393 return &instruction->base;
1394}
1395
13781396static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
13791397 IrInstruction *struct_ptr, TypeStructField *field)
13801398{
......@@ -5101,6 +5119,21 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
51015119 IrInstruction *load_ptr = ir_build_load_ptr(irb, scope, node, ptr_instruction);
51025120 return ir_expr_wrap(irb, scope, load_ptr, result_loc);
51035121 }
5122 case BuiltinFnIdHasField:
5123 {
5124 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
5125 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
5126 if (arg0_value == irb->codegen->invalid_instruction)
5127 return arg0_value;
5128
5129 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
5130 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
5131 if (arg1_value == irb->codegen->invalid_instruction)
5132 return arg1_value;
5133
5134 IrInstruction *type_info = ir_build_has_field(irb, scope, node, arg0_value, arg1_value);
5135 return ir_lval_wrap(irb, scope, type_info, lval);
5136 }
51045137 case BuiltinFnIdTypeInfo:
51055138 {
51065139 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -22620,6 +22653,40 @@ static IrInstruction *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInstr
2262022653 }
2262122654}
2262222655
22656static IrInstruction *ir_analyze_instruction_has_field(IrAnalyze *ira, IrInstructionHasField *instruction) {
22657 Error err;
22658 IrInstruction *container_type_value = instruction->container_type->child;
22659 ZigType *container_type = ir_resolve_type(ira, container_type_value);
22660 if (type_is_invalid(container_type))
22661 return ira->codegen->invalid_instruction;
22662
22663 if ((err = ensure_complete_type(ira->codegen, container_type)))
22664 return ira->codegen->invalid_instruction;
22665
22666 Buf *field_name = instruction->field_name_buffer;
22667 if (!field_name) {
22668 IrInstruction *field_name_expr = instruction->field_name_expr->child;
22669 field_name = ir_resolve_str(ira, field_name_expr);
22670 if (!field_name)
22671 return ira->codegen->invalid_instruction;
22672 }
22673
22674 bool result;
22675 if (container_type->id == ZigTypeIdStruct)
22676 result = (bool)find_struct_type_field(container_type, field_name);
22677 else if (container_type->id == ZigTypeIdEnum)
22678 result = (bool)find_enum_type_field(container_type, field_name);
22679 else if (container_type->id == ZigTypeIdUnion)
22680 result = (bool)find_union_type_field(container_type, field_name);
22681 else {
22682 ir_add_error(ira, container_type_value,
22683 buf_sprintf("type '%s' does not support @memberName", buf_ptr(&container_type->name)));
22684 return ira->codegen->invalid_instruction;
22685 }
22686 return ir_build_const_bool(&ira->new_irb,
22687 instruction->base.scope, instruction->base.source_node, result);
22688}
22689
2262322690static IrInstruction *ir_analyze_instruction_breakpoint(IrAnalyze *ira, IrInstructionBreakpoint *instruction) {
2262422691 IrInstruction *result = ir_build_breakpoint(&ira->new_irb,
2262522692 instruction->base.scope, instruction->base.source_node);
......@@ -25480,6 +25547,8 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2548025547 return ir_analyze_instruction_bit_offset_of(ira, (IrInstructionBitOffsetOf *)instruction);
2548125548 case IrInstructionIdTypeInfo:
2548225549 return ir_analyze_instruction_type_info(ira, (IrInstructionTypeInfo *) instruction);
25550 case IrInstructionIdHasField:
25551 return ir_analyze_instruction_has_field(ira, (IrInstructionHasField *) instruction);
2548325552 case IrInstructionIdTypeId:
2548425553 return ir_analyze_instruction_type_id(ira, (IrInstructionTypeId *)instruction);
2548525554 case IrInstructionIdSetEvalBranchQuota:
......@@ -25788,6 +25857,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2578825857 case IrInstructionIdByteOffsetOf:
2578925858 case IrInstructionIdBitOffsetOf:
2579025859 case IrInstructionIdTypeInfo:
25860 case IrInstructionIdHasField:
2579125861 case IrInstructionIdTypeId:
2579225862 case IrInstructionIdAlignCast:
2579325863 case IrInstructionIdImplicitCast:
src/ir_print.cpp+11
......@@ -1270,6 +1270,14 @@ static void ir_print_type_info(IrPrint *irp, IrInstructionTypeInfo *instruction)
12701270 fprintf(irp->f, ")");
12711271}
12721272
1273static void ir_print_has_field(IrPrint *irp, IrInstructionHasField *instruction) {
1274 fprintf(irp->f, "@hasField(");
1275 ir_print_other_instruction(irp, instruction->container_type);
1276 fprintf(irp->f, ",");
1277 ir_print_other_instruction(irp, instruction->field_name_expr);
1278 fprintf(irp->f, ")");
1279}
1280
12731281static void ir_print_type_id(IrPrint *irp, IrInstructionTypeId *instruction) {
12741282 fprintf(irp->f, "@typeId(");
12751283 ir_print_other_instruction(irp, instruction->type_value);
......@@ -1965,6 +1973,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
19651973 case IrInstructionIdTypeInfo:
19661974 ir_print_type_info(irp, (IrInstructionTypeInfo *)instruction);
19671975 break;
1976 case IrInstructionIdHasField:
1977 ir_print_has_field(irp, (IrInstructionHasField *)instruction);
1978 break;
19681979 case IrInstructionIdTypeId:
19691980 ir_print_type_id(irp, (IrInstructionTypeId *)instruction);
19701981 break;
test/stage1/behavior.zig+1
......@@ -98,4 +98,5 @@ comptime {
9898 _ = @import("behavior/void.zig");
9999 _ = @import("behavior/while.zig");
100100 _ = @import("behavior/widening.zig");
101 _ = @import("behavior/hasfield.zig");
101102}
test/stage1/behavior/hasfield.zig created+30
......@@ -0,0 +1,30 @@
1const expect = @import("std").testing.expect;
2const builtin = @import("builtin");
3
4test "@hasField" {
5 const struc = struct {
6 a: i32,
7 b: []u8,
8 };
9 expect(@hasField(struc, "a") == true);
10 expect(@hasField(struc, "b") == true);
11 expect(@hasField(struc, "non-existant") == false);
12
13 const unin = union {
14 a: u64,
15 b: []u16,
16 };
17 expect(@hasField(unin, "a") == true);
18 expect(@hasField(unin, "b") == true);
19 expect(@hasField(unin, "non-existant") == false);
20
21 const enm = enum {
22 a,
23 b,
24 };
25 expect(@hasField(enm, "a") == true);
26 expect(@hasField(enm, "b") == true);
27 expect(@hasField(enm, "non-existant") == false);
28
29 expect(@hasField(builtin, "os") == true);
30}