From d6190529edfb22930d3d94dcc5694529517eeb2a Mon Sep 17 00:00:00 2001 From: rmrf Date: Sat, 14 Dec 2019 14:08:53 -0500 Subject: [PATCH] Use private template functions for port insertions --- src/Matchers.cpp | 35 +++++++++++++++++------------------ src/Matchers.h | 31 +++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/Matchers.cpp b/src/Matchers.cpp index 637e538d3..e2310605b 100644 --- a/src/Matchers.cpp +++ b/src/Matchers.cpp @@ -109,7 +109,7 @@ auto match_non_sc_types = cxxRecordDecl( // add instance matcher // Not nested. - instance_matcher.registerMatchers( finder ); + instance_matcher.registerMatchers(finder); // finder.addMatcher( match_clock_ports, this ); finder.addMatcher(match_sc_in_clk, this); @@ -118,7 +118,13 @@ auto match_non_sc_types = cxxRecordDecl( finder.addMatcher(match_in_out_ports, this); finder.addMatcher(match_internal_signal, this); finder.addMatcher(match_non_sc_types, this); +} +template +void ModuleDeclarationMatcher::insert_port(PortType &port, T *decl) { + auto name{decl->getIdentifier()->getNameStart()}; + port.push_back( + std::make_tuple(name, new PortDecl(name, decl, parseTemplateType(decl)))); } void ModuleDeclarationMatcher::run(const MatchFinder::MatchResult &result) { @@ -133,7 +139,7 @@ void ModuleDeclarationMatcher::run(const MatchFinder::MatchResult &result) { if (auto decl = const_cast( result.Nodes.getNodeAs("sc_module"))) { cout << " Found sc_module: " << decl->getIdentifier()->getNameStart() - << endl; + << " CXXRecordDecl*: " << decl << endl; std::string name{decl->getIdentifier()->getNameStart()}; // decl->dump(); // @@ -151,48 +157,41 @@ void ModuleDeclarationMatcher::run(const MatchFinder::MatchResult &result) { */ } - if (auto fd = result.Nodes.getNodeAs("sc_in_clk")) { + if (auto fd = checkMatch("sc_in_clk", result)) { std::string port_name{fd->getIdentifier()->getNameStart()}; cout << " Found sc_in_clk: " << port_name << endl; - clock_ports_.push_back(std::make_tuple( - port_name, new PortDecl(port_name, fd, parseTemplateType(fd)))); + + insert_port(clock_ports_, fd); } - if (auto fd = result.Nodes.getNodeAs("sc_in")) { + if (auto fd = checkMatch("sc_in", result)) { auto port_name{fd->getIdentifier()->getNameStart()}; cout << " Found sc_in: " << port_name << endl; - // cout << fd->getParent()->getIdentifier()->getNameStart() << endl; - - in_ports_.push_back(std::make_tuple( - port_name, new PortDecl(port_name, fd, parseTemplateType(fd)))); + insert_port(in_ports_, fd); } if (auto fd = checkMatch("sc_out", result)) { auto port_name{fd->getIdentifier()->getNameStart()}; cout << " Found sc_out: " << port_name << endl; - out_ports_.push_back(std::make_tuple( - port_name, new PortDecl(port_name, fd, parseTemplateType(fd)))); + insert_port(out_ports_, fd); } if (auto fd = checkMatch("sc_inout", result)) { auto port_name{fd->getIdentifier()->getNameStart()}; cout << " Found sc_inout: " << port_name << endl; - inout_ports_.push_back(std::make_tuple( - port_name, new PortDecl(port_name, fd, parseTemplateType(fd)))); + insert_port(inout_ports_, fd); } if (auto fd = checkMatch("sc_signal", result)) { auto signal_name{fd->getIdentifier()->getNameStart()}; cout << " Found sc_signal: " << signal_name << endl; - signal_fields_.push_back(std::make_tuple( - signal_name, new PortDecl(signal_name, fd, parseTemplateType(fd)))); + insert_port(signal_fields_, fd); } if (auto fd = checkMatch("other_fields", result)) { auto field_name{fd->getIdentifier()->getNameStart()}; cout << " Found others fields: " << field_name << endl; - other_fields_.push_back(std::make_tuple( - field_name, new PortDecl(field_name, fd, parseTemplateType(fd)))); + insert_port(other_fields_, fd); } } diff --git a/src/Matchers.h b/src/Matchers.h index 94cb76fd8..1d9042b7c 100644 --- a/src/Matchers.h +++ b/src/Matchers.h @@ -49,6 +49,10 @@ class InstanceMatcher : public MatchFinder::MatchCallback { std::string name{instance->getIdentifier()->getNameStart()}; cout << "Found a member field instance: " << name << endl; list_instance_fields_.push_back(std::make_tuple(name, instance)); + + // Get the pointer to the type declaration. + auto qtype{instance->getType().getTypePtr()}; + qtype->dump(); } if (auto instance = const_cast( @@ -58,18 +62,24 @@ class InstanceMatcher : public MatchFinder::MatchCallback { list_instance_vars_.push_back(std::make_tuple(name, instance)); // const Type * returned - // - /* + // + cout << "Figure out type of vardecl\n"; auto qtype{instance->getType().getTypePtr()}; - if (auto dp = qtype->getAs()) { - auto tn{dp->getTemplateName()}; - auto tunder{tn.getUnderlying()}; - auto name{tunder.getAsTemplateDecl()->getNameAsString()}; - cout << "NAME: " << name << endl; + if (auto dp = qtype->getAs()) { + auto tn{dp->getTemplateName()}; + auto tunder{tn.getUnderlying()}; + auto name{tunder.getAsTemplateDecl()->getNameAsString()}; + cout << "template name: \n"; + tn.dump(); + cout << ", NAME: " << name << endl; + + if (dp->isRecordType() ) { + auto rt{ dp->getAsCXXRecordDecl() }; + cout << "RECORD type: " << rt << "\n"; } + } qtype->dump(); - */ } } @@ -128,6 +138,11 @@ class ModuleDeclarationMatcher : public MatchFinder::MatchCallback { ModuleDeclarationType; typedef std::vector > PortType; + private: + // Template functions. + template + void insert_port( PortType & port, T *decl ); + public: void registerMatchers(MatchFinder &finder); virtual void run(const MatchFinder::MatchResult &result);