Commit 6b4f6ebd89

Jimmi Holst Christensen <jimmiholstchristensen@gmail.com>
2018-04-19 20:11:16
Added field builtin function
1 parent 06909ce
src/all_types.hpp
@@ -1292,6 +1292,7 @@ enum BuiltinFnId {
     BuiltinFnIdMemberCount,
     BuiltinFnIdMemberType,
     BuiltinFnIdMemberName,
+    BuiltinFnIdField,
     BuiltinFnIdTypeof,
     BuiltinFnIdAddWithOverflow,
     BuiltinFnIdSubWithOverflow,
@@ -2225,7 +2226,7 @@ struct IrInstructionFieldPtr {
     IrInstruction base;
 
     IrInstruction *container_ptr;
-    Buf *field_name;
+    IrInstruction *field_name_expr;
     bool is_const;
 };
 
src/codegen.cpp
@@ -6114,6 +6114,7 @@ static void define_builtin_fns(CodeGen *g) {
     create_builtin_fn(g, BuiltinFnIdMemberCount, "memberCount", 1);
     create_builtin_fn(g, BuiltinFnIdMemberType, "memberType", 2);
     create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2);
+    create_builtin_fn(g, BuiltinFnIdField, "field", 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);
@@ -7185,4 +7186,3 @@ PackageTableEntry *codegen_create_package(CodeGen *g, const char *root_src_dir,
     }
     return pkg;
 }
-
src/ir.cpp
@@ -1033,18 +1033,26 @@ static IrInstruction *ir_build_elem_ptr_from(IrBuilder *irb, IrInstruction *old_
     return new_instruction;
 }
 
-static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
-    IrInstruction *container_ptr, Buf *field_name)
+static IrInstruction *ir_build_field_ptr_inner(IrBuilder *irb, Scope *scope, AstNode *source_node,
+    IrInstruction *container_ptr, IrInstruction *field_name_expr)
 {
     IrInstructionFieldPtr *instruction = ir_build_instruction<IrInstructionFieldPtr>(irb, scope, source_node);
     instruction->container_ptr = container_ptr;
-    instruction->field_name = field_name;
+    instruction->field_name_expr = field_name_expr;
 
     ir_ref_instruction(container_ptr, irb->current_basic_block);
+    ir_ref_instruction(field_name_expr, irb->current_basic_block);
 
     return &instruction->base;
 }
 
+static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
+    IrInstruction *container_ptr, Buf *field_name)
+{
+    IrInstruction *field_name_expr = ir_build_const_str_lit(irb, scope, source_node, field_name);
+    return ir_build_field_ptr_inner(irb, scope, source_node, container_ptr, field_name_expr);
+}
+
 static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
     IrInstruction *struct_ptr, TypeStructField *field)
 {
@@ -4015,6 +4023,24 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
 
                 return ir_build_member_name(irb, scope, node, arg0_value, arg1_value);
             }
+        case BuiltinFnIdField:
+            {
+                AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
+                IrInstruction *arg0_value = ir_gen_node_extra(irb, arg0_node, scope, LVAL_PTR);
+                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 *ptr_instruction = ir_build_field_ptr_inner(irb, scope, node, arg0_value, arg1_value);
+                //if (lval.is_ptr)
+                //    return ptr_instruction;
+
+                return ir_build_load_ptr(irb, scope, node, ptr_instruction);
+            }
         case BuiltinFnIdBreakpoint:
             return ir_build_breakpoint(irb, scope, node);
         case BuiltinFnIdReturnAddress:
@@ -13458,7 +13484,11 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
         zig_unreachable();
     }
 
-    Buf *field_name = field_ptr_instruction->field_name;
+    IrInstruction *field_name_expr = field_ptr_instruction->field_name_expr->other;
+    Buf *field_name = ir_resolve_str(ira, field_name_expr);
+    if (!field_name)
+        return ira->codegen->builtin_types.entry_invalid;
+
     AstNode *source_node = field_ptr_instruction->base.source_node;
 
     if (type_is_invalid(container_type)) {
src/ir_print.cpp
@@ -360,7 +360,8 @@ static void ir_print_ptr_type_child(IrPrint *irp, IrInstructionPtrTypeChild *ins
 static void ir_print_field_ptr(IrPrint *irp, IrInstructionFieldPtr *instruction) {
     fprintf(irp->f, "fieldptr ");
     ir_print_other_instruction(irp, instruction->container_ptr);
-    fprintf(irp->f, ".%s", buf_ptr(instruction->field_name));
+    fprintf(irp->f, ".");
+    ir_print_other_instruction(irp, instruction->field_name_expr);
 }
 
 static void ir_print_struct_field_ptr(IrPrint *irp, IrInstructionStructFieldPtr *instruction) {
test/cases/reflection.zig
@@ -1,5 +1,6 @@
 const assert = @import("std").debug.assert;
 const mem = @import("std").mem;
+const reflection = this;
 
 test "reflection: array, pointer, nullable, error union type child" {
     comptime {
@@ -56,7 +57,30 @@ test "reflection: enum member types and names" {
 
 }
 
+test "reflection: @field" {
+    const f = Foo {
+        .one = 42,
+        .two = true,
+        .three = void{},
+    };
+
+    assert(f.one == f.one);
+    assert(@field(f, "o" ++ "ne") == f.one);
+    assert(@field(f, "t" ++ "wo") == f.two);
+    assert(@field(f, "th" ++ "ree") == f.three);
+    assert(@field(Foo, "const" ++ "ant") == Foo.constant);
+    assert(@field(Bar, "O" ++ "ne") == Bar.One);
+    assert(@field(Bar, "O" ++ "ne") == Bar.One);
+    assert(@field(Bar, "O" ++ "ne") == Bar.One);
+    assert(@field(Bar, "T" ++ "wo") == Bar.Two);
+    assert(@field(Bar, "Th" ++ "ree") == Bar.Three);
+    assert(@field(Bar, "F" ++ "our") == Bar.Four);
+    assert(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2));
+}
+
 const Foo = struct {
+    const constant = 52;
+
     one: i32,
     two: bool,
     three: void,