From 10ea2fa67e1b084749a2acc9d11a95dbb6953add Mon Sep 17 00:00:00 2001 From: Krystine Sherwin <93062060+KrystalDelusion@users.noreply.github.com> Date: Sat, 10 May 2025 17:24:13 +1200 Subject: [PATCH] ast.h: Move attribute check into set There are a number of places that check for an existing attribute and delete it before assigning a new node to it. Reduce code duplication by doing it as part of the `set_attribute()` method. --- frontends/ast/ast.cc | 4 ---- frontends/ast/ast.h | 2 ++ frontends/ast/simplify.cc | 6 ------ 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/frontends/ast/ast.cc b/frontends/ast/ast.cc index 431f7b4f8..e2e83a225 100644 --- a/frontends/ast/ast.cc +++ b/frontends/ast/ast.cc @@ -1174,10 +1174,6 @@ static RTLIL::Module *process_module(RTLIL::Design *design, AstNode *ast, bool d delete ast->attributes.at(ID::lib_whitebox); ast->attributes.erase(ID::lib_whitebox); } else { - if (ast->attributes.count(ID::whitebox)) { - delete ast->attributes.at(ID::whitebox); - ast->attributes.erase(ID::whitebox); - } AstNode *n = ast->attributes.at(ID::lib_whitebox); ast->set_attribute(ID::whitebox, n); ast->attributes.erase(ID::lib_whitebox); diff --git a/frontends/ast/ast.h b/frontends/ast/ast.h index 2c2d408ce..66417cda2 100644 --- a/frontends/ast/ast.h +++ b/frontends/ast/ast.h @@ -360,6 +360,8 @@ namespace AST void set_attribute(RTLIL::IdString key, AstNode *node) { + if (attributes.count(key)) + delete attributes.at(key); attributes[key] = node; node->set_in_param_flag(true); } diff --git a/frontends/ast/simplify.cc b/frontends/ast/simplify.cc index 3411d6c03..73bf3245a 100644 --- a/frontends/ast/simplify.cc +++ b/frontends/ast/simplify.cc @@ -1110,8 +1110,6 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin if (node->children.size() == 1 && node->children[0]->type == AST_RANGE) { for (auto c : node->children[0]->children) { if (!c->is_simple_const_expr()) { - if (attributes.count(ID::dynports)) - delete attributes.at(ID::dynports); set_attribute(ID::dynports, AstNode::mkconst_int(1, true)); } } @@ -1159,8 +1157,6 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin if (node->is_signed) first_node->is_signed = true; for (auto &it : node->attributes) { - if (first_node->attributes.count(it.first) > 0) - delete first_node->attributes[it.first]; first_node->set_attribute(it.first, it.second->clone()); } children.erase(children.begin()+(i--)); @@ -1919,8 +1915,6 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin if (!str.empty() && str[0] == '\\' && (template_node->type == AST_STRUCT || template_node->type == AST_UNION)) { // replace instance with wire representing the packed structure newNode = make_packed_struct(template_node, str, attributes); - if (newNode->attributes.count(ID::wiretype)) - delete newNode->attributes[ID::wiretype]; newNode->set_attribute(ID::wiretype, mkconst_str(resolved_type_node->str)); // add original input/output attribute to resolved wire newNode->is_input = this->is_input;