diff options
| author | gingerBill <ginger.bill.22@gmail.com> | 2016-08-16 21:01:59 +0100 |
|---|---|---|
| committer | gingerBill <ginger.bill.22@gmail.com> | 2016-08-16 21:01:59 +0100 |
| commit | 9e61e3beee6ca5ae48db68fd03ed0c73604e7279 (patch) | |
| tree | 937d3130c4a68e2b36a3af5380b7d19f2e54aafd /src | |
| parent | e8530ca883edd79c188443ced54cea2c5d3ad4ed (diff) | |
field = value, for structure literals
Diffstat (limited to 'src')
| -rw-r--r-- | src/checker/expr.cpp | 82 | ||||
| -rw-r--r-- | src/codegen/ssa.cpp | 40 | ||||
| -rw-r--r-- | src/main.cpp | 30 | ||||
| -rw-r--r-- | src/parser.cpp | 15 |
4 files changed, 125 insertions, 42 deletions
diff --git a/src/checker/expr.cpp b/src/checker/expr.cpp index 542afb853..8f017df80 100644 --- a/src/checker/expr.cpp +++ b/src/checker/expr.cpp @@ -1862,21 +1862,69 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ { // Checker values AstNode *elem = cl->elem_list; isize field_count = t->structure.field_count; - isize index = 0; - for (; - elem != NULL; - elem = elem->next, index++) { - Entity *field = t->structure.fields[index]; - - check_expr(c, o, elem); - if (index >= field_count) { - error(&c->error_collector, ast_node_token(o->expr), "Too many values in structure literal, expected %td", field_count); - break; + if (elem->kind == AstNode_FieldValue) { + b32 *fields_visited = gb_alloc_array(c->allocator, b32, field_count); + + for (; + elem != NULL; + elem = elem->next) { + if (elem->kind != AstNode_FieldValue) { + error(&c->error_collector, ast_node_token(elem), + "Mixture of `field = value` and value elements in a structure literal is not allowed"); + continue; + } + ast_node(kv, FieldValue, elem); + if (kv->field->kind != AstNode_Ident) { + gbString expr_str = expr_to_string(kv->field); + defer (gb_string_free(expr_str)); + error(&c->error_collector, ast_node_token(elem), + "Invalid field name `%s` in structure literal", expr_str); + continue; + } + String name = kv->field->Ident.token.string; + + isize index = 0; + Entity *e = lookup_field(type, kv->field, &index); + if (e == NULL) { + error(&c->error_collector, ast_node_token(elem), + "Unknown field `%.*s` in structure literal", LIT(name)); + continue; + } + Entity *field = t->structure.fields[index]; + add_entity_use(&c->info, kv->field, field); + + if (fields_visited[index]) { + error(&c->error_collector, ast_node_token(elem), + "Duplicate field `%.*s` in structure literal", LIT(name)); + continue; + } + + fields_visited[index] = true; + check_expr(c, o, kv->value); + check_assignment(c, o, field->type, make_string("structure literal")); + } + } else { + isize index = 0; + for (; + elem != NULL; + elem = elem->next, index++) { + if (elem->kind == AstNode_FieldValue) { + error(&c->error_collector, ast_node_token(elem), + "Mixture of `field = value` and value elements in a structure literal is not allowed"); + continue; + } + Entity *field = t->structure.fields[index]; + + check_expr(c, o, elem); + if (index >= field_count) { + error(&c->error_collector, ast_node_token(o->expr), "Too many values in structure literal, expected %td", field_count); + break; + } + check_assignment(c, o, field->type, make_string("structure literal")); + } + if (cl->elem_count < field_count) { + error(&c->error_collector, cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elem_count); } - check_assignment(c, o, field->type, make_string("structure literal")); - } - if (cl->elem_count < field_count) { - error(&c->error_collector, cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elem_count); } } @@ -2368,6 +2416,12 @@ gbString write_expr_to_string(gbString str, AstNode *node) { str = gb_string_appendc(str, ".."); case_end; + case_ast_node(fv, FieldValue, node); + str = write_expr_to_string(str, fv->field); + str = gb_string_appendc(str, " = "); + str = write_expr_to_string(str, fv->value); + case_end; + case_ast_node(pt, PointerType, node); str = gb_string_appendc(str, "^"); str = write_expr_to_string(str, pt->type); diff --git a/src/codegen/ssa.cpp b/src/codegen/ssa.cpp index f6497fc81..5ff0c7cfd 100644 --- a/src/codegen/ssa.cpp +++ b/src/codegen/ssa.cpp @@ -1576,20 +1576,34 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue case Type_Structure: { auto *st = &base_type->structure; - isize index = 0; - for (AstNode *elem = cl->elem_list; - elem != NULL; - elem = elem->next, index++) { - ssaValue *field_expr = ssa_build_expr(proc, elem); - Type *t = ssa_value_type(field_expr); - GB_ASSERT(t->kind != Type_Tuple); - Entity *field = st->fields[index]; - Type *ft = field->type; - ssaValue *fv = ssa_emit_conv(proc, field_expr, ft); - ssaValue *gep = ssa_emit_struct_gep(proc, v, index, ft); - ssa_emit_store(proc, gep, fv); - } + if (cl->elem_list != NULL) { + isize index = 0; + AstNode *elem = cl->elem_list; + for (; + elem != NULL; + elem = elem->next, index++) { + isize field_index = index; + ssaValue *field_expr = NULL; + Entity *field = NULL; + + if (elem->kind == AstNode_FieldValue) { + ast_node(kv, FieldValue, elem); + Entity *e = lookup_field(base_type, kv->field, &field_index); + field_expr = ssa_build_expr(proc, kv->value); + } else { + field_expr = ssa_build_expr(proc, elem); + } + + GB_ASSERT(ssa_value_type(field_expr)->kind != Type_Tuple); + field = st->fields[field_index]; + + Type *ft = field->type; + ssaValue *fv = ssa_emit_conv(proc, field_expr, ft); + ssaValue *gep = ssa_emit_struct_gep(proc, v, field_index, ft); + ssa_emit_store(proc, gep, fv); + } + } } break; case Type_Array: { isize index = 0; diff --git a/src/main.cpp b/src/main.cpp index 1eb4b92b8..c7f7ce764 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #include "checker/checker.cpp" #include "codegen/codegen.cpp" -void win32_exec_command_line_app(char *fmt, ...) { +i32 win32_exec_command_line_app(char *fmt, ...) { STARTUPINFOA start_info = {gb_size_of(STARTUPINFOA)}; PROCESS_INFORMATION pi = {}; start_info.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; @@ -25,12 +25,19 @@ void win32_exec_command_line_app(char *fmt, ...) { NULL, NULL, true, 0, NULL, NULL, &start_info, &pi)) { WaitForSingleObject(pi.hProcess, INFINITE); + + DWORD exit_code = 0; + GetExitCodeProcess(pi.hProcess, &exit_code); + CloseHandle(pi.hProcess); CloseHandle(pi.hThread); + + return cast(i32)exit_code; } else { // NOTE(bill): failed to create process } + return 0; } int main(int argc, char **argv) { @@ -71,21 +78,20 @@ int main(int argc, char **argv) { char const *output_name = ssa.output_file.filename; isize base_name_len = gb_path_extension(output_name)-1 - output_name; - win32_exec_command_line_app( + i32 exit_code = win32_exec_command_line_app( "opt -mem2reg %s -o %.*s.bc", output_name, cast(int)base_name_len, output_name); - win32_exec_command_line_app( - "clang %.*s.bc -o %.*s.exe -Wno-override-module " - "-lkernel32.lib -luser32.lib -lgdi32.lib -lopengl32.lib", - cast(int)base_name_len, output_name, - cast(int)base_name_len, output_name); - - - if (run_output) { - win32_exec_command_line_app("%.*s.exe", cast(int)base_name_len, output_name); + if (exit_code == 0) { + win32_exec_command_line_app( + "clang %.*s.bc -o %.*s.exe -Wno-override-module " + "-lkernel32.lib -luser32.lib -lgdi32.lib -lopengl32.lib", + cast(int)base_name_len, output_name, + cast(int)base_name_len, output_name); + if (run_output) { + win32_exec_command_line_app("%.*s.exe", cast(int)base_name_len, output_name); + } } - return 0; } } diff --git a/src/parser.cpp b/src/parser.cpp index d6c450a88..051cd9779 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -115,6 +115,7 @@ AST_NODE_KIND(_ExprBegin, struct{}) \ AstNode *low, *high, *max; \ b32 triple_indexed; \ }) \ + AST_NODE_KIND(FieldValue, struct { Token eq; AstNode *field, *value; }) \ AST_NODE_KIND(Ellipsis, struct { Token token; }) \ AST_NODE_KIND(_ExprEnd, struct{}) \ AST_NODE_KIND(_StmtBegin, struct{}) \ @@ -301,6 +302,8 @@ Token ast_node_token(AstNode *node) { return node->SliceExpr.open; case AstNode_Ellipsis: return node->Ellipsis.token; + case AstNode_FieldValue: + return node->FieldValue.eq; case AstNode_DerefExpr: return node->DerefExpr.op; case AstNode_BadStmt: @@ -572,6 +575,13 @@ gb_inline AstNode *make_proc_lit(AstFile *f, AstNode *type, AstNode *body, u64 t return result; } +gb_inline AstNode *make_field_value(AstFile *f, AstNode *field, AstNode *value, Token eq) { + AstNode *result = make_node(f, AstNode_FieldValue); + result->FieldValue.field = field; + result->FieldValue.value = value; + result->FieldValue.eq = eq; + return result; +} gb_inline AstNode *make_compound_lit(AstFile *f, AstNode *type, AstNode *elem_list, isize elem_count, Token open, Token close) { @@ -930,12 +940,11 @@ AstNode *parse_element_list(AstFile *f, isize *element_count_) { while (f->cursor[0].kind != Token_CloseBrace && f->cursor[0].kind != Token_EOF) { AstNode *elem = parse_value(f); - #if 0 - // TODO(bill): Designated Initializers if (f->cursor[0].kind == Token_Eq) { Token eq = expect_token(f, Token_Eq); + AstNode *value = parse_value(f); + elem = make_field_value(f, elem, value, eq); } - #endif DLIST_APPEND(root, curr, elem); element_count++; if (f->cursor[0].kind != Token_Comma) |