From e229885b2bd3b9b017f88d19a773bf989251ad51 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 1 Dec 2019 19:11:00 +0000 Subject: [PATCH] Remove addressing mode `Addressing_Immutable` --- src/check_decl.cpp | 2 -- src/check_expr.cpp | 34 +++++++++++----------------------- src/check_stmt.cpp | 17 ++++++----------- src/check_type.cpp | 4 ++-- src/checker.cpp | 3 +-- src/entity.cpp | 13 +++++-------- src/ir.cpp | 18 +++++++++--------- src/parser.hpp | 1 - 8 files changed, 34 insertions(+), 58 deletions(-) diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 45269762e..5f8a5be4f 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -1154,7 +1154,6 @@ void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *decl, Type *ty if (!(e->flags & EntityFlag_Using)) { continue; } - bool is_immutable = e->Variable.is_immutable; bool is_value = (e->flags & EntityFlag_Value) != 0 && !is_type_pointer(e->type); String name = e->token.string; Type *t = base_type(type_deref(e->type)); @@ -1168,7 +1167,6 @@ void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *decl, Type *ty Entity *f = scope->elements.entries[i].value; if (f->kind == Entity_Variable) { Entity *uvar = alloc_entity_using_variable(e, f->token, f->type, nullptr); - uvar->Variable.is_immutable = is_immutable; if (is_value) uvar->flags |= EntityFlag_Value; ProcUsingVar puv = {e, uvar}; diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 2ee91a5b3..81095db4a 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -1142,9 +1142,6 @@ Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *named_type, Typ if (e->flags & EntityFlag_Value) { o->mode = Addressing_Value; } - if (e->Variable.is_immutable) { - o->mode = Addressing_Immutable; - } break; case Entity_Procedure: @@ -3251,9 +3248,7 @@ Entity *check_selector(CheckerContext *c, Operand *operand, Ast *node, Type *typ break; case Entity_Variable: // TODO(bill): Is this the rule I need? - if (operand->mode == Addressing_Immutable) { - // Okay - } else if (operand->mode == Addressing_Context) { + if (operand->mode == Addressing_Context) { if (sel.indirect) { operand->mode = Addressing_Variable; } @@ -6778,8 +6773,7 @@ bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 *max_count, if (o->mode == Addressing_Constant) { *max_count = o->value.value_string.len; } - if (o->mode != Addressing_Immutable && o->mode != Addressing_Constant) { - // o->mode = Addressing_Variable; + if (o->mode != Addressing_Constant) { o->mode = Addressing_Value; } o->type = t_u8; @@ -6789,27 +6783,25 @@ bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 *max_count, case Type_Array: *max_count = t->Array.count; - if (o->mode != Addressing_Immutable) { - if (indirection) { - o->mode = Addressing_Variable; - } else if (o->mode != Addressing_Variable && - o->mode != Addressing_Constant) { - o->mode = Addressing_Value; - } + if (indirection) { + o->mode = Addressing_Variable; + } else if (o->mode != Addressing_Variable && + o->mode != Addressing_Constant) { + o->mode = Addressing_Value; } o->type = t->Array.elem; return true; case Type_Slice: o->type = t->Slice.elem; - if (o->mode != Addressing_Immutable && o->mode != Addressing_Constant) { + if (o->mode != Addressing_Constant) { o->mode = Addressing_Variable; } return true; case Type_DynamicArray: o->type = t->DynamicArray.elem; - if (o->mode != Addressing_Immutable && o->mode != Addressing_Constant) { + if (o->mode != Addressing_Constant) { o->mode = Addressing_Variable; } return true; @@ -8137,9 +8129,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type return kind; } - if (o->mode != Addressing_Immutable) { - o->mode = Addressing_Value; - } + o->mode = Addressing_Value; if (se->low == nullptr && se->high != nullptr) { // error(se->interval0, "1st index is required if a 2nd index is specified"); @@ -8191,9 +8181,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type } else { Type *t = base_type(o->type); if (t->kind == Type_Pointer && !is_type_empty_union(t->Pointer.elem)) { - if (o->mode != Addressing_Immutable) { - o->mode = Addressing_Variable; - } + o->mode = Addressing_Variable; o->type = t->Pointer.elem; } else { gbString str = expr_to_string(o->expr); diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 8c5293d81..f7c5355c2 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -313,9 +313,7 @@ Type *check_assignment_variable(CheckerContext *ctx, Operand *lhs, Operand *rhs) Entity *e = entity_of_ident(lhs->expr); gbString str = expr_to_string(lhs->expr); - if (lhs->mode == Addressing_Immutable) { - error(lhs->expr, "Cannot assign to an immutable: '%s'", str); - } else if (e != nullptr && e->flags & EntityFlag_Param) { + if (e != nullptr && e->flags & EntityFlag_Param) { error(lhs->expr, "Cannot assign to '%s' which is a procedure parameter", str); } else { error(lhs->expr, "Cannot assign to '%s'", str); @@ -688,8 +686,7 @@ void check_inline_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) { found = scope_lookup_current(ctx->scope, str); } if (found == nullptr) { - bool is_immutable = true; - entity = alloc_entity_variable(ctx->scope, token, type, is_immutable, EntityState_Resolved); + entity = alloc_entity_variable(ctx->scope, token, type, EntityState_Resolved); entity->flags |= EntityFlag_Value; add_entity_definition(&ctx->checker->info, name, entity); } else { @@ -1164,7 +1161,7 @@ void check_type_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) { check_open_scope(ctx, stmt); { - Entity *tag_var = alloc_entity_variable(ctx->scope, lhs->Ident.token, case_type, false, EntityState_Resolved); + Entity *tag_var = alloc_entity_variable(ctx->scope, lhs->Ident.token, case_type, EntityState_Resolved); tag_var->flags |= EntityFlag_Used; tag_var->flags |= EntityFlag_Value; add_entity(ctx->checker, ctx->scope, lhs, tag_var); @@ -1609,8 +1606,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { found = scope_lookup_current(ctx->scope, str); } if (found == nullptr) { - bool is_immutable = false; - entity = alloc_entity_variable(ctx->scope, token, type, is_immutable, EntityState_Resolved); + entity = alloc_entity_variable(ctx->scope, token, type, EntityState_Resolved); entity->flags |= EntityFlag_Value; add_entity_definition(&ctx->checker->info, name, entity); } else { @@ -1815,7 +1811,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { new_name_count += 1; } if (found == nullptr) { - entity = alloc_entity_variable(ctx->scope, token, nullptr, false); + entity = alloc_entity_variable(ctx->scope, token, nullptr); entity->identifier = name; Ast *fl = ctx->foreign_context.curr_library; @@ -1975,7 +1971,6 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { if (e->kind != Entity_Variable) { continue; } - bool is_immutable = e->Variable.is_immutable; String name = e->token.string; Type *t = base_type(type_deref(e->type)); @@ -1987,7 +1982,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { Entity *f = scope->elements.entries[i].value; if (f->kind == Entity_Variable) { Entity *uvar = alloc_entity_using_variable(e, f->token, f->type, nullptr); - uvar->Variable.is_immutable = is_immutable; + uvar->flags |= (e->flags & EntityFlag_Value); Entity *prev = scope_insert(ctx->scope, uvar); if (prev != nullptr) { error(token, "Namespace collision while 'using' '%.*s' of: %.*s", LIT(name), LIT(prev->token.string)); diff --git a/src/check_type.cpp b/src/check_type.cpp index 550761d78..a1ee1d154 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -200,8 +200,8 @@ void check_struct_fields(CheckerContext *ctx, Ast *node, Array *fields Entity *make_names_field_for_struct(CheckerContext *ctx, Scope *scope) { Entity *e = alloc_entity_field(scope, make_token_ident(str_lit("names")), t_string_slice, false, 0); - e->Variable.is_immutable = true; e->flags |= EntityFlag_TypeField; + e->flags |= EntityFlag_Value; return e; } @@ -935,7 +935,7 @@ void check_bit_field_type(CheckerContext *ctx, Type *bit_field_type, Ast *node) u32 bits = cast(u32)bits_; Type *value_type = alloc_type_bit_field_value(bits); - Entity *e = alloc_entity_variable(bit_field_type->BitField.scope, ident->Ident.token, value_type, false); + Entity *e = alloc_entity_variable(bit_field_type->BitField.scope, ident->Ident.token, value_type); e->identifier = ident; e->flags |= EntityFlag_BitFieldValue; diff --git a/src/checker.cpp b/src/checker.cpp index db9deb9ab..cce2aac35 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -7,7 +7,6 @@ void check_expr(CheckerContext *c, Operand *operand, Ast *expression); bool is_operand_value(Operand o) { switch (o.mode) { case Addressing_Value: - case Addressing_Immutable: case Addressing_Context: case Addressing_Variable: case Addressing_Constant: @@ -2607,7 +2606,7 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) { error(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_strings[name->kind])); continue; } - Entity *e = alloc_entity_variable(c->scope, name->Ident.token, nullptr, false); + Entity *e = alloc_entity_variable(c->scope, name->Ident.token, nullptr); e->identifier = name; if (entity_is_private) { diff --git a/src/entity.cpp b/src/entity.cpp index c433d98cb..0c7bc8530 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -121,7 +121,6 @@ struct Entity { String link_prefix; bool is_foreign; bool is_export; - bool is_immutable; } Variable; struct { Type * type_parameter_specialization; @@ -216,9 +215,8 @@ Entity *alloc_entity(EntityKind kind, Scope *scope, Token token, Type *type) { return entity; } -Entity *alloc_entity_variable(Scope *scope, Token token, Type *type, bool is_immutable, EntityState state = EntityState_Unresolved) { +Entity *alloc_entity_variable(Scope *scope, Token token, Type *type, EntityState state = EntityState_Unresolved) { Entity *entity = alloc_entity(Entity_Variable, scope, token, type); - entity->Variable.is_immutable = is_immutable; entity->state = state; return entity; } @@ -250,8 +248,7 @@ Entity *alloc_entity_type_name(Scope *scope, Token token, Type *type, EntityStat } Entity *alloc_entity_param(Scope *scope, Token token, Type *type, bool is_using, bool is_value) { - bool is_immutable = false; - Entity *entity = alloc_entity_variable(scope, token, type, is_immutable); + Entity *entity = alloc_entity_variable(scope, token, type); entity->flags |= EntityFlag_Used; entity->flags |= EntityFlag_Param; entity->state = EntityState_Resolved; @@ -271,7 +268,7 @@ Entity *alloc_entity_const_param(Scope *scope, Token token, Type *type, ExactVal Entity *alloc_entity_field(Scope *scope, Token token, Type *type, bool is_using, i32 field_src_index, EntityState state = EntityState_Unresolved) { - Entity *entity = alloc_entity_variable(scope, token, type, false); + Entity *entity = alloc_entity_variable(scope, token, type); entity->Variable.field_src_index = field_src_index; entity->Variable.field_index = field_src_index; if (is_using) entity->flags |= EntityFlag_Using; @@ -281,7 +278,7 @@ Entity *alloc_entity_field(Scope *scope, Token token, Type *type, bool is_using, } Entity *alloc_entity_array_elem(Scope *scope, Token token, Type *type, i32 field_src_index) { - Entity *entity = alloc_entity_variable(scope, token, type, false); + Entity *entity = alloc_entity_variable(scope, token, type); entity->Variable.field_src_index = field_src_index; entity->Variable.field_index = field_src_index; entity->flags |= EntityFlag_Field; @@ -347,6 +344,6 @@ Entity *alloc_entity_label(Scope *scope, Token token, Type *type, Ast *node, Ast Entity *alloc_entity_dummy_variable(Scope *scope, Token token) { token.string = str_lit("_"); - return alloc_entity_variable(scope, token, nullptr, false); + return alloc_entity_variable(scope, token, nullptr); } diff --git a/src/ir.cpp b/src/ir.cpp index 0051404e9..78fe6519a 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1476,7 +1476,7 @@ irValue *ir_generate_array(irModule *m, Type *elem_type, i64 count, String prefi String s = make_string_c(text); - Entity *e = alloc_entity_variable(nullptr, make_token_ident(s), alloc_type_array(elem_type, count), false); + Entity *e = alloc_entity_variable(nullptr, make_token_ident(s), alloc_type_array(elem_type, count)); irValue *value = ir_value_global(e, nullptr); value->Global.is_private = true; ir_module_add_value(m, e, value); @@ -1743,7 +1743,7 @@ irValue *ir_add_local_generated(irProcedure *proc, Type *type, bool zero_initial if (proc->curr_block) { scope = proc->curr_block->scope; } - Entity *e = alloc_entity_variable(scope, empty_token, type, false); + Entity *e = alloc_entity_variable(scope, empty_token, type); return ir_add_local(proc, e, nullptr, zero_initialized); } @@ -1759,7 +1759,7 @@ irValue *ir_add_global_generated(irModule *m, Type *type, irValue *value) { String name = make_string(str, len-1); Scope *scope = nullptr; - Entity *e = alloc_entity_variable(scope, make_token_ident(name), type, false); + Entity *e = alloc_entity_variable(scope, make_token_ident(name), type); irValue *g = ir_value_global(e, value); ir_module_add_value(m, e, g); map_set(&m->members, hash_string(name), g); @@ -10562,7 +10562,7 @@ void ir_init_module(irModule *m, Checker *c) { isize max_type_info_count = ir_type_info_count(m->info); String name = str_lit(IR_TYPE_INFO_DATA_NAME); - Entity *e = alloc_entity_variable(nullptr, make_token_ident(name), alloc_type_array(t_type_info, max_type_info_count), false); + Entity *e = alloc_entity_variable(nullptr, make_token_ident(name), alloc_type_array(t_type_info, max_type_info_count)); irValue *g = ir_value_global(e, nullptr); g->Global.is_private = true; ir_module_add_value(m, e, g); @@ -10600,7 +10600,7 @@ void ir_init_module(irModule *m, Checker *c) { { String name = str_lit(IR_TYPE_INFO_TYPES_NAME); Entity *e = alloc_entity_variable(nullptr, make_token_ident(name), - alloc_type_array(t_type_info_ptr, count), false); + alloc_type_array(t_type_info_ptr, count)); irValue *g = ir_value_global(e, nullptr); ir_module_add_value(m, e, g); map_set(&m->members, hash_string(name), g); @@ -10609,7 +10609,7 @@ void ir_init_module(irModule *m, Checker *c) { { String name = str_lit(IR_TYPE_INFO_NAMES_NAME); Entity *e = alloc_entity_variable(nullptr, make_token_ident(name), - alloc_type_array(t_string, count), false); + alloc_type_array(t_string, count)); irValue *g = ir_value_global(e, nullptr); ir_module_add_value(m, e, g); map_set(&m->members, hash_string(name), g); @@ -10618,7 +10618,7 @@ void ir_init_module(irModule *m, Checker *c) { { String name = str_lit(IR_TYPE_INFO_OFFSETS_NAME); Entity *e = alloc_entity_variable(nullptr, make_token_ident(name), - alloc_type_array(t_uintptr, count), false); + alloc_type_array(t_uintptr, count)); irValue *g = ir_value_global(e, nullptr); ir_module_add_value(m, e, g); map_set(&m->members, hash_string(name), g); @@ -10628,7 +10628,7 @@ void ir_init_module(irModule *m, Checker *c) { { String name = str_lit(IR_TYPE_INFO_USINGS_NAME); Entity *e = alloc_entity_variable(nullptr, make_token_ident(name), - alloc_type_array(t_bool, count), false); + alloc_type_array(t_bool, count)); irValue *g = ir_value_global(e, nullptr); ir_module_add_value(m, e, g); map_set(&m->members, hash_string(name), g); @@ -10638,7 +10638,7 @@ void ir_init_module(irModule *m, Checker *c) { { String name = str_lit(IR_TYPE_INFO_TAGS_NAME); Entity *e = alloc_entity_variable(nullptr, make_token_ident(name), - alloc_type_array(t_string, count), false); + alloc_type_array(t_string, count)); irValue *g = ir_value_global(e, nullptr); ir_module_add_value(m, e, g); map_set(&m->members, hash_string(name), g); diff --git a/src/parser.hpp b/src/parser.hpp index dfcf8b60e..13c66d8d1 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -10,7 +10,6 @@ enum AddressingMode { Addressing_Invalid, // invalid addressing mode Addressing_NoValue, // no value (void in C) Addressing_Value, // computed value (rvalue) - Addressing_Immutable, // immutable computed value (const rvalue) Addressing_Context, // context value Addressing_Variable, // addressable variable (lvalue) Addressing_Constant, // constant