authorgravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-04-25 23:39:28-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-27 14:37:18-04:00
log45f4a1124f80219d9237d6809ecb919a8896723a
treeb9e88d0dc07274aba05b2c4ef752bceae1062e55
parent0df82889cf01757f2c19f841a5e3446a5ba70eac

implement @Type() for more types


2 files changed, 85 insertions(+), 5 deletions(-)

src/ir.cpp+46-5
...@@ -25267,6 +25267,19 @@ static ZigType *get_const_field_meta_type(IrAnalyze *ira, AstNode *source_node,...@@ -25267,6 +25267,19 @@ static ZigType *get_const_field_meta_type(IrAnalyze *ira, AstNode *source_node,
25267 return value->data.x_type;25267 return value->data.x_type;
25268}25268}
2526925269
25270static ZigType *get_const_field_meta_type_optional(IrAnalyze *ira, AstNode *source_node,
25271 ZigValue *struct_value, const char *name, size_t field_index)
25272{
25273 ZigValue *value = get_const_field(ira, source_node, struct_value, name, field_index);
25274 if (value == nullptr)
25275 return ira->codegen->invalid_inst_gen->value->type;
25276 assert(value->type->id == ZigTypeIdOptional);
25277 assert(value->type->data.maybe.child_type == ira->codegen->builtin_types.entry_type);
25278 if (value->data.x_optional == nullptr)
25279 return nullptr;
25280 return value->data.x_optional->data.x_type;
25281}
25282
25270static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeId tagTypeId, ZigValue *payload) {25283static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeId tagTypeId, ZigValue *payload) {
25271 Error err;25284 Error err;
25272 switch (tagTypeId) {25285 switch (tagTypeId) {
...@@ -25388,14 +25401,42 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI...@@ -25388,14 +25401,42 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
25388 return ira->codegen->builtin_types.entry_undef;25401 return ira->codegen->builtin_types.entry_undef;
25389 case ZigTypeIdNull:25402 case ZigTypeIdNull:
25390 return ira->codegen->builtin_types.entry_null;25403 return ira->codegen->builtin_types.entry_null;
25391 case ZigTypeIdOptional:25404 case ZigTypeIdOptional: {
25392 case ZigTypeIdErrorUnion:25405 assert(payload->special == ConstValSpecialStatic);
25406 assert(payload->type == ir_type_info_get_type(ira, "Optional", nullptr));
25407 ZigType *child_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "child", 0);
25408 return get_optional_type(ira->codegen, child_type);
25409 }
25410 case ZigTypeIdErrorUnion: {
25411 assert(payload->special == ConstValSpecialStatic);
25412 assert(payload->type == ir_type_info_get_type(ira, "ErrorUnion", nullptr));
25413 ZigType *err_set_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "error_set", 0);
25414 ZigType *payload_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "payload", 1);
25415 return get_error_union_type(ira->codegen, err_set_type, payload_type);
25416 }
25417 case ZigTypeIdOpaque: {
25418 Buf *bare_name = buf_alloc();
25419 Buf *full_name = get_anon_type_name(ira->codegen,
25420 ira->old_irb.exec, "opaque", source_instr->scope, source_instr->source_node, bare_name);
25421 return get_opaque_type(ira->codegen,
25422 source_instr->scope, source_instr->source_node, buf_ptr(full_name), bare_name);
25423 }
25424 case ZigTypeIdVector: {
25425 assert(payload->special == ConstValSpecialStatic);
25426 assert(payload->type == ir_type_info_get_type(ira, "Vector", nullptr));
25427 BigInt *len = get_const_field_lit_int(ira, source_instr->source_node, payload, "len", 0);
25428 ZigType *child_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "child", 1);
25429 return get_vector_type(ira->codegen, bigint_as_u32(len), child_type);
25430 }
25431 case ZigTypeIdAnyFrame: {
25432 assert(payload->special == ConstValSpecialStatic);
25433 assert(payload->type == ir_type_info_get_type(ira, "AnyFrame", nullptr));
25434 ZigType *child_type = get_const_field_meta_type_optional(ira, source_instr->source_node, payload, "child", 0);
25435 return get_any_frame_type(ira->codegen, child_type);
25436 }
25393 case ZigTypeIdErrorSet:25437 case ZigTypeIdErrorSet:
25394 case ZigTypeIdEnum:25438 case ZigTypeIdEnum:
25395 case ZigTypeIdOpaque:
25396 case ZigTypeIdFnFrame:25439 case ZigTypeIdFnFrame:
25397 case ZigTypeIdAnyFrame:
25398 case ZigTypeIdVector:
25399 case ZigTypeIdEnumLiteral:25440 case ZigTypeIdEnumLiteral:
25400 ir_add_error(ira, source_instr, buf_sprintf(25441 ir_add_error(ira, source_instr, buf_sprintf(
25401 "TODO implement @Type for 'TypeInfo.%s': see https://github.com/ziglang/zig/issues/2907", type_id_name(tagTypeId)));25442 "TODO implement @Type for 'TypeInfo.%s': see https://github.com/ziglang/zig/issues/2907", type_id_name(tagTypeId)));
test/stage1/behavior/type.zig+39
...@@ -171,3 +171,42 @@ test "@Type picks up the sentinel value from TypeInfo" {...@@ -171,3 +171,42 @@ test "@Type picks up the sentinel value from TypeInfo" {
171 [:4]allowzero align(4) volatile u8, [:4]allowzero align(4) const volatile u8,171 [:4]allowzero align(4) volatile u8, [:4]allowzero align(4) const volatile u8,
172 });172 });
173}173}
174
175test "Type.Optional" {
176 testTypes(&[_]type{
177 ?u8,
178 ?*u8,
179 ?[]u8,
180 ?[*]u8,
181 ?[*c]u8,
182 });
183}
184
185test "Type.ErrorUnion" {
186 testTypes(&[_]type{
187 error{}!void,
188 error{Error}!void,
189 });
190}
191
192test "Type.Opaque" {
193 testing.expect(@OpaqueType() != @Type(.Opaque));
194 testing.expect(@Type(.Opaque) != @Type(.Opaque));
195 testing.expect(@typeInfo(@Type(.Opaque)) == .Opaque);
196}
197
198test "Type.Vector" {
199 testTypes(&[_]type{
200 @Vector(0, u8),
201 @Vector(4, u8),
202 @Vector(8, *u8),
203 });
204}
205
206test "Type.AnyFrame" {
207 testTypes(&[_]type{
208 anyframe,
209 anyframe->u8,
210 anyframe->anyframe->u8,
211 });
212}