Merge branch 'shawnl-has-field'

This commit is contained in:
Andrew Kelley 2019-07-02 16:53:08 -04:00
commit 57d6724186
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
8 changed files with 145 additions and 0 deletions

View File

@ -6801,6 +6801,19 @@ test "@hasDecl" {
assert(!@hasDecl(Foo, "nope1234"));
}
{#code_end#}
{#see_also|@hasField#}
{#header_close#}
{#header_open|@hasField#}
<pre>{#syntax#}@hasField(comptime T: type, comptime name: []const u8) bool{#endsyntax#}</pre>
<p>Returns whether the field name of a struct, union, or enum exists.</p>
<p>
The result is a compile time constant.
</p>
<p>
It does not include functions, variables, or constants.
</p>
{#see_also|@hasDecl#}
{#header_close#}
{#header_open|@import#}

View File

@ -1416,6 +1416,7 @@ enum BuiltinFnId {
BuiltinFnIdMemberName,
BuiltinFnIdField,
BuiltinFnIdTypeInfo,
BuiltinFnIdHasField,
BuiltinFnIdTypeof,
BuiltinFnIdAddWithOverflow,
BuiltinFnIdSubWithOverflow,
@ -2307,6 +2308,7 @@ enum IrInstructionId {
IrInstructionIdByteOffsetOf,
IrInstructionIdBitOffsetOf,
IrInstructionIdTypeInfo,
IrInstructionIdHasField,
IrInstructionIdTypeId,
IrInstructionIdSetEvalBranchQuota,
IrInstructionIdPtrType,
@ -3333,6 +3335,13 @@ struct IrInstructionTypeInfo {
IrInstruction *type_value;
};
struct IrInstructionHasField {
IrInstruction base;
IrInstruction *container_type;
IrInstruction *field_name;
};
struct IrInstructionTypeId {
IrInstruction base;

View File

@ -5592,6 +5592,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdByteOffsetOf:
case IrInstructionIdBitOffsetOf:
case IrInstructionIdTypeInfo:
case IrInstructionIdHasField:
case IrInstructionIdTypeId:
case IrInstructionIdSetEvalBranchQuota:
case IrInstructionIdPtrType:
@ -7328,6 +7329,7 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2);
create_builtin_fn(g, BuiltinFnIdField, "field", 2);
create_builtin_fn(g, BuiltinFnIdTypeInfo, "typeInfo", 1);
create_builtin_fn(g, BuiltinFnIdHasField, "hasField", 2);
create_builtin_fn(g, BuiltinFnIdTypeof, "typeOf", 1); // TODO rename to TypeOf
create_builtin_fn(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4);
create_builtin_fn(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4);

View File

@ -897,6 +897,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;
}
@ -1375,6 +1379,19 @@ 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)
{
IrInstructionHasField *instruction = ir_build_instruction<IrInstructionHasField>(irb, scope, source_node);
instruction->container_type = container_type;
instruction->field_name = field_name;
ir_ref_instruction(container_type, irb->current_basic_block);
ir_ref_instruction(field_name, 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)
{
@ -5101,6 +5118,21 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
IrInstruction *load_ptr = ir_build_load_ptr(irb, scope, node, ptr_instruction);
return ir_expr_wrap(irb, scope, load_ptr, result_loc);
}
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, result_loc);
}
case BuiltinFnIdTypeInfo:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
@ -22620,6 +22652,34 @@ static IrInstruction *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInstr
}
}
static IrInstruction *ir_analyze_instruction_has_field(IrAnalyze *ira, IrInstructionHasField *instruction) {
Error err;
ZigType *container_type = ir_resolve_type(ira, instruction->container_type->child);
if (type_is_invalid(container_type))
return ira->codegen->invalid_instruction;
if ((err = type_resolve(ira->codegen, container_type, ResolveStatusZeroBitsKnown)))
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 = 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_const_bool(ira, &instruction->base, 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);
@ -25480,6 +25540,8 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
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:
@ -25788,6 +25850,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdByteOffsetOf:
case IrInstructionIdBitOffsetOf:
case IrInstructionIdTypeInfo:
case IrInstructionIdHasField:
case IrInstructionIdTypeId:
case IrInstructionIdAlignCast:
case IrInstructionIdImplicitCast:

View File

@ -1270,6 +1270,14 @@ static void ir_print_type_info(IrPrint *irp, IrInstructionTypeInfo *instruction)
fprintf(irp->f, ")");
}
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);
fprintf(irp->f, ")");
}
static void ir_print_type_id(IrPrint *irp, IrInstructionTypeId *instruction) {
fprintf(irp->f, "@typeId(");
ir_print_other_instruction(irp, instruction->type_value);
@ -1965,6 +1973,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
case IrInstructionIdTypeInfo:
ir_print_type_info(irp, (IrInstructionTypeInfo *)instruction);
break;
case IrInstructionIdHasField:
ir_print_has_field(irp, (IrInstructionHasField *)instruction);
break;
case IrInstructionIdTypeId:
ir_print_type_id(irp, (IrInstructionTypeId *)instruction);
break;

View File

@ -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 {

View File

@ -98,4 +98,5 @@ comptime {
_ = @import("behavior/void.zig");
_ = @import("behavior/while.zig");
_ = @import("behavior/widening.zig");
_ = @import("behavior/hasfield.zig");
}

View File

@ -0,0 +1,37 @@
const expect = @import("std").testing.expect;
const builtin = @import("builtin");
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(enm, "nope") == false);
}