Reduce size of astgen generated implementation boilerplate.

Rely on C++ templates instead for simplicity. No functional change.
This commit is contained in:
Geza Lore 2021-10-16 19:18:06 +01:00
parent b8f6b262e4
commit fc80ace6e4
2 changed files with 25 additions and 48 deletions

View File

@ -1838,16 +1838,22 @@ protected:
private:
void iterateListBackwards(AstNVisitor& v);
// CONVERSION
// For internal use only.
template <typename T> inline static bool privateTypeTest(const AstNode* nodep);
public:
// These for use by VN_IS macro only
template <class T> static bool privateIs(const AstNode* nodep);
// These for use by VN_CAST macro only
template <class T> static T* privateCast(AstNode* nodep);
// These for use by VN_CAST_CONST macro only
template <class T> static const T* privateConstCast(const AstNode* nodep);
// For use via the VN_IS macro only
template <typename T> inline static bool privateIs(const AstNode* nodep) {
return nodep && privateTypeTest<T>(nodep);
}
// For use via the VN_CAST macro only
template <typename T> inline static T* privateCast(AstNode* nodep) {
return privateIs<T>(nodep) ? reinterpret_cast<T*>(nodep) : nullptr;
}
// For use via the VN_CAST_CONST macro only
template <typename T> inline static const T* privateConstCast(const AstNode* nodep) {
return privateIs<T>(nodep) ? reinterpret_cast<const T*>(nodep) : nullptr;
}
};
// Specialisations of privateIs/privateCast

View File

@ -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<Ast" + typen +
">(const AstNode* nodep) { ")
fh.write("template<> inline bool AstNode::privateTypeTest<Ast" +
typen + ">(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<int>(nodep->type()) >= static_cast<int>(AstType::first"
+ typen + ")) && ")
"static_cast<int>(nodep->type()) >= static_cast<int>(AstType::first"
+ typen + ") && ")
fh.write(
"(static_cast<int>(nodep->type()) <= static_cast<int>(AstType::last"
+ typen + ")); ")
"static_cast<int>(nodep->type()) <= static_cast<int>(AstType::last"
+ typen + "); ")
else:
fh.write(
"(static_cast<int>(nodep->type()) == static_cast<int>(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<Ast" + typen +
">(AstNode* nodep) { ")
if typen == "Node":
fh.write("return nodep; ")
else:
fh.write("return AstNode::privateIs<Ast" + typen +
">(nodep) ? ")
fh.write("reinterpret_cast<Ast" + typen + "*>(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<Ast" + typen +
">(const AstNode* nodep) { ")
if typen == "Node":
fh.write("return nodep; ")
else:
fh.write("return AstNode::privateIs<Ast" + typen +
">(nodep) ? ")
fh.write("reinterpret_cast<const Ast" + typen +
"*>(nodep) : NULL; ")
fh.write("nodep->type() == AstType::at" + typen + "; ")
fh.write("}\n")