authorgravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-04-28 11:14:47-06:00
committergravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-05-02 14:39:27-06:00
log1696e943acd67119104f303467c0e26eecb94544
tree0a152f64456fa75fff0776e54732f6beec34d6ee
parentb7914d901c8c5761457a4774858f1004febc2d3a
signaturelock-open Commit is signed but in an unrecognized format.

Implement @typeInfo for @Frame()

Closes https://github.com/ziglang/zig/issues/3066

4 files changed, 42 insertions(+), 4 deletions(-)

src/analyze.cpp+13
......@@ -6021,6 +6021,19 @@ ZigValue *create_const_null(CodeGen *g, ZigType *type) {
60216021 return const_val;
60226022}
60236023
6024void init_const_fn(ZigValue *const_val, ZigFn *fn) {
6025 const_val->special = ConstValSpecialStatic;
6026 const_val->type = fn->type_entry;
6027 const_val->data.x_ptr.special = ConstPtrSpecialFunction;
6028 const_val->data.x_ptr.data.fn.fn_entry = fn;
6029}
6030
6031ZigValue *create_const_fn(CodeGen *g, ZigFn *fn) {
6032 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
6033 init_const_fn(const_val, fn);
6034 return const_val;
6035}
6036
60246037void init_const_float(ZigValue *const_val, ZigType *type, double value) {
60256038 const_val->special = ConstValSpecialStatic;
60266039 const_val->type = type;
src/analyze.hpp+3
......@@ -180,6 +180,9 @@ ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size
180180void init_const_null(ZigValue *const_val, ZigType *type);
181181ZigValue *create_const_null(CodeGen *g, ZigType *type);
182182
183void init_const_fn(ZigValue *const_val, ZigFn *fn);
184ZigValue *create_const_fn(CodeGen *g, ZigFn *fn);
185
183186ZigValue **alloc_const_vals_ptrs(CodeGen *g, size_t count);
184187ZigValue **realloc_const_vals_ptrs(CodeGen *g, ZigValue **ptr, size_t old_count, size_t new_count);
185188
src/ir.cpp+12-3
......@@ -25166,9 +25166,18 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
2516625166 break;
2516725167 }
2516825168 case ZigTypeIdFnFrame:
25169 ir_add_error(ira, source_instr,
25170 buf_sprintf("compiler bug: TODO @typeInfo for async function frames. https://github.com/ziglang/zig/issues/3066"));
25171 return ErrorSemanticAnalyzeFail;
25169 {
25170 result = ira->codegen->pass1_arena->create<ZigValue>();
25171 result->special = ConstValSpecialStatic;
25172 result->type = ir_type_info_get_type(ira, "Frame", nullptr);
25173 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 1);
25174 result->data.x_struct.fields = fields;
25175 ZigFn *fn = type_entry->data.frame.fn;
25176 // function: var
25177 ensure_field_index(result->type, "function", 0);
25178 fields[0] = create_const_fn(ira->codegen, fn);
25179 break;
25180 }
2517225181 }
2517325182
2517425183 assert(result != nullptr);
test/stage1/behavior/type_info.zig+14-1
......@@ -202,7 +202,7 @@ fn testUnion() void {
202202 expect(typeinfo_info.Union.fields[4].enum_field != null);
203203 expect(typeinfo_info.Union.fields[4].enum_field.?.value == 4);
204204 expect(typeinfo_info.Union.fields[4].field_type == @TypeOf(@typeInfo(u8).Int));
205 expect(typeinfo_info.Union.decls.len == 20);
205 expect(typeinfo_info.Union.decls.len == 21);
206206
207207 const TestNoTagUnion = union {
208208 Foo: void,
......@@ -389,3 +389,16 @@ test "defaut value for a var-typed field" {
389389 const S = struct { x: var };
390390 expect(@typeInfo(S).Struct.fields[0].default_value == null);
391391}
392
393fn add(a: i32, b: i32) i32 {
394 return a + b;
395}
396
397test "type info for async frames" {
398 switch (@typeInfo(@Frame(add))) {
399 .Frame => |frame| {
400 expect(frame.function == add);
401 },
402 else => unreachable,
403 }
404}