ast, read_verilog: ownership in AST, use C++ styles for parser and lexer #5135
+2,516
−2,635
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The purpose of this PR is to clarify resource ownership (responsibility for construction and deletion) in the AST by distinguishing between owning pointers (
std::unique_ptr<AstNode>
) and non-owning pointers (AstNode*
).Core idea
struct AstNode
now holds these members:There are no more
new AstNode
and correspondingdelete
statements present in the codebase.std::unique_ptr<AstNode>
falling out of scope triggers~AstNode
, destroying its children, too. Assigningnullptr
to it behaves the same way. This should eliminateAstNode
allocations being reported as leaking onyosys
termination byvalgrind
.Damage
This implied returning
std::unique_ptr<AstNode>
from parse rules. Prior to this PR, the return data type for parse rules is auto-generated with the bison%union
directive. The auto-generated code handling it relies on each union member type having anT& operator=(T&);
, butstd::unique_ptr<...>
deletes its own for good reason.That's why I refactored the parser and lexer with their
C++
modes causing a greater code change.Somehow, syntax errors now don't quote characters, so you get
ERROR: syntax error, unexpected ;
instead ofERROR: syntax error, unexpected ';'
.Testing
This PR has a high risk of creating new yosys segfaults in edge cases not hit by our test suite. More extensive testing will be needed
TODOs
verilog_parser.tab.hh
, which is a missing Makefile dependency (make has to be rerun a couple times when building) and is probably a downgrade in compilation timesstd::unique_ptr
to avoid accidental copies