From fc80ace6e4b27386ff961fe900556bf44c606e46 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Sat, 16 Oct 2021 19:18:06 +0100 Subject: [PATCH] Reduce size of astgen generated implementation boilerplate. Rely on C++ templates instead for simplicity. No functional change. --- src/V3Ast.h | 24 +++++++++++++++--------- src/astgen | 49 ++++++++++--------------------------------------- 2 files changed, 25 insertions(+), 48 deletions(-) diff --git a/src/V3Ast.h b/src/V3Ast.h index 56e93bb4d..9d7abd681 100644 --- a/src/V3Ast.h +++ b/src/V3Ast.h @@ -1838,16 +1838,22 @@ protected: private: void iterateListBackwards(AstNVisitor& v); - // CONVERSION + // For internal use only. + template inline static bool privateTypeTest(const AstNode* nodep); + public: - // These for use by VN_IS macro only - template static bool privateIs(const AstNode* nodep); - - // These for use by VN_CAST macro only - template static T* privateCast(AstNode* nodep); - - // These for use by VN_CAST_CONST macro only - template static const T* privateConstCast(const AstNode* nodep); + // For use via the VN_IS macro only + template inline static bool privateIs(const AstNode* nodep) { + return nodep && privateTypeTest(nodep); + } + // For use via the VN_CAST macro only + template inline static T* privateCast(AstNode* nodep) { + return privateIs(nodep) ? reinterpret_cast(nodep) : nullptr; + } + // For use via the VN_CAST_CONST macro only + template inline static const T* privateConstCast(const AstNode* nodep) { + return privateIs(nodep) ? reinterpret_cast(nodep) : nullptr; + } }; // Specialisations of privateIs/privateCast diff --git a/src/astgen b/src/astgen index a2cf43088..6328e4adc 100755 --- a/src/astgen +++ b/src/astgen @@ -516,52 +516,23 @@ def write_visitor(filename): def write_impl(filename): with open_file(filename) as fh: fh.write("\n") - fh.write(" // These for use by VN_IS only\n") + fh.write("// For internal use. They assume argument is not nullptr.\n") for typen in sorted(Classes.keys()): - fh.write("template<> inline bool AstNode::privateIs(const AstNode* nodep) { ") + fh.write("template<> inline bool AstNode::privateTypeTest(const AstNode* nodep) { ") if typen == "Node": - fh.write("return nodep != NULL; ") + fh.write("return true; ") else: - fh.write("return nodep && ") + fh.write("return ") if re.search(r'^Node', typen): fh.write( - "(static_cast(nodep->type()) >= static_cast(AstType::first" - + typen + ")) && ") + "static_cast(nodep->type()) >= static_cast(AstType::first" + + typen + ") && ") fh.write( - "(static_cast(nodep->type()) <= static_cast(AstType::last" - + typen + ")); ") + "static_cast(nodep->type()) <= static_cast(AstType::last" + + typen + "); ") else: - fh.write( - "(static_cast(nodep->type()) == static_cast(AstType::at" - + typen + ")); ") - fh.write("}\n") - - fh.write(" // These for use by VN_CAST macro only\n") - for typen in sorted(Classes.keys()): - fh.write("template<> inline Ast" + typen + - "* AstNode::privateCast(AstNode* nodep) { ") - if typen == "Node": - fh.write("return nodep; ") - else: - fh.write("return AstNode::privateIs(nodep) ? ") - fh.write("reinterpret_cast(nodep) : NULL; ") - fh.write("}\n") - - fh.write(" // These for use by VN_CAST_CONST macro only\n") - for typen in sorted(Classes.keys()): - fh.write("template<> inline const Ast" + typen + - "* AstNode::privateConstCast(const AstNode* nodep) { ") - if typen == "Node": - fh.write("return nodep; ") - else: - fh.write("return AstNode::privateIs(nodep) ? ") - fh.write("reinterpret_cast(nodep) : NULL; ") + fh.write("nodep->type() == AstType::at" + typen + "; ") fh.write("}\n")