From 6b5e9aec8e0120669cad45056fbf268163b02233 Mon Sep 17 00:00:00 2001 From: Ginger Bill Date: Mon, 12 Jun 2017 15:42:21 +0100 Subject: Pascal style declaration grouping with () --- src/ir.cpp | 134 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 72 insertions(+), 62 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index abf2d80be..accdeb86e 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -5793,7 +5793,7 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) { case_ast_node(us, UsingStmt, node); for_array(i, us->list) { AstNode *decl = unparen_expr(us->list[i]); - if (decl->kind == AstNode_ValueDecl) { + if (decl->kind == AstNode_GenDecl) { ir_build_stmt(proc, decl); } } @@ -5812,56 +5812,88 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) { ir_build_assign_op(proc, addr, v_one, op); case_end; - case_ast_node(vd, ValueDecl, node); - if (vd->token.kind != Token_const) { - irModule *m = proc->module; - gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena); + case_ast_node(gd, GenDecl, node); + for_array(i, gd->specs) { + AstNode *spec = gd->specs[i]; + switch (gd->token.kind) { + case Token_var: + case Token_let: { + ast_node(vd, ValueSpec, spec); + + irModule *m = proc->module; + gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena); + + if (vd->values.count == 0) { // declared and zero-initialized + for_array(i, vd->names) { + AstNode *name = vd->names[i]; + if (!ir_is_blank_ident(name)) { + ir_add_local_for_identifier(proc, name, true); + } + } + } else { // Tuple(s) + Array lvals = {}; + Array inits = {}; + array_init(&lvals, m->tmp_allocator, vd->names.count); + array_init(&inits, m->tmp_allocator, vd->names.count); + + for_array(i, vd->names) { + AstNode *name = vd->names[i]; + irAddr lval = ir_addr(NULL); + if (!ir_is_blank_ident(name)) { + ir_add_local_for_identifier(proc, name, false); + lval = ir_build_addr(proc, name); + } - if (vd->values.count == 0) { // declared and zero-initialized - for_array(i, vd->names) { - AstNode *name = vd->names[i]; - if (!ir_is_blank_ident(name)) { - ir_add_local_for_identifier(proc, name, true); + array_add(&lvals, lval); } - } - } else { // Tuple(s) - Array lvals = {}; - Array inits = {}; - array_init(&lvals, m->tmp_allocator, vd->names.count); - array_init(&inits, m->tmp_allocator, vd->names.count); - - for_array(i, vd->names) { - AstNode *name = vd->names[i]; - irAddr lval = ir_addr(NULL); - if (!ir_is_blank_ident(name)) { - ir_add_local_for_identifier(proc, name, false); - lval = ir_build_addr(proc, name); + + for_array(i, vd->values) { + irValue *init = ir_build_expr(proc, vd->values[i]); + Type *t = ir_type(init); + if (t->kind == Type_Tuple) { + for (isize i = 0; i < t->Tuple.variable_count; i++) { + Entity *e = t->Tuple.variables[i]; + irValue *v = ir_emit_struct_ev(proc, init, i); + array_add(&inits, v); + } + } else { + array_add(&inits, init); + } } - array_add(&lvals, lval); - } - for_array(i, vd->values) { - irValue *init = ir_build_expr(proc, vd->values[i]); - Type *t = ir_type(init); - if (t->kind == Type_Tuple) { - for (isize i = 0; i < t->Tuple.variable_count; i++) { - Entity *e = t->Tuple.variables[i]; - irValue *v = ir_emit_struct_ev(proc, init, i); - array_add(&inits, v); - } - } else { - array_add(&inits, init); + for_array(i, inits) { + ir_addr_store(proc, lvals[i], inits[i]); } } + gb_temp_arena_memory_end(tmp); + } break; - for_array(i, inits) { - ir_addr_store(proc, lvals[i], inits[i]); + case Token_type: { + ast_node(td, TypeSpec, node); + + AstNode *ident = td->name; + GB_ASSERT(ident->kind == AstNode_Ident); + Entity *e = entity_of_ident(proc->module->info, ident); + GB_ASSERT(e != NULL); + if (e->kind == Entity_TypeName) { + // NOTE(bill): Generate a new name + // parent_proc.name-guid + String ts_name = e->token.string; + isize name_len = proc->name.len + 1 + ts_name.len + 1 + 10 + 1; + u8 *name_text = gb_alloc_array(proc->module->allocator, u8, name_len); + i32 guid = cast(i32)proc->module->members.entries.count; + name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s.%.*s-%d", LIT(proc->name), LIT(ts_name), guid); + String name = make_string(name_text, name_len-1); + + irValue *value = ir_value_type_name(proc->module->allocator, + name, e->type); + map_set(&proc->module->entity_names, hash_pointer(e), name); + ir_gen_global_type_name(proc->module, e, name); } + } break; } - - gb_temp_arena_memory_end(tmp); } case_end; @@ -5936,28 +5968,6 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) { } case_end; - case_ast_node(td, TypeDecl, node); - AstNode *ident = td->name; - GB_ASSERT(ident->kind == AstNode_Ident); - Entity *e = entity_of_ident(proc->module->info, ident); - GB_ASSERT(e != NULL); - if (e->kind == Entity_TypeName) { - // NOTE(bill): Generate a new name - // parent_proc.name-guid - String ts_name = e->token.string; - isize name_len = proc->name.len + 1 + ts_name.len + 1 + 10 + 1; - u8 *name_text = gb_alloc_array(proc->module->allocator, u8, name_len); - i32 guid = cast(i32)proc->module->members.entries.count; - name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s.%.*s-%d", LIT(proc->name), LIT(ts_name), guid); - String name = make_string(name_text, name_len-1); - - irValue *value = ir_value_type_name(proc->module->allocator, - name, e->type); - map_set(&proc->module->entity_names, hash_pointer(e), name); - ir_gen_global_type_name(proc->module, e, name); - } - case_end; - case_ast_node(as, AssignStmt, node); ir_emit_comment(proc, str_lit("AssignStmt")); -- cgit v1.2.3