3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-06-19 12:23:39 +00:00

ast: ownership for string values

This commit is contained in:
Emil J. Tywoniak 2025-06-17 01:57:51 +02:00
parent 8a9f491ffc
commit 20225d19ae
2 changed files with 71 additions and 75 deletions

View file

@ -74,12 +74,12 @@ YOSYS_NAMESPACE_END
log("Lexer warning: The SystemVerilog keyword `%s' (at %s:%d) is not "\
"recognized unless read_verilog is called with -sv!\n", YYText(), \
AST::current_filename.c_str(), yylineno); \
string_t val = new std::string(std::string("\\") + YYText()); \
return parser::make_TOK_ID(val, out_loc);
string_t val = std::make_unique<std::string>(std::string("\\") + YYText()); \
return parser::make_TOK_ID(std::move(val), out_loc);
#define NON_KEYWORD() \
string_t val = new std::string(std::string("\\") + YYText()); \
return parser::make_TOK_ID(val, out_loc);
string_t val = std::make_unique<std::string>(std::string("\\") + YYText()); \
return parser::make_TOK_ID(std::move(val), out_loc);
// #define YY_INPUT(buf,result,max_size) \
// result = readsome(*extra->lexin, buf, max_size)
@ -289,8 +289,8 @@ TIME_SCALE_SUFFIX [munpf]?s
[a-zA-Z_$][a-zA-Z0-9_$]*/[ \t\r\n]*:[ \t\r\n]*(assert|assume|cover|restrict)[^a-zA-Z0-9_$\.] {
if (!strcmp(YYText(), "default"))
return parser::make_TOK_DEFAULT(out_loc);
string_t val = new std::string(std::string("\\") + YYText());
return parser::make_TOK_SVA_LABEL(val, out_loc);
string_t val = std::make_unique<std::string>(std::string("\\") + YYText());
return parser::make_TOK_SVA_LABEL(std::move(val), out_loc);
}
"assert" { if (mode->formal) return parser::make_TOK_ASSERT(out_loc); SV_KEYWORD(parser::make_TOK_ASSERT(out_loc)); }
@ -339,35 +339,35 @@ TIME_SCALE_SUFFIX [munpf]?s
"packed" { SV_KEYWORD(parser::make_TOK_PACKED(out_loc)); }
{UNSIGNED_NUMBER} {
string_t val = new std::string(YYText());
return parser::make_TOK_CONSTVAL(val, out_loc);
string_t val = std::make_unique<std::string>(YYText());
return parser::make_TOK_CONSTVAL(std::move(val), out_loc);
}
\'[01zxZX] {
string_t val = new std::string(YYText());
return parser::make_TOK_UNBASED_UNSIZED_CONSTVAL(val, out_loc);
string_t val = std::make_unique<std::string>(YYText());
return parser::make_TOK_UNBASED_UNSIZED_CONSTVAL(std::move(val), out_loc);
}
\'[sS]?[bodhBODH] {
BEGIN(BASED_CONST);
string_t val = new std::string(YYText());
return parser::make_TOK_BASE(val, out_loc);
string_t val = std::make_unique<std::string>(YYText());
return parser::make_TOK_BASE(std::move(val), out_loc);
}
<BASED_CONST>[0-9a-fA-FzxZX?][0-9a-fA-FzxZX?_]* {
BEGIN(0);
string_t val = new std::string(YYText());
return parser::make_TOK_BASED_CONSTVAL(val, out_loc);
string_t val = std::make_unique<std::string>(YYText());
return parser::make_TOK_BASED_CONSTVAL(std::move(val), out_loc);
}
{FIXED_POINT_NUMBER_DEC} {
string_t val = new std::string(YYText());
return parser::make_TOK_REALVAL(val, out_loc);
string_t val = std::make_unique<std::string>(YYText());
return parser::make_TOK_REALVAL(std::move(val), out_loc);
}
{FIXED_POINT_NUMBER_NO_DEC} {
string_t val = new std::string(YYText());
return parser::make_TOK_REALVAL(val, out_loc);
string_t val = std::make_unique<std::string>(YYText());
return parser::make_TOK_REALVAL(std::move(val), out_loc);
}
\" { BEGIN(STRING); }
@ -407,33 +407,33 @@ TIME_SCALE_SUFFIX [munpf]?s
yystr[j++] = yystr[i++];
}
yystr[j] = 0;
string_t val = new std::string(yystr, j);
string_t val = std::make_unique<std::string>(yystr, j);
free(yystr);
return parser::make_TOK_STRING(val, out_loc);
return parser::make_TOK_STRING(std::move(val), out_loc);
}
and|nand|or|nor|xor|xnor|not|buf|bufif0|bufif1|notif0|notif1 {
auto val = new std::string(YYText());
return parser::make_TOK_PRIMITIVE(val, out_loc);
auto val = std::make_unique<std::string>(YYText());
return parser::make_TOK_PRIMITIVE(std::move(val), out_loc);
}
supply0 { return parser::make_TOK_SUPPLY0(out_loc); }
supply1 { return parser::make_TOK_SUPPLY1(out_loc); }
"$"(display[bho]?|write[bho]?|strobe|monitor|time|realtime|stop|finish|dumpfile|dumpvars|dumpon|dumpoff|dumpall) {
auto val = new std::string(YYText());
return parser::make_TOK_ID(val, out_loc);
auto val = std::make_unique<std::string>(YYText());
return parser::make_TOK_ID(std::move(val), out_loc);
}
"$"(setup|hold|setuphold|removal|recovery|recrem|skew|timeskew|fullskew|nochange) {
if (!mode->specify) REJECT;
auto val = new std::string(YYText());
return parser::make_TOK_ID(val, out_loc);
auto val = std::make_unique<std::string>(YYText());
return parser::make_TOK_ID(std::move(val), out_loc);
}
"$"(info|warning|error|fatal) {
auto val = new std::string(YYText());
return parser::make_TOK_MSG_TASKS(val, out_loc);
auto val = std::make_unique<std::string>(YYText());
return parser::make_TOK_MSG_TASKS(std::move(val), out_loc);
}
"$signed" { return parser::make_TOK_TO_SIGNED(out_loc); }
@ -444,15 +444,15 @@ supply1 { return parser::make_TOK_SUPPLY1(out_loc); }
auto s = std::string("\\") + YYText();
if (extra->pkg_user_types.count(s) > 0) {
// package qualified typedefed name
auto val = new std::string(s);
return parser::make_TOK_PKG_USER_TYPE(val, out_loc);
auto val = std::make_unique<std::string>(s);
return parser::make_TOK_PKG_USER_TYPE(std::move(val), out_loc);
}
else {
// backup before :: just return first part
size_t len = strchr(YYText(), ':') - YYText();
yyless(len);
auto val = new std::string(std::string("\\") + YYText());
return parser::make_TOK_ID(val, out_loc);
auto val = std::make_unique<std::string>(std::string("\\") + YYText());
return parser::make_TOK_ID(std::move(val), out_loc);
}
}
@ -460,18 +460,18 @@ supply1 { return parser::make_TOK_SUPPLY1(out_loc); }
auto s = std::string("\\") + YYText();
if (isUserType(extra, s)) {
// previously typedefed name
auto val = new std::string(s);
return parser::make_TOK_USER_TYPE(val, out_loc);
auto val = std::make_unique<std::string>(s);
return parser::make_TOK_USER_TYPE(std::move(val), out_loc);
}
else {
auto val = new std::string(std::string("\\") + YYText());
return parser::make_TOK_ID(val, out_loc);
auto val = std::make_unique<std::string>(std::string("\\") + YYText());
return parser::make_TOK_ID(std::move(val), out_loc);
}
}
[a-zA-Z_$][a-zA-Z0-9_$\.]* {
auto val = new std::string(std::string("\\") + YYText());
return parser::make_TOK_ID(val, out_loc);
auto val = std::make_unique<std::string>(std::string("\\") + YYText());
return parser::make_TOK_ID(std::move(val), out_loc);
}
"/*"[ \t]*(synopsys|synthesis)[ \t]*translate_off[ \t]*"*/" {
@ -532,8 +532,8 @@ import[ \t\r\n]+\"(DPI|DPI-C)\"[ \t\r\n]+function[ \t\r\n]+ {
}
<IMPORT_DPI>[a-zA-Z_$][a-zA-Z0-9_$]* {
auto val = new std::string(std::string("\\") + YYText());
return parser::make_TOK_ID(val, out_loc);
auto val = std::make_unique<std::string>(std::string("\\") + YYText());
return parser::make_TOK_ID(std::move(val), out_loc);
}
<IMPORT_DPI>[ \t\r\n] /* ignore whitespaces */
@ -548,8 +548,8 @@ import[ \t\r\n]+\"(DPI|DPI-C)\"[ \t\r\n]+function[ \t\r\n]+ {
}
"\\"[^ \t\r\n]+ {
auto val = new std::string(YYText());
return parser::make_TOK_ID(val, out_loc);
auto val = std::make_unique<std::string>(YYText());
return parser::make_TOK_ID(std::move(val), out_loc);
}
"(*" { return parser::make_ATTR_BEGIN(out_loc); }
@ -605,8 +605,8 @@ import[ \t\r\n]+\"(DPI|DPI-C)\"[ \t\r\n]+function[ \t\r\n]+ {
[-+]?[=*]> {
if (!mode->specify) REJECT;
auto val = new std::string(YYText());
return parser::make_TOK_SPECIFY_OPER(val, out_loc);
auto val = std::make_unique<std::string>(YYText());
return parser::make_TOK_SPECIFY_OPER(std::move(val), out_loc);
}
"&&&" {

View file

@ -244,7 +244,7 @@
node->children.push_back(std::move(rangeNode));
}
static void checkLabelsMatch(const frontend_verilog_yy::parser::location_type& loc, const char *element, const std::string *before, const std::string *after)
static void checkLabelsMatch(const frontend_verilog_yy::parser::location_type& loc, const char *element, const std::string* before, const std::string *after)
{
if (!before && after)
err_at_loc(loc, "%s missing where end label (%s) was given.",
@ -271,7 +271,6 @@
node->is_custom_type = true;
node->children.push_back(std::make_unique<AstNode>(AST_WIRETYPE));
node->children.back()->str = *name;
delete name;
}
void ParseState::addTypedefNode(std::string *name, std::unique_ptr<AstNode> node)
@ -287,7 +286,6 @@
auto qname = current_ast_mod->str + "::" + (*name).substr(1);
pkg_user_types[qname] = tnode;
}
delete name;
}
void ParseState::enterTypeScope()
@ -479,7 +477,7 @@
specify_triple fall;
};
using string_t = std::string *;
using string_t = std::unique_ptr<std::string>;
using ast_t = std::unique_ptr<YOSYS_NAMESPACE_PREFIX AST::AstNode>;
using al_t = YOSYS_NAMESPACE_PREFIX dict<YOSYS_NAMESPACE_PREFIX RTLIL::IdString, std::unique_ptr<YOSYS_NAMESPACE_PREFIX AST::AstNode>>*;
using specify_target_ptr_t = std::unique_ptr<struct specify_target>;
@ -686,27 +684,27 @@ attr_assign:
hierarchical_id:
TOK_ID {
$$ = $1;
$$ = std::move($1);
} |
hierarchical_id TOK_PACKAGESEP TOK_ID {
if ($3->compare(0, 1, "\\") == 0)
*$1 += "::" + $3->substr(1);
else
*$1 += "::" + *$3;
$$ = $1;
$$ = std::move($1);
} |
hierarchical_id TOK_DOT TOK_ID {
if ($3->compare(0, 1, "\\") == 0)
*$1 += "." + $3->substr(1);
else
*$1 += "." + *$3;
$$ = $1;
$$ = std::move($1);
};
hierarchical_type_id:
TOK_USER_TYPE
| TOK_PKG_USER_TYPE // package qualified type name
| TOK_LPAREN TOK_USER_TYPE TOK_RPAREN { $$ = $2; } // non-standard grammar
TOK_USER_TYPE {$$ = std::move($1); }
| TOK_PKG_USER_TYPE {$$ = std::move($1); } // package qualified type name
| TOK_LPAREN TOK_USER_TYPE TOK_RPAREN { $$ = std::move($2); } // non-standard grammar
;
module:
@ -727,7 +725,7 @@ module:
SET_AST_NODE_LOC(extra->ast_stack.back(), @2, @$);
extra->ast_stack.pop_back();
log_assert(extra->ast_stack.size() == 1);
checkLabelsMatch(@11, "Module name", $4, $11);
checkLabelsMatch(@11, "Module name", $4.get(), $11.get());
extra->current_ast_mod = nullptr;
extra->exitTypeScope();
};
@ -835,7 +833,7 @@ package:
append_attr(mod, $1);
} TOK_SEMICOL package_body TOK_ENDPACKAGE opt_label {
extra->ast_stack.pop_back();
checkLabelsMatch(@9, "Package name", $4, $9);
checkLabelsMatch(@9, "Package name", $4.get(), $9.get());
extra->current_ast_mod = nullptr;
extra->exitTypeScope();
};
@ -1043,7 +1041,7 @@ logic_type:
extra->astbuf3->is_signed = true;
} |
hierarchical_type_id {
extra->addWiretypeNode($1, extra->astbuf3.get());
extra->addWiretypeNode($1.get(), extra->astbuf3.get());
};
integer_atom_type:
@ -1311,7 +1309,7 @@ specify_item:
auto en_expr = std::move($1);
char specify_edge = $3;
auto src_expr = std::move($4);
string *oper = $5;
string *oper = $5.get();
specify_target_ptr_t target = std::move($6);
specify_rise_fall_ptr_t timing = std::move($9);
@ -1387,8 +1385,6 @@ specify_item:
cell->children.push_back(std::make_unique<AstNode>(AST_ARGUMENT, std::move(target->dat)));
cell->children.back()->str = "\\DAT";
}
delete oper;
} |
TOK_ID TOK_LPAREN specify_edge expr specify_condition TOK_COMMA specify_edge expr specify_condition TOK_COMMA specify_triple specify_opt_triple TOK_RPAREN TOK_SEMICOL {
if (*$1 != "$setup" && *$1 != "$hold" && *$1 != "$setuphold" && *$1 != "$removal" && *$1 != "$recovery" &&
@ -1725,7 +1721,7 @@ param_implicit_type: param_signed param_range;
param_type:
param_integer_type | param_real | param_range_type | param_implicit_type |
hierarchical_type_id {
extra->addWiretypeNode($1, extra->astbuf1.get());
extra->addWiretypeNode($1.get(), extra->astbuf1.get());
};
param_decl:
@ -1982,7 +1978,7 @@ member_type_token:
member_type: type_atom type_signing
| type_vec type_signing
| hierarchical_type_id { extra->addWiretypeNode($1, extra->astbuf1.get()); }
| hierarchical_type_id { extra->addWiretypeNode($1.get(), extra->astbuf1.get()); }
;
struct_var_list: struct_var
@ -2161,8 +2157,8 @@ assign_expr:
SET_AST_NODE_LOC(node, @$, @$);
};
type_name: TOK_ID // first time seen
| TOK_USER_TYPE { if (extra->isInLocalScope($1)) lexer->err("Duplicate declaration of TYPEDEF '%s'", $1->c_str()+1); $$ = std::move($1); }
type_name: TOK_ID { $$ = std::move($1); } // first time seen
| TOK_USER_TYPE { if (extra->isInLocalScope($1.get())) lexer->err("Duplicate declaration of TYPEDEF '%s'", $1->c_str()+1); $$ = std::move($1); }
;
typedef_decl:
@ -2180,15 +2176,15 @@ typedef_decl:
}
rewriteAsMemoryNode(extra->astbuf1.get(), std::move($5));
}
extra->addTypedefNode($4, std::move(extra->astbuf1)); }
| TOK_TYPEDEF enum_struct_type type_name TOK_SEMICOL { extra->addTypedefNode($3, std::move($2)); }
extra->addTypedefNode($4.get(), std::move(extra->astbuf1)); }
| TOK_TYPEDEF enum_struct_type type_name TOK_SEMICOL { extra->addTypedefNode($3.get(), std::move($2)); }
;
typedef_base_type:
hierarchical_type_id {
$$ = std::make_unique<AstNode>(AST_WIRE);
$$->is_logic = true;
extra->addWiretypeNode($1, $$.get());
extra->addWiretypeNode($1.get(), $$.get());
} |
integer_vector_type opt_signedness_default_unsigned {
$$ = std::make_unique<AstNode>(AST_WIRE);
@ -2231,10 +2227,10 @@ cell_stmt:
tok_prim_wrapper:
TOK_PRIMITIVE {
$$ = $1;
$$ = std::move($1);
} |
TOK_OR {
$$ = new std::string("or");
$$ = std::make_unique<std::string>("or");
};
cell_list:
@ -2474,7 +2470,7 @@ always_event:
opt_label:
TOK_COL TOK_ID {
$$ = $2;
$$ = std::move($2);
} |
%empty {
$$ = nullptr;
@ -2482,7 +2478,7 @@ opt_label:
opt_sva_label:
TOK_SVA_LABEL TOK_COL {
$$ = $1;
$$ = std::move($1);
} |
%empty {
$$ = nullptr;
@ -2790,7 +2786,7 @@ behavioral_stmt:
node->str = *$4;
} behavioral_stmt_list TOK_END opt_label {
extra->exitTypeScope();
checkLabelsMatch(@8, "Begin label", $4, $8);
checkLabelsMatch(@8, "Begin label", $4.get(), $8.get());
AstNode *node = extra->ast_stack.back();
// In SystemVerilog, unnamed blocks with block item declarations
// create an implicit hierarchy scope
@ -3173,7 +3169,7 @@ gen_block:
node->str = $3 ? *$3 : std::string();
} module_gen_body TOK_END opt_label {
extra->exitTypeScope();
checkLabelsMatch(@7, "Begin label", $3, $7);
checkLabelsMatch(@7, "Begin label", $3.get(), $7.get());
SET_AST_NODE_LOC(extra->ast_stack.back(), @1, @7);
extra->ast_stack.pop_back();
};
@ -3519,8 +3515,8 @@ concat_list:
};
integral_number:
TOK_CONSTVAL { $$ = $1; } |
TOK_UNBASED_UNSIZED_CONSTVAL { $$ = $1; } |
TOK_CONSTVAL { $$ = std::move($1); } |
TOK_UNBASED_UNSIZED_CONSTVAL { $$ = std::move($1); } |
TOK_BASE TOK_BASED_CONSTVAL {
$1->append(*$2);
$$ = std::move($1);