Skip to content

Commit

Permalink
Template arguments are being parsed.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrf committed Dec 19, 2019
1 parent 1256321 commit f9ace8f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 80 deletions.
24 changes: 22 additions & 2 deletions src/FindTemplateParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,30 @@ FindTemplateParameters::FindTemplateParameters(CXXRecordDecl *declaration,
bool FindTemplateParameters::VisitCXXRecordDecl(CXXRecordDecl *declaration) {
if (IdentifierInfo *info = declaration_->getIdentifier()) {
auto module_name = info->getNameStart();

// Check if the class is a templated module class.

// This will only be called on instances of template specialized classes (so
// it seems). This should provide to the actual template arguments. For
// example, int.

if (const auto tdecl =
dyn_cast<ClassTemplateSpecializationDecl>(declaration)) {
os_ << "@@ template specialization args: " << module_name << "\n";
template_args_ = &tdecl->getTemplateArgs();
for ( size_t i{0} ; i < template_args_->size(); ++i) {
auto q{ template_args_->get(i).getAsType() };
auto name { q.getAsString() };
os_ << "@@ size: " << template_args_->size() << ", arg0: " << name << "\n";

}
}

// TODO: I'm not sure if this is required since the above should capture
// sc_in declarations too. But, I'll have to add a test for it.
// This will provide access to the actual template parameters
auto template_args{declaration->getDescribedClassTemplate()};
if (template_args != nullptr) {
os_ << "@@ template described class args: " << module_name << "\n";
template_parameters_ = template_args->getTemplateParameters();
}
}
Expand All @@ -37,7 +57,7 @@ vector<string> FindTemplateParameters::getTemplateParameters() const {

for (auto parm : template_parameters_->asArray()) {
parm_list.push_back(parm->getName());
// os_ << "Parm: " << parm->getName() << "\n";
os_ << "Parm: " << parm->getName() << "\n";
}
return parm_list;
}
Expand Down
1 change: 1 addition & 0 deletions src/FindTemplateParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class FindTemplateParameters : public RecursiveASTVisitor<FindTemplateParameters
CXXRecordDecl *declaration_;
llvm::raw_ostream &os_;
TemplateParameterList *template_parameters_;
const TemplateArgumentList *template_args_;
};
} // namespace scpar
#endif
87 changes: 34 additions & 53 deletions src/Matchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,101 +184,82 @@ void ModuleDeclarationMatcher::run(const MatchFinder::MatchResult &result) {
}
}

const ModuleDeclarationMatcher::PortType &ModuleDeclarationMatcher::getFields(
const std::string &port_type) {
if (port_type == "sc_in") {
return in_ports_;
}
if (port_type == "sc_inout") {
return inout_ports_;
}
if (port_type == "sc_out") {
return out_ports_;
}
if (port_type == "sc_signal") {
return signal_fields_;
}
if (port_type == "sc_in_clk") {
return clock_ports_;
}
if (port_type == "others") {
return other_fields_;
}
// You should never get here.
assert(true);
}

void ModuleDeclarationMatcher::pruneMatches() {
// Must have found instances.
// 1. For every module found, check if there is an instance.
// 2. If there is an instance, then add it into the list.

for (auto const &element : found_declarations_) {
auto decl{get<1>(element)};
//std::cout << "## fd name: " << get<0>(element) << "\n ";
InstanceListType instance_list;
InstanceMatcher::InstanceDeclType instance;
// std::cout << "## fd name: " << get<0>(element) << "\n ";
InstanceListType instance_list;
InstanceMatcher::InstanceDeclType instance;
if (instance_matcher.findInstance(decl, instance)) {
std::cout << "## GOOD MODULE: " << get<0>(element) << std::endl;
pruned_declarations_.push_back(element);
pruned_declarations_map_.insert( ModuleDeclarationPairType( decl, get<0>(element) ));
// pruned_declarations_.push_back(element);
pruned_declarations_map_.insert(
ModuleDeclarationPairType(decl, get<0>(element)));
instance_list.push_back(instance);
declaration_instance_map_.insert( DeclarationInstancePairType(decl, instance_list));
}

declaration_instance_map_.insert(DeclarationInstancePairType(decl, instance_list));
}

for (auto const &element : found_template_declarations_) {
auto decl{get<1>(element)};
//std::cout << "## ftd name: " << get<0>(element) << "\n ";
// std::cout << "## ftd name: " << get<0>(element) << "\n ";
InstanceListType instance_list;
InstanceMatcher::InstanceDeclType instance;
InstanceMatcher::InstanceDeclType instance;
if (instance_matcher.findInstance(decl, instance)) {
std::cout << "## GOOD TEMPLATE MODULE: " << get<0>(element) << std::endl;
pruned_declarations_.push_back(element);
pruned_declarations_map_.insert( ModuleDeclarationPairType( decl, get<0>(element) ));
// pruned_declarations_.push_back(element);
pruned_declarations_map_.insert(
ModuleDeclarationPairType(decl, get<0>(element)));
instance_list.push_back(instance);
declaration_instance_map_.insert(
DeclarationInstancePairType(decl, instance_list));
}
declaration_instance_map_.insert(DeclarationInstancePairType(decl, instance_list));
}
}

void ModuleDeclarationMatcher::dump() {

cout << "## Non-template module declarations: " << found_declarations_.size() << "\n";
for (const auto &i : found_declarations_) {
cout << "module name : " << get<0>(i) << ", " << get<1>(i)
<< std::endl;
}

cout << "## Template module declarations: " << found_template_declarations_.size() << "\n";
for (const auto &i : found_template_declarations_) {
cout << "template module name: " << get<0>(i) << ", " << get<1>(i)
<< std::endl;
}

for (const auto &i : pruned_declarations_) {
cout << "pruned module name: " << get<0>(i) << ", " << get<1>(i)
<< std::endl;
}

cout << "## Pruned declaration Map\n";
// for (const auto &i : pruned_declarations_) {
// cout << "pruned module name: " << get<0>(i) << ", " << get<1>(i)
// << std::endl;
// }
cout << "## Pruned declaration Map: " << pruned_declarations_map_.size() << "\n";
for (const auto &i : pruned_declarations_map_) {
auto decl{ i.first };
auto decl_name{ i.second };
cout << "CXXRecordDecl* " << i.first << ", module name: " << decl_name << "\n";
auto decl{i.first};
auto decl_name{i.second};
cout << "CXXRecordDecl* " << i.first << ", module name: " << decl_name
<< "\n";
}


// Print the instances.
instance_matcher.dump();

cout << "\n## Dump map of decl->instances\n";
cout << "\n## Dump map of decl->instances: " << declaration_instance_map_.size() << "\n";

for (const auto &i : declaration_instance_map_ ) {
auto decl{ i.first };
auto instance_list{ i.second };
for (const auto &i : declaration_instance_map_) {
auto decl{i.first};
auto instance_list{i.second};

cout << "decl: " << decl->getIdentifier()->getNameStart();
for (const auto &instance : instance_list ) {
cout << ", instance type: " << get<0>(instance) << ", " << get<1>(instance) << "\n";
for (const auto &instance : instance_list) {
cout << ", instance type: " << get<0>(instance) << ", "
<< get<1>(instance) << "\n";
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/Matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
});

if (found_it != instances_.end()) {
cout << "FOUND AN FIELD instance: " << get<0>(*found_it) << ", "
<< get<1>(*found_it) << endl;
// cout << "FOUND AN FIELD instance: " << get<0>(*found_it) << ", "
// << get<1>(*found_it) << endl;
// This is an odd way to set tuples. Perhaps replace with a nicer
// interface.
get<0>(instance) = get<0>(*found_it);
Expand Down Expand Up @@ -100,15 +100,15 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
if (auto instance = const_cast<FieldDecl *>(
result.Nodes.getNodeAs<FieldDecl>("instances_in_fielddecl"))) {
std::string name{instance->getIdentifier()->getNameStart()};
cout << "Found a member field instance: " << name << endl;
cout << "@@ Found a member field instance: " << name << endl;

instances_.push_back(std::make_tuple(name, instance));
}

if (auto instance = const_cast<VarDecl *>(
result.Nodes.getNodeAs<VarDecl>("instances_in_vardecl"))) {
std::string name{instance->getIdentifier()->getNameStart()};
cout << "Found a member variable instance: " << name << endl;
cout << "@@ Found a member variable instance: " << name << endl;

if (auto instance_name = const_cast<CXXConstructExpr *>(
result.Nodes.getNodeAs<CXXConstructExpr>("constructor_expr"))) {
Expand All @@ -124,9 +124,12 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
std::string s;
llvm::raw_string_ostream sstream(s);
first_arg->printPretty(sstream, 0, Policy);
cout << "instance name: " << sstream.str() << "\n";
std::string strip_quote_name{sstream.str()};
strip_quote_name.erase(
std::remove(strip_quote_name.begin(), strip_quote_name.end(), '\"'),
strip_quote_name.end());

instances_.push_back(std::make_tuple(sstream.str(), instance));
instances_.push_back(std::make_tuple(strip_quote_name, instance));
}
}
}
Expand Down Expand Up @@ -236,8 +239,6 @@ class ModuleDeclarationMatcher : public MatchFinder::MatchCallback {
return pruned_declarations_map_;
}

const PortType &getFields(const std::string &port_type);

void pruneMatches();
void dump();
};
Expand Down
28 changes: 18 additions & 10 deletions src/SystemCClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ bool SystemCConsumer::fire() {
// Map CXXRecordDecl => std::string
auto found_module_declarations{
module_declaration_handler.getFoundModuleDeclarations()};

os_ << "@@@@@@@@@@@@ FOUND MODUE: " << found_module_declarations.size()
<< "\n";
auto found_top_module{std::find_if(
found_module_declarations.begin(), found_module_declarations.end(),
[this](
Expand Down Expand Up @@ -151,17 +148,28 @@ bool SystemCConsumer::fire() {
os_ << "CXXRecordDecl* " << cxx_decl
<< ", module type: " << found_module_declarations[cxx_decl];
for (const auto &instance : instance_list) {
auto add_module_decl{ new ModuleDecl{ found_module_declarations[cxx_decl], cxx_decl } };
add_module_decl->setInstanceName( get<0>(instance) );
systemcModel_->addModuleDecl( add_module_decl );
auto add_module_decl{
new ModuleDecl{found_module_declarations[cxx_decl], cxx_decl}};


// Insert what you know about the parsed sc_module
// 1. Insert the instance name from Matchers
add_module_decl->setInstanceName(get<0>(instance));

// 2. Find the template arguments for the class.
FindTemplateParameters tparms{cxx_decl, os_};
add_module_decl->setTemplateParameters(tparms.getTemplateParameters());
add_module_decl->dump_json();
//cxx_decl->dump();

os_ << ", instance name: " << get<0>(instance) << ", Decl* "
<< get<1>(instance) << "\n";
add_module_decl->dump_json();

systemcModel_->addModuleDecl(add_module_decl);

//os_ << ", instance name: " << get<0>(instance) << ", Decl* " << get<1>(instance) << "\n";
}
os_ << "\n";

}

////////////////////////////////////////////////////////////////
// Figure out the module map.
////////////////////////////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions tests/data/templated-module.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "systemc.h"

typedef float MY_FLOAT;

SC_MODULE( non_template ) {
int x;

Expand Down Expand Up @@ -44,5 +46,6 @@ int sc_main(int argc, char *argv[]) {

non_template nt("non-templated-module-instance");

test<MY_FLOAT, double> test_double2("testing_double_double");
return 0;
}
9 changes: 2 additions & 7 deletions tests/t5-template-matching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ TEST_CASE("Read SystemC model from file for testing", "[parsing]") {
module_declaration_handler.pruneMatches();
module_declaration_handler.dump();

//match_instances.registerMatchers( matchRegistry );
//match_instances.dump();


/*
ModuleDeclarationMatcher module_declaration_handler{};
Expand All @@ -54,12 +51,10 @@ TEST_CASE("Read SystemC model from file for testing", "[parsing]") {
llvm::outs() << "================ END =============== \n";




SystemCConsumer sc{from_ast};
sc.HandleTranslationUnit(from_ast->getASTContext());
auto model{sc.getSystemCModel()};
auto module_decl{model->getModuleDecl()};
//auto model{sc.getSystemCModel()};
//auto module_decl{model->getModuleDecl()};

SECTION("Test systemc-clang AST matchers ", "[matchers]") {
// The module instances have all the information.
Expand Down

0 comments on commit f9ace8f

Please sign in to comment.