authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-24 00:44:18-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-24 00:44:18-04:00
logd0551db5cd29e4c7f361ef40a37486138a4e8b1e
tree9b591aa521477ec0bb3bd0ef046e50d140e51d67
parent64dddd7afe14a683826b03bc36ab80fa93a84e2c
signaturelock-open Commit is signed but in an unrecognized format.

introduce the enum literal type

see #683

16 files changed, 195 insertions(+), 3 deletions(-)

src-self-hosted/ir.zig+1
......@@ -1186,6 +1186,7 @@ pub const Builder = struct {
11861186 ast.Node.Id.AsyncAttribute => return error.Unimplemented,
11871187 ast.Node.Id.ParamDecl => return error.Unimplemented,
11881188 ast.Node.Id.FieldInitializer => return error.Unimplemented,
1189 ast.Node.Id.EnumLiteral => return error.Unimplemented,
11891190 }
11901191 }
11911192
src-self-hosted/type.zig+18
......@@ -32,6 +32,7 @@ pub const Type = struct {
3232 Id.Array => @fieldParentPtr(Array, "base", base).destroy(comp),
3333 Id.ComptimeFloat => @fieldParentPtr(ComptimeFloat, "base", base).destroy(comp),
3434 Id.ComptimeInt => @fieldParentPtr(ComptimeInt, "base", base).destroy(comp),
35 Id.EnumLiteral => @fieldParentPtr(EnumLiteral, "base", base).destroy(comp),
3536 Id.Undefined => @fieldParentPtr(Undefined, "base", base).destroy(comp),
3637 Id.Null => @fieldParentPtr(Null, "base", base).destroy(comp),
3738 Id.Optional => @fieldParentPtr(Optional, "base", base).destroy(comp),
......@@ -65,6 +66,7 @@ pub const Type = struct {
6566 Id.Array => return @fieldParentPtr(Array, "base", base).getLlvmType(allocator, llvm_context),
6667 Id.ComptimeFloat => unreachable,
6768 Id.ComptimeInt => unreachable,
69 Id.EnumLiteral => unreachable,
6870 Id.Undefined => unreachable,
6971 Id.Null => unreachable,
7072 Id.Optional => return @fieldParentPtr(Optional, "base", base).getLlvmType(allocator, llvm_context),
......@@ -85,6 +87,7 @@ pub const Type = struct {
8587 Id.Type,
8688 Id.ComptimeFloat,
8789 Id.ComptimeInt,
90 Id.EnumLiteral,
8891 Id.Undefined,
8992 Id.Null,
9093 Id.BoundFn,
......@@ -118,6 +121,7 @@ pub const Type = struct {
118121 Id.Type,
119122 Id.ComptimeFloat,
120123 Id.ComptimeInt,
124 Id.EnumLiteral,
121125 Id.Undefined,
122126 Id.Null,
123127 Id.BoundFn,
......@@ -940,6 +944,20 @@ pub const Type = struct {
940944 }
941945 };
942946
947 pub const EnumLiteral = struct {
948 base: Type,
949
950 /// Adds 1 reference to the resulting type
951 pub fn get(comp: *Compilation) *EnumLiteral {
952 comp.comptime_int_type.base.base.ref();
953 return comp.comptime_int_type;
954 }
955
956 pub fn destroy(self: *EnumLiteral, comp: *Compilation) void {
957 comp.gpa().destroy(self);
958 }
959 };
960
943961 pub const Undefined = struct {
944962 base: Type,
945963
src/all_types.hpp+10
......@@ -317,6 +317,7 @@ struct ConstExprValue {
317317 ConstArrayValue x_array;
318318 ConstPtrValue x_ptr;
319319 ConstArgTuple x_arg_tuple;
320 Buf *x_enum_literal;
320321
321322 // populated if special == ConstValSpecialRuntime
322323 RuntimeHintErrorUnion rh_error_union;
......@@ -468,6 +469,7 @@ enum NodeType {
468469 NodeTypeAwaitExpr,
469470 NodeTypeSuspend,
470471 NodeTypePromiseType,
472 NodeTypeEnumLiteral,
471473};
472474
473475enum CallingConvention {
......@@ -929,6 +931,11 @@ struct AstNodePromiseType {
929931 AstNode *payload_type; // can be NULL
930932};
931933
934struct AstNodeEnumLiteral {
935 Token *period;
936 Token *identifier;
937};
938
932939struct AstNode {
933940 enum NodeType type;
934941 size_t line;
......@@ -989,6 +996,7 @@ struct AstNode {
989996 AstNodeAwaitExpr await_expr;
990997 AstNodeSuspend suspend;
991998 AstNodePromiseType promise_type;
999 AstNodeEnumLiteral enum_literal;
9921000 } data;
9931001};
9941002
......@@ -1252,6 +1260,7 @@ enum ZigTypeId {
12521260 ZigTypeIdOpaque,
12531261 ZigTypeIdPromise,
12541262 ZigTypeIdVector,
1263 ZigTypeIdEnumLiteral,
12551264};
12561265
12571266enum OnePossibleValue {
......@@ -1741,6 +1750,7 @@ struct CodeGen {
17411750 ZigType *entry_global_error_set;
17421751 ZigType *entry_arg_tuple;
17431752 ZigType *entry_promise;
1753 ZigType *entry_enum_literal;
17441754 } builtin_types;
17451755 ZigType *align_amt_type;
17461756 ZigType *stack_trace_type;
src/analyze.cpp+31-1
......@@ -242,6 +242,7 @@ AstNode *type_decl_node(ZigType *type_entry) {
242242 case ZigTypeIdArray:
243243 case ZigTypeIdComptimeFloat:
244244 case ZigTypeIdComptimeInt:
245 case ZigTypeIdEnumLiteral:
245246 case ZigTypeIdUndefined:
246247 case ZigTypeIdNull:
247248 case ZigTypeIdOptional:
......@@ -303,6 +304,7 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
303304 case ZigTypeIdArray:
304305 case ZigTypeIdComptimeFloat:
305306 case ZigTypeIdComptimeInt:
307 case ZigTypeIdEnumLiteral:
306308 case ZigTypeIdUndefined:
307309 case ZigTypeIdNull:
308310 case ZigTypeIdOptional:
......@@ -1463,6 +1465,7 @@ static Error emit_error_unless_type_allowed_in_packed_struct(CodeGen *g, ZigType
14631465 case ZigTypeIdUnreachable:
14641466 case ZigTypeIdComptimeFloat:
14651467 case ZigTypeIdComptimeInt:
1468 case ZigTypeIdEnumLiteral:
14661469 case ZigTypeIdUndefined:
14671470 case ZigTypeIdNull:
14681471 case ZigTypeIdErrorUnion:
......@@ -1550,6 +1553,7 @@ bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry) {
15501553 case ZigTypeIdMetaType:
15511554 case ZigTypeIdComptimeFloat:
15521555 case ZigTypeIdComptimeInt:
1556 case ZigTypeIdEnumLiteral:
15531557 case ZigTypeIdUndefined:
15541558 case ZigTypeIdNull:
15551559 case ZigTypeIdErrorUnion:
......@@ -1712,6 +1716,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
17121716 return g->builtin_types.entry_invalid;
17131717 case ZigTypeIdComptimeFloat:
17141718 case ZigTypeIdComptimeInt:
1719 case ZigTypeIdEnumLiteral:
17151720 case ZigTypeIdBoundFn:
17161721 case ZigTypeIdMetaType:
17171722 case ZigTypeIdVoid:
......@@ -1806,6 +1811,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
18061811
18071812 case ZigTypeIdComptimeFloat:
18081813 case ZigTypeIdComptimeInt:
1814 case ZigTypeIdEnumLiteral:
18091815 case ZigTypeIdBoundFn:
18101816 case ZigTypeIdMetaType:
18111817 case ZigTypeIdUnreachable:
......@@ -3621,6 +3627,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
36213627 case NodeTypeAwaitExpr:
36223628 case NodeTypeSuspend:
36233629 case NodeTypePromiseType:
3630 case NodeTypeEnumLiteral:
36243631 zig_unreachable();
36253632 }
36263633}
......@@ -3658,6 +3665,7 @@ ZigType *validate_var_type(CodeGen *g, AstNode *source_node, ZigType *type_entry
36583665 return g->builtin_types.entry_invalid;
36593666 case ZigTypeIdComptimeFloat:
36603667 case ZigTypeIdComptimeInt:
3668 case ZigTypeIdEnumLiteral:
36613669 case ZigTypeIdMetaType:
36623670 case ZigTypeIdVoid:
36633671 case ZigTypeIdBool:
......@@ -3807,7 +3815,8 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
38073815 implicit_type = g->builtin_types.entry_invalid;
38083816 } else if ((!is_const || linkage == VarLinkageExternal) &&
38093817 (implicit_type->id == ZigTypeIdComptimeFloat ||
3810 implicit_type->id == ZigTypeIdComptimeInt))
3818 implicit_type->id == ZigTypeIdComptimeInt ||
3819 implicit_type->id == ZigTypeIdEnumLiteral))
38113820 {
38123821 add_node_error(g, source_node, buf_sprintf("unable to infer variable type"));
38133822 implicit_type = g->builtin_types.entry_invalid;
......@@ -4051,6 +4060,7 @@ static bool is_container(ZigType *type_entry) {
40514060 case ZigTypeIdArray:
40524061 case ZigTypeIdComptimeFloat:
40534062 case ZigTypeIdComptimeInt:
4063 case ZigTypeIdEnumLiteral:
40544064 case ZigTypeIdUndefined:
40554065 case ZigTypeIdNull:
40564066 case ZigTypeIdOptional:
......@@ -4109,6 +4119,7 @@ void resolve_container_type(CodeGen *g, ZigType *type_entry) {
41094119 case ZigTypeIdArray:
41104120 case ZigTypeIdComptimeFloat:
41114121 case ZigTypeIdComptimeInt:
4122 case ZigTypeIdEnumLiteral:
41124123 case ZigTypeIdUndefined:
41134124 case ZigTypeIdNull:
41144125 case ZigTypeIdOptional:
......@@ -4647,6 +4658,7 @@ bool handle_is_ptr(ZigType *type_entry) {
46474658 case ZigTypeIdMetaType:
46484659 case ZigTypeIdComptimeFloat:
46494660 case ZigTypeIdComptimeInt:
4661 case ZigTypeIdEnumLiteral:
46504662 case ZigTypeIdUndefined:
46514663 case ZigTypeIdNull:
46524664 case ZigTypeIdBoundFn:
......@@ -4827,6 +4839,8 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
48274839 }
48284840 return result;
48294841 }
4842 case ZigTypeIdEnumLiteral:
4843 return buf_hash(const_val->data.x_enum_literal) * 2691276464;
48304844 case ZigTypeIdEnum:
48314845 {
48324846 uint32_t result = 31643936;
......@@ -4974,6 +4988,7 @@ static bool can_mutate_comptime_var_state(ConstExprValue *value) {
49744988 case ZigTypeIdFloat:
49754989 case ZigTypeIdComptimeFloat:
49764990 case ZigTypeIdComptimeInt:
4991 case ZigTypeIdEnumLiteral:
49774992 case ZigTypeIdUndefined:
49784993 case ZigTypeIdNull:
49794994 case ZigTypeIdBoundFn:
......@@ -5043,6 +5058,7 @@ static bool return_type_is_cacheable(ZigType *return_type) {
50435058 case ZigTypeIdFloat:
50445059 case ZigTypeIdComptimeFloat:
50455060 case ZigTypeIdComptimeInt:
5061 case ZigTypeIdEnumLiteral:
50465062 case ZigTypeIdUndefined:
50475063 case ZigTypeIdNull:
50485064 case ZigTypeIdBoundFn:
......@@ -5173,6 +5189,7 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
51735189 case ZigTypeIdOpaque:
51745190 case ZigTypeIdComptimeFloat:
51755191 case ZigTypeIdComptimeInt:
5192 case ZigTypeIdEnumLiteral:
51765193 case ZigTypeIdMetaType:
51775194 case ZigTypeIdBoundFn:
51785195 case ZigTypeIdArgTuple:
......@@ -5236,6 +5253,7 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) {
52365253 zig_unreachable();
52375254 case ZigTypeIdComptimeFloat:
52385255 case ZigTypeIdComptimeInt:
5256 case ZigTypeIdEnumLiteral:
52395257 case ZigTypeIdUndefined:
52405258 case ZigTypeIdNull:
52415259 case ZigTypeIdMetaType:
......@@ -5794,6 +5812,8 @@ bool const_values_equal(CodeGen *g, ConstExprValue *a, ConstExprValue *b) {
57945812 case ZigTypeIdInt:
57955813 case ZigTypeIdComptimeInt:
57965814 return bigint_cmp(&a->data.x_bigint, &b->data.x_bigint) == CmpEQ;
5815 case ZigTypeIdEnumLiteral:
5816 return buf_eql_buf(a->data.x_enum_literal, b->data.x_enum_literal);
57975817 case ZigTypeIdPointer:
57985818 case ZigTypeIdFn:
57995819 return const_values_equal_ptr(a, b);
......@@ -6044,6 +6064,9 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
60446064 case ZigTypeIdInt:
60456065 bigint_append_buf(buf, &const_val->data.x_bigint, 10);
60466066 return;
6067 case ZigTypeIdEnumLiteral:
6068 buf_append_buf(buf, const_val->data.x_enum_literal);
6069 return;
60476070 case ZigTypeIdMetaType:
60486071 buf_appendf(buf, "%s", buf_ptr(&const_val->data.x_type->name));
60496072 return;
......@@ -6213,6 +6236,7 @@ uint32_t type_id_hash(TypeId x) {
62136236 case ZigTypeIdStruct:
62146237 case ZigTypeIdComptimeFloat:
62156238 case ZigTypeIdComptimeInt:
6239 case ZigTypeIdEnumLiteral:
62166240 case ZigTypeIdUndefined:
62176241 case ZigTypeIdNull:
62186242 case ZigTypeIdOptional:
......@@ -6260,6 +6284,7 @@ bool type_id_eql(TypeId a, TypeId b) {
62606284 case ZigTypeIdStruct:
62616285 case ZigTypeIdComptimeFloat:
62626286 case ZigTypeIdComptimeInt:
6287 case ZigTypeIdEnumLiteral:
62636288 case ZigTypeIdUndefined:
62646289 case ZigTypeIdNull:
62656290 case ZigTypeIdOptional:
......@@ -6439,6 +6464,7 @@ static const ZigTypeId all_type_ids[] = {
64396464 ZigTypeIdOpaque,
64406465 ZigTypeIdPromise,
64416466 ZigTypeIdVector,
6467 ZigTypeIdEnumLiteral,
64426468};
64436469
64446470ZigTypeId type_id_at_index(size_t index) {
......@@ -6504,6 +6530,8 @@ size_t type_id_index(ZigType *entry) {
65046530 return 22;
65056531 case ZigTypeIdVector:
65066532 return 23;
6533 case ZigTypeIdEnumLiteral:
6534 return 24;
65076535 }
65086536 zig_unreachable();
65096537}
......@@ -6534,6 +6562,8 @@ const char *type_id_name(ZigTypeId id) {
65346562 return "ComptimeFloat";
65356563 case ZigTypeIdComptimeInt:
65366564 return "ComptimeInt";
6565 case ZigTypeIdEnumLiteral:
6566 return "EnumLiteral";
65376567 case ZigTypeIdUndefined:
65386568 return "Undefined";
65396569 case ZigTypeIdNull:
src/ast_render.cpp+7
......@@ -259,6 +259,8 @@ static const char *node_type_str(NodeType node_type) {
259259 return "PromiseType";
260260 case NodeTypePointerType:
261261 return "PointerType";
262 case NodeTypeEnumLiteral:
263 return "EnumLiteral";
262264 }
263265 zig_unreachable();
264266}
......@@ -1154,6 +1156,11 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
11541156 }
11551157 break;
11561158 }
1159 case NodeTypeEnumLiteral:
1160 {
1161 fprintf(ar->f, ".%s", buf_ptr(&node->data.enum_literal.identifier->data.str_lit.str));
1162 break;
1163 }
11571164 case NodeTypeParamDecl:
11581165 case NodeTypeTestDecl:
11591166 case NodeTypeStructField:
src/codegen.cpp+12-1
......@@ -5818,6 +5818,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
58185818 case ZigTypeIdUnreachable:
58195819 case ZigTypeIdComptimeFloat:
58205820 case ZigTypeIdComptimeInt:
5821 case ZigTypeIdEnumLiteral:
58215822 case ZigTypeIdUndefined:
58225823 case ZigTypeIdNull:
58235824 case ZigTypeIdErrorUnion:
......@@ -6419,6 +6420,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
64196420 case ZigTypeIdUnreachable:
64206421 case ZigTypeIdComptimeFloat:
64216422 case ZigTypeIdComptimeInt:
6423 case ZigTypeIdEnumLiteral:
64226424 case ZigTypeIdUndefined:
64236425 case ZigTypeIdNull:
64246426 case ZigTypeIdBoundFn:
......@@ -7005,6 +7007,12 @@ static void define_builtin_types(CodeGen *g) {
70057007 g->builtin_types.entry_num_lit_int = entry;
70067008 g->primitive_type_table.put(&entry->name, entry);
70077009 }
7010 {
7011 ZigType *entry = new_type_table_entry(ZigTypeIdEnumLiteral);
7012 buf_init_from_str(&entry->name, "(enum literal)");
7013 entry->zero_bits = true;
7014 g->builtin_types.entry_enum_literal = entry;
7015 }
70087016 {
70097017 ZigType *entry = new_type_table_entry(ZigTypeIdUndefined);
70107018 buf_init_from_str(&entry->name, "(undefined)");
......@@ -7175,7 +7183,6 @@ static void define_builtin_types(CodeGen *g) {
71757183 ZigType *entry = get_promise_type(g, nullptr);
71767184 g->primitive_type_table.put(&entry->name, entry);
71777185 }
7178
71797186}
71807187
71817188
......@@ -7527,6 +7534,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
75277534 " Opaque: void,\n"
75287535 " Promise: Promise,\n"
75297536 " Vector: Vector,\n"
7537 " EnumLiteral: void,\n"
75307538 "\n\n"
75317539 " pub const Int = struct {\n"
75327540 " is_signed: bool,\n"
......@@ -8626,6 +8634,7 @@ static void prepend_c_type_to_decl_list(CodeGen *g, GenH *gen_h, ZigType *type_e
86268634 case ZigTypeIdMetaType:
86278635 case ZigTypeIdComptimeFloat:
86288636 case ZigTypeIdComptimeInt:
8637 case ZigTypeIdEnumLiteral:
86298638 case ZigTypeIdUndefined:
86308639 case ZigTypeIdNull:
86318640 case ZigTypeIdBoundFn:
......@@ -8812,6 +8821,7 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu
88128821 case ZigTypeIdBoundFn:
88138822 case ZigTypeIdComptimeFloat:
88148823 case ZigTypeIdComptimeInt:
8824 case ZigTypeIdEnumLiteral:
88158825 case ZigTypeIdUndefined:
88168826 case ZigTypeIdNull:
88178827 case ZigTypeIdArgTuple:
......@@ -8965,6 +8975,7 @@ static void gen_h_file(CodeGen *g) {
89658975 case ZigTypeIdPointer:
89668976 case ZigTypeIdComptimeFloat:
89678977 case ZigTypeIdComptimeInt:
8978 case ZigTypeIdEnumLiteral:
89688979 case ZigTypeIdArray:
89698980 case ZigTypeIdUndefined:
89708981 case ZigTypeIdNull:
src/ir.cpp+30
......@@ -257,6 +257,7 @@ static bool types_have_same_zig_comptime_repr(ZigType *a, ZigType *b) {
257257 case ZigTypeIdBool:
258258 case ZigTypeIdComptimeFloat:
259259 case ZigTypeIdComptimeInt:
260 case ZigTypeIdEnumLiteral:
260261 case ZigTypeIdPointer:
261262 case ZigTypeIdUndefined:
262263 case ZigTypeIdNull:
......@@ -1144,6 +1145,14 @@ static IrInstruction *ir_build_const_bool(IrBuilder *irb, Scope *scope, AstNode
11441145 return &const_instruction->base;
11451146}
11461147
1148static IrInstruction *ir_build_const_enum_literal(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *name) {
1149 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
1150 const_instruction->base.value.type = irb->codegen->builtin_types.entry_enum_literal;
1151 const_instruction->base.value.special = ConstValSpecialStatic;
1152 const_instruction->base.value.data.x_enum_literal = name;
1153 return &const_instruction->base;
1154}
1155
11471156static IrInstruction *ir_build_const_bound_fn(IrBuilder *irb, Scope *scope, AstNode *source_node,
11481157 ZigFn *fn_entry, IrInstruction *first_arg)
11491158{
......@@ -5794,6 +5803,12 @@ static IrInstruction *ir_gen_bool_literal(IrBuilder *irb, Scope *scope, AstNode
57945803 return ir_build_const_bool(irb, scope, node, node->data.bool_literal.value);
57955804}
57965805
5806static IrInstruction *ir_gen_enum_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
5807 assert(node->type == NodeTypeEnumLiteral);
5808 Buf *name = &node->data.enum_literal.identifier->data.str_lit.str;
5809 return ir_build_const_enum_literal(irb, scope, node, name);
5810}
5811
57975812static IrInstruction *ir_gen_string_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
57985813 assert(node->type == NodeTypeStringLiteral);
57995814
......@@ -7564,6 +7579,8 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
75647579 return ir_lval_wrap(irb, scope, ir_gen_await_expr(irb, scope, node), lval);
75657580 case NodeTypeSuspend:
75667581 return ir_lval_wrap(irb, scope, ir_gen_suspend(irb, scope, node), lval);
7582 case NodeTypeEnumLiteral:
7583 return ir_lval_wrap(irb, scope, ir_gen_enum_literal(irb, scope, node), lval);
75677584 }
75687585 zig_unreachable();
75697586}
......@@ -12264,6 +12281,7 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *
1226412281 case ZigTypeIdArgTuple:
1226512282 case ZigTypeIdPromise:
1226612283 case ZigTypeIdEnum:
12284 case ZigTypeIdEnumLiteral:
1226712285 operator_allowed = is_equality_cmp;
1226812286 break;
1226912287
......@@ -13596,6 +13614,7 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
1359613614 case ZigTypeIdUnreachable:
1359713615 case ZigTypeIdComptimeFloat:
1359813616 case ZigTypeIdComptimeInt:
13617 case ZigTypeIdEnumLiteral:
1359913618 case ZigTypeIdUndefined:
1360013619 case ZigTypeIdNull:
1360113620 case ZigTypeIdOptional:
......@@ -13629,6 +13648,7 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
1362913648 case ZigTypeIdArgTuple:
1363013649 case ZigTypeIdOpaque:
1363113650 case ZigTypeIdPromise:
13651 case ZigTypeIdEnumLiteral:
1363213652 ir_add_error(ira, target,
1363313653 buf_sprintf("invalid export target type '%s'", buf_ptr(&target->value.type->name)));
1363413654 break;
......@@ -14805,6 +14825,7 @@ static IrInstruction *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_
1480514825 case ZigTypeIdStruct:
1480614826 case ZigTypeIdComptimeFloat:
1480714827 case ZigTypeIdComptimeInt:
14828 case ZigTypeIdEnumLiteral:
1480814829 case ZigTypeIdUndefined:
1480914830 case ZigTypeIdNull:
1481014831 case ZigTypeIdOptional:
......@@ -16448,6 +16469,7 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1644816469 case ZigTypeIdStruct:
1644916470 case ZigTypeIdComptimeFloat:
1645016471 case ZigTypeIdComptimeInt:
16472 case ZigTypeIdEnumLiteral:
1645116473 case ZigTypeIdOptional:
1645216474 case ZigTypeIdErrorUnion:
1645316475 case ZigTypeIdErrorSet:
......@@ -16560,6 +16582,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
1656016582 case ZigTypeIdStruct:
1656116583 case ZigTypeIdComptimeFloat:
1656216584 case ZigTypeIdComptimeInt:
16585 case ZigTypeIdEnumLiteral:
1656316586 case ZigTypeIdOptional:
1656416587 case ZigTypeIdErrorUnion:
1656516588 case ZigTypeIdErrorSet:
......@@ -16613,6 +16636,7 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira,
1661316636 case ZigTypeIdNull:
1661416637 case ZigTypeIdComptimeFloat:
1661516638 case ZigTypeIdComptimeInt:
16639 case ZigTypeIdEnumLiteral:
1661616640 case ZigTypeIdBoundFn:
1661716641 case ZigTypeIdMetaType:
1661816642 case ZigTypeIdArgTuple:
......@@ -17019,6 +17043,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1701917043 case ZigTypeIdFloat:
1702017044 case ZigTypeIdComptimeFloat:
1702117045 case ZigTypeIdComptimeInt:
17046 case ZigTypeIdEnumLiteral:
1702217047 case ZigTypeIdPointer:
1702317048 case ZigTypeIdPromise:
1702417049 case ZigTypeIdFn:
......@@ -18237,6 +18262,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1823718262 case ZigTypeIdUnreachable:
1823818263 case ZigTypeIdComptimeFloat:
1823918264 case ZigTypeIdComptimeInt:
18265 case ZigTypeIdEnumLiteral:
1824018266 case ZigTypeIdUndefined:
1824118267 case ZigTypeIdNull:
1824218268 case ZigTypeIdArgTuple:
......@@ -20443,6 +20469,7 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
2044320469 case ZigTypeIdUnreachable:
2044420470 case ZigTypeIdComptimeFloat:
2044520471 case ZigTypeIdComptimeInt:
20472 case ZigTypeIdEnumLiteral:
2044620473 case ZigTypeIdUndefined:
2044720474 case ZigTypeIdNull:
2044820475 case ZigTypeIdBoundFn:
......@@ -21293,6 +21320,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2129321320 case ZigTypeIdUnreachable:
2129421321 case ZigTypeIdComptimeFloat:
2129521322 case ZigTypeIdComptimeInt:
21323 case ZigTypeIdEnumLiteral:
2129621324 case ZigTypeIdUndefined:
2129721325 case ZigTypeIdNull:
2129821326 case ZigTypeIdPromise:
......@@ -21452,6 +21480,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2145221480 case ZigTypeIdUnreachable:
2145321481 case ZigTypeIdComptimeFloat:
2145421482 case ZigTypeIdComptimeInt:
21483 case ZigTypeIdEnumLiteral:
2145521484 case ZigTypeIdUndefined:
2145621485 case ZigTypeIdNull:
2145721486 case ZigTypeIdPromise:
......@@ -21609,6 +21638,7 @@ static bool type_can_bit_cast(ZigType *t) {
2160921638 case ZigTypeIdUnreachable:
2161021639 case ZigTypeIdComptimeFloat:
2161121640 case ZigTypeIdComptimeInt:
21641 case ZigTypeIdEnumLiteral:
2161221642 case ZigTypeIdUndefined:
2161321643 case ZigTypeIdNull:
2161421644 case ZigTypeIdPointer:
src/parser.cpp+19
......@@ -81,6 +81,7 @@ static AstNode *ast_parse_for_type_expr(ParseContext *pc);
8181static AstNode *ast_parse_while_type_expr(ParseContext *pc);
8282static AstNode *ast_parse_switch_expr(ParseContext *pc);
8383static AstNode *ast_parse_asm_expr(ParseContext *pc);
84static AstNode *ast_parse_enum_lit(ParseContext *pc);
8485static AstNode *ast_parse_asm_output(ParseContext *pc);
8586static AsmOutput *ast_parse_asm_output_item(ParseContext *pc);
8687static AstNode *ast_parse_asm_input(ParseContext *pc);
......@@ -1161,6 +1162,10 @@ static AstNode *ast_parse_prefix_expr(ParseContext *pc) {
11611162// / Block
11621163// / CurlySuffixExpr
11631164static AstNode *ast_parse_primary_expr(ParseContext *pc) {
1165 AstNode *enum_lit = ast_parse_enum_lit(pc);
1166 if (enum_lit != nullptr)
1167 return enum_lit;
1168
11641169 AstNode *asm_expr = ast_parse_asm_expr(pc);
11651170 if (asm_expr != nullptr)
11661171 return asm_expr;
......@@ -1831,6 +1836,18 @@ static AstNode *ast_parse_asm_expr(ParseContext *pc) {
18311836 return res;
18321837}
18331838
1839static AstNode *ast_parse_enum_lit(ParseContext *pc) {
1840 Token *period = eat_token_if(pc, TokenIdDot);
1841 if (period == nullptr)
1842 return nullptr;
1843
1844 Token *identifier = expect_token(pc, TokenIdSymbol);
1845 AstNode *res = ast_create_node(pc, NodeTypeEnumLiteral, period);
1846 res->data.enum_literal.period = period;
1847 res->data.enum_literal.identifier = identifier;
1848 return res;
1849}
1850
18341851// AsmOutput <- COLON AsmOutputList AsmInput?
18351852static AstNode *ast_parse_asm_output(ParseContext *pc) {
18361853 if (eat_token_if(pc, TokenIdColon) == nullptr)
......@@ -3000,5 +3017,7 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
30003017 case NodeTypeSuspend:
30013018 visit_field(&node->data.suspend.block, visit, context);
30023019 break;
3020 case NodeTypeEnumLiteral:
3021 break;
30033022 }
30043023}
std/hash_map.zig+2
......@@ -490,6 +490,7 @@ pub fn autoHash(key: var, comptime rng: *std.rand.Random, comptime HashInt: type
490490 builtin.TypeId.ComptimeFloat,
491491 builtin.TypeId.ComptimeInt,
492492 builtin.TypeId.Type,
493 builtin.TypeId.EnumLiteral,
493494 => return 0,
494495
495496 builtin.TypeId.Pointer => |info| switch (info.size) {
......@@ -531,6 +532,7 @@ pub fn autoEql(a: var, b: @typeOf(a)) bool {
531532 builtin.TypeId.Float,
532533 builtin.TypeId.ComptimeFloat,
533534 builtin.TypeId.ComptimeInt,
535 builtin.TypeId.EnumLiteral,
534536 builtin.TypeId.Promise,
535537 builtin.TypeId.Enum,
536538 builtin.TypeId.BoundFn,
std/testing.zig+1
......@@ -42,6 +42,7 @@ pub fn expectEqual(expected: var, actual: @typeOf(expected)) void {
4242 TypeId.Float,
4343 TypeId.ComptimeFloat,
4444 TypeId.ComptimeInt,
45 TypeId.EnumLiteral,
4546 TypeId.Enum,
4647 TypeId.Fn,
4748 TypeId.Promise,
std/zig/ast.zig+19
......@@ -296,6 +296,7 @@ pub const Node = struct {
296296 // Primary expressions
297297 IntegerLiteral,
298298 FloatLiteral,
299 EnumLiteral,
299300 StringLiteral,
300301 MultilineStringLiteral,
301302 CharLiteral,
......@@ -1849,6 +1850,24 @@ pub const Node = struct {
18491850 }
18501851 };
18511852
1853 pub const EnumLiteral = struct {
1854 base: Node,
1855 dot: TokenIndex,
1856 name: TokenIndex,
1857
1858 pub fn iterate(self: *EnumLiteral, index: usize) ?*Node {
1859 return null;
1860 }
1861
1862 pub fn firstToken(self: *const EnumLiteral) TokenIndex {
1863 return self.dot;
1864 }
1865
1866 pub fn lastToken(self: *const EnumLiteral) TokenIndex {
1867 return self.name;
1868 }
1869 };
1870
18521871 pub const FloatLiteral = struct {
18531872 base: Node,
18541873 token: TokenIndex,
std/zig/parse.zig+21
......@@ -2543,6 +2543,27 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
25432543 _ = try createToCtxLiteral(arena, opt_ctx, ast.Node.IntegerLiteral, token.index);
25442544 continue;
25452545 },
2546 Token.Id.Period => {
2547 const name_token = nextToken(&tok_it, &tree);
2548 if (name_token.ptr.id != Token.Id.Identifier) {
2549 ((try tree.errors.addOne())).* = Error{
2550 .ExpectedToken = Error.ExpectedToken{
2551 .token = name_token.index,
2552 .expected_id = Token.Id.Identifier,
2553 },
2554 };
2555 return tree;
2556 }
2557
2558 const node = try arena.create(ast.Node.EnumLiteral);
2559 node.* = ast.Node.EnumLiteral{
2560 .base = ast.Node{ .id = ast.Node.Id.EnumLiteral },
2561 .dot = token.index,
2562 .name = name_token.index,
2563 };
2564 opt_ctx.store(&node.base);
2565 continue;
2566 },
25462567 Token.Id.FloatLiteral => {
25472568 _ = try createToCtxLiteral(arena, opt_ctx, ast.Node.FloatLiteral, token.index);
25482569 continue;
std/zig/parser_test.zig+7
......@@ -1,3 +1,10 @@
1test "zig fmt: enum literal" {
2 try testCanonical(
3 \\const x = .hi;
4 \\
5 );
6}
7
18test "zig fmt: character literal larger than u8" {
29 try testCanonical(
310 \\const x = '\U01f4a9';
std/zig/render.zig+7
......@@ -1667,6 +1667,13 @@ fn renderExpression(
16671667 return renderToken(tree, stream, asm_output.lastToken(), indent, start_col, space); // )
16681668 },
16691669
1670 ast.Node.Id.EnumLiteral => {
1671 const enum_literal = @fieldParentPtr(ast.Node.EnumLiteral, "base", base);
1672
1673 try renderToken(tree, stream, enum_literal.dot, indent, start_col, Space.None); // .
1674 return renderToken(tree, stream, enum_literal.name, indent, start_col, space); // name
1675 },
1676
16701677 ast.Node.Id.StructField,
16711678 ast.Node.Id.UnionTag,
16721679 ast.Node.Id.EnumTag,
test/stage1/behavior/enum.zig+9
......@@ -892,3 +892,12 @@ test "tag name with assigned enum values" {
892892 var b = LocalFoo.B;
893893 expect(mem.eql(u8, @tagName(b), "B"));
894894}
895
896test "enum literal equality" {
897 const x = .hi;
898 const y = .ok;
899 const z = .hi;
900
901 expect(x != y);
902 expect(x == z);
903}
test/stage1/behavior/type_info.zig+1-1
......@@ -190,7 +190,7 @@ fn testUnion() void {
190190 expect(TypeId(typeinfo_info) == TypeId.Union);
191191 expect(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);
192192 expect(typeinfo_info.Union.tag_type.? == TypeId);
193 expect(typeinfo_info.Union.fields.len == 24);
193 expect(typeinfo_info.Union.fields.len == 25);
194194 expect(typeinfo_info.Union.fields[4].enum_field != null);
195195 expect(typeinfo_info.Union.fields[4].enum_field.?.value == 4);
196196 expect(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int));