Skip to content

Commit 5b27943

Browse files
committed
implement anon struct literal syntax
This implements stage1 parser support for anonymous struct literal syntax (see #685), as well as semantic analysis support for anonymous struct literals and anonymous list literals (see #208). The semantic analysis works when there is a type coercion in the result location; inferring the struct type based on the values in the literal is not implemented yet. Also remaining to do is zig fmt support for this new syntax and documentation updates.
1 parent de30438 commit 5b27943

File tree

9 files changed

+174
-120
lines changed

9 files changed

+174
-120
lines changed

lib/std/builtin.zig

+2-31
Original file line numberDiff line numberDiff line change
@@ -90,40 +90,11 @@ pub const Mode = enum {
9090
ReleaseSmall,
9191
};
9292

93-
/// This data structure is used by the Zig language code generation and
94-
/// therefore must be kept in sync with the compiler implementation.
95-
pub const TypeId = enum {
96-
Type,
97-
Void,
98-
Bool,
99-
NoReturn,
100-
Int,
101-
Float,
102-
Pointer,
103-
Array,
104-
Struct,
105-
ComptimeFloat,
106-
ComptimeInt,
107-
Undefined,
108-
Null,
109-
Optional,
110-
ErrorUnion,
111-
ErrorSet,
112-
Enum,
113-
Union,
114-
Fn,
115-
BoundFn,
116-
ArgTuple,
117-
Opaque,
118-
Frame,
119-
AnyFrame,
120-
Vector,
121-
EnumLiteral,
122-
};
93+
pub const TypeId = @TagType(TypeInfo);
12394

12495
/// This data structure is used by the Zig language code generation and
12596
/// therefore must be kept in sync with the compiler implementation.
126-
pub const TypeInfo = union(TypeId) {
97+
pub const TypeInfo = union(enum) {
12798
Type: void,
12899
Void: void,
129100
Bool: void,

src/all_types.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,7 @@ struct TypeStructField {
12371237
enum ResolveStatus {
12381238
ResolveStatusUnstarted,
12391239
ResolveStatusInvalid,
1240+
ResolveStatusBeingInferred,
12401241
ResolveStatusZeroBitsKnown,
12411242
ResolveStatusAlignmentKnown,
12421243
ResolveStatusSizeKnown,
@@ -1285,6 +1286,7 @@ struct ZigTypeStruct {
12851286
bool requires_comptime;
12861287
bool resolve_loop_flag_zero_bits;
12871288
bool resolve_loop_flag_other;
1289+
bool is_inferred;
12881290
};
12891291

12901292
struct ZigTypeOptional {
@@ -2812,7 +2814,7 @@ struct IrInstructionElemPtr {
28122814

28132815
IrInstruction *array_ptr;
28142816
IrInstruction *elem_index;
2815-
IrInstruction *init_array_type;
2817+
AstNode *init_array_type_source_node;
28162818
PtrLen ptr_len;
28172819
bool safety_check_on;
28182820
};
@@ -2909,11 +2911,11 @@ struct IrInstructionResizeSlice {
29092911
struct IrInstructionContainerInitList {
29102912
IrInstruction base;
29112913

2912-
IrInstruction *container_type;
29132914
IrInstruction *elem_type;
29142915
size_t item_count;
29152916
IrInstruction **elem_result_loc_list;
29162917
IrInstruction *result_loc;
2918+
AstNode *init_array_type_source_node;
29172919
};
29182920

29192921
struct IrInstructionContainerInitFieldsField {
@@ -2926,7 +2928,6 @@ struct IrInstructionContainerInitFieldsField {
29262928
struct IrInstructionContainerInitFields {
29272929
IrInstruction base;
29282930

2929-
IrInstruction *container_type;
29302931
size_t field_count;
29312932
IrInstructionContainerInitFieldsField *fields;
29322933
IrInstruction *result_loc;

src/analyze.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ void init_scope(CodeGen *g, Scope *dest, ScopeId id, AstNode *source_node, Scope
140140
static ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type,
141141
ZigType *import, Buf *bare_name)
142142
{
143-
assert(node == nullptr || node->type == NodeTypeContainerDecl || node->type == NodeTypeFnCallExpr);
144143
ScopeDecls *scope = allocate<ScopeDecls>(1);
145144
init_scope(g, &scope->base, ScopeIdDecls, node, parent);
146145
scope->decl_table.init(4);
@@ -346,6 +345,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
346345
switch (status) {
347346
case ResolveStatusInvalid:
348347
zig_unreachable();
348+
case ResolveStatusBeingInferred:
349+
zig_unreachable();
349350
case ResolveStatusUnstarted:
350351
case ResolveStatusZeroBitsKnown:
351352
return true;
@@ -362,6 +363,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
362363
switch (status) {
363364
case ResolveStatusInvalid:
364365
zig_unreachable();
366+
case ResolveStatusBeingInferred:
367+
zig_unreachable();
365368
case ResolveStatusUnstarted:
366369
return true;
367370
case ResolveStatusZeroBitsKnown:
@@ -6132,6 +6135,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
61326135
continue;
61336136
if (instruction->ref_count == 0)
61346137
continue;
6138+
if ((err = type_resolve(g, instruction->value.type, ResolveStatusZeroBitsKnown)))
6139+
return ErrorSemanticAnalyzeFail;
61356140
if (!type_has_bits(instruction->value.type))
61366141
continue;
61376142
if (scope_needs_spill(instruction->scope)) {
@@ -6271,6 +6276,8 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
62716276
switch (status) {
62726277
case ResolveStatusUnstarted:
62736278
return ErrorNone;
6279+
case ResolveStatusBeingInferred:
6280+
zig_unreachable();
62746281
case ResolveStatusInvalid:
62756282
zig_unreachable();
62766283
case ResolveStatusZeroBitsKnown:
@@ -9038,4 +9045,3 @@ Error analyze_import(CodeGen *g, ZigType *source_import, Buf *import_target_str,
90389045
*out_import = add_source_file(g, target_package, resolved_path, import_code, source_kind);
90399046
return ErrorNone;
90409047
}
9041-

src/ast_render.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,9 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
821821
break;
822822
}
823823
case NodeTypeContainerInitExpr:
824-
render_node_ungrouped(ar, node->data.container_init_expr.type);
824+
if (node->data.container_init_expr.type != nullptr) {
825+
render_node_ungrouped(ar, node->data.container_init_expr.type);
826+
}
825827
if (node->data.container_init_expr.kind == ContainerInitKindStruct) {
826828
fprintf(ar->f, "{\n");
827829
ar->indent += ar->indent_size;

0 commit comments

Comments
 (0)