Skip to content

Commit

Permalink
Disable AST matching inclusion into systemc-clang
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrf committed Dec 10, 2019
1 parent 1349949 commit 8b8c82d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 117 deletions.
46 changes: 0 additions & 46 deletions release-it.json

This file was deleted.

115 changes: 62 additions & 53 deletions src/Matchers.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
#ifndef _MATCHERS_HPP_
#define _MATCHERS_HPP_
#define _MATCHERS_HPP_
#include <map>
#include <tuple>
#include <vector>
#include <map>
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "FindTemplateTypes.h"
#include "ModuleDecl.h"
#include "PortDecl.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"

using namespace clang;
using namespace clang::ast_matchers;
using namespace scpar;

namespace sc_ast_matchers {

// InstanceMatcher
class InstanceMatcher : public MatchFinder::MatchCallback {
public:
void registerMatchers(MatchFinder &finder) {
//clang-format off
// InstanceMatcher
class InstanceMatcher : public MatchFinder::MatchCallback {
public:
typedef std::vector<std::tuple<std::string, FieldDecl *> > instance_fields;
typedef std::vector<std::tuple<std::string, VarDecl *> > instance_vars;

public:
void registerMatchers(MatchFinder &finder) {
/* clang-format off */
auto match_instances =
fieldDecl(
hasType(
Expand All @@ -32,32 +37,38 @@ namespace sc_ast_matchers {
cxxRecordDecl(isDerivedFrom(hasName("::sc_core::sc_module")))
)
).bind("instances_in_vardecl");
//clang-format on

finder.addMatcher(match_instances, this);
finder.addMatcher(match_instances_vars, this);
}

virtual void run(const MatchFinder::MatchResult &result) {
/*
if (auto field_decl = const_cast<FieldDecl *>(
result.Nodes.getNodeAs<FieldDecl>("sc_in"))) {
std::string name{field_decl->getIdentifier()->getNameStart()};
input_port_names.push_back(name);
}
*/
cout << " Found an instance: "<< endl;
}
/* clang-format on */

public:
std::vector<std::string> input_port_names;
};
finder.addMatcher(match_instances, this);
finder.addMatcher(match_instances_vars, this);
}

virtual void run(const MatchFinder::MatchResult &result) {
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;
list_instance_fields_.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;
list_instance_vars_.push_back(std::make_tuple(name, instance));
}
}

private:
instance_fields list_instance_fields_;
instance_vars list_instance_vars_;
};

// FieldMatcher class
class FieldMatcher : public MatchFinder::MatchCallback {
public:
void registerMatchers(MatchFinder &finder) {
/* clang-format off */
auto match_module_decls = cxxRecordDecl(
// isExpansionInMainFile(),
isDerivedFrom(hasName("::sc_core::sc_module")),
Expand All @@ -66,7 +77,7 @@ class FieldMatcher : public MatchFinder::MatchCallback {
auto match_in_ports = cxxRecordDecl(forEach(
fieldDecl(hasType(cxxRecordDecl(hasName("sc_in")))).bind("sc_in")));

//clang-format on
/* clang-format off */

finder.addMatcher(match_in_ports, this);
}
Expand All @@ -85,33 +96,31 @@ class FieldMatcher : public MatchFinder::MatchCallback {
std::vector<std::string> input_port_names;
};



class ModuleDeclarationMatcher : public MatchFinder::MatchCallback {
//
public:
typedef std::vector< std::tuple<std::string, CXXRecordDecl*> > ModuleDeclarationTuple;
typedef std::vector< std::tuple<std::string, PortDecl*> > PortType;

public:
void registerMatchers( MatchFinder &finder );
virtual void run( const MatchFinder::MatchResult &result );
const ModuleDeclarationTuple& getFoundModuleDeclarations() const;
const PortType& getFields( const std::string & port_type );

void dump();
private:
ModuleDeclarationTuple found_declarations_;
PortType clock_ports_;
PortType in_ports_;
PortType out_ports_;
PortType inout_ports_;
PortType other_fields_;
PortType signal_fields_;
//
public:
typedef std::vector<std::tuple<std::string, CXXRecordDecl *> >
ModuleDeclarationTuple;
typedef std::vector<std::tuple<std::string, PortDecl *> > PortType;

public:
void registerMatchers(MatchFinder &finder);
virtual void run(const MatchFinder::MatchResult &result);
const ModuleDeclarationTuple &getFoundModuleDeclarations() const;
const PortType &getFields(const std::string &port_type);

void dump();

private:
ModuleDeclarationTuple found_declarations_;
PortType clock_ports_;
PortType in_ports_;
PortType out_ports_;
PortType inout_ports_;
PortType other_fields_;
PortType signal_fields_;
};

}; // namespace sc_ast_matchers
}; // namespace sc_ast_matchers

#endif

60 changes: 48 additions & 12 deletions src/ModuleDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,32 @@ using namespace scpar;
using namespace std;

ModuleDecl::ModuleDecl()
: module_name_{"NONE"}, class_decl_{nullptr}, constructor_stmt_{nullptr} {}
: module_name_{"NONE"},
instance_name_{"NONE"},
class_decl_{nullptr},
constructor_stmt_{nullptr},
instance_field_decl_{nullptr},
instance_var_decl_{nullptr} {}

ModuleDecl::ModuleDecl(const string &name, CXXRecordDecl *decl)
: module_name_{name}, class_decl_{decl} {}
: module_name_{name},
instance_name_{"NONE"},
class_decl_{decl},
instance_field_decl_{nullptr},
instance_var_decl_{nullptr} {}

ModuleDecl::ModuleDecl(
const std::tuple<const std::string &, CXXRecordDecl *> &element)
: module_name_{get<0>(element)}, class_decl_{get<1>(element)} {}

ModuleDecl::ModuleDecl(const ModuleDecl &from) {
module_name_ = from.module_name_;
instance_name_ = from.instance_name_;

class_decl_ = from.class_decl_;
constructor_stmt_ = from.constructor_stmt_;
instance_field_decl_ = from.instance_field_decl_;
instance_var_decl_ = from.instance_var_decl_;

process_map_ = from.process_map_;
in_ports_ = from.in_ports_;
Expand All @@ -37,17 +50,21 @@ ModuleDecl::ModuleDecl(const ModuleDecl &from) {
signals_ = from.signals_;

instance_list_ = from.instance_list_;
_portSignalMap = from._portSignalMap;
_vef = from._vef;
port_signal_map_ = from.port_signal_map_;
vef_ = from.vef_;

// Class template parameters.
template_parameters_ = from.template_parameters_;
}

ModuleDecl &ModuleDecl::operator=(const ModuleDecl &from) {
module_name_ = from.module_name_;
instance_name_ = from.instance_name_;

class_decl_ = from.class_decl_;
constructor_stmt_ = from.constructor_stmt_;
instance_field_decl_ = from.instance_field_decl_;
instance_var_decl_ = from.instance_var_decl_;

process_map_ = from.process_map_;
in_ports_ = from.in_ports_;
Expand All @@ -64,8 +81,8 @@ ModuleDecl &ModuleDecl::operator=(const ModuleDecl &from) {
signals_ = from.signals_;

instance_list_ = from.instance_list_;
_portSignalMap = from._portSignalMap;
_vef = from._vef;
port_signal_map_ = from.port_signal_map_;
vef_ = from.vef_;

// Class template parameters.
template_parameters_ = from.template_parameters_;
Expand All @@ -75,6 +92,9 @@ ModuleDecl &ModuleDecl::operator=(const ModuleDecl &from) {
ModuleDecl::~ModuleDecl() {
class_decl_ = nullptr;
constructor_stmt_ = nullptr;
instance_field_decl_ = nullptr;
instance_var_decl_ = nullptr;

// Delete all pointers in ports.
for (auto input_port : in_ports_) {
// It is a tuple
Expand Down Expand Up @@ -119,7 +139,7 @@ void ModuleDecl::addInstances(const vector<string> &instanceList) {
}

void ModuleDecl::addSignalBinding(map<string, string> portSignalMap) {
_portSignalMap.insert(portSignalMap.begin(), portSignalMap.end());
port_signal_map_.insert(portSignalMap.begin(), portSignalMap.end());
}

void ModuleDecl::addSignals(const FindSignals::signalMapType &signal_map) {
Expand Down Expand Up @@ -240,7 +260,7 @@ void ModuleDecl::addConstructor(Stmt *constructor) {
}

void ModuleDecl::addProcess(FindEntryFunctions::entryFunctionVectorType *efv) {
_vef = *efv;
vef_ = *efv;
for (unsigned int i = 0; i < efv->size(); ++i) {
EntryFunctionContainer *ef = (*efv)[i];

Expand Down Expand Up @@ -276,7 +296,7 @@ void ModuleDecl::addProcess(FindEntryFunctions::entryFunctionVectorType *efv) {
vector<string> ModuleDecl::getInstanceList() { return instance_list_; }

vector<EntryFunctionContainer *> ModuleDecl::getEntryFunctionContainer() {
return _vef;
return vef_;
}

int ModuleDecl::getNumInstances() { return instance_list_.size(); }
Expand Down Expand Up @@ -313,7 +333,9 @@ ModuleDecl::interfaceMapType ModuleDecl::getIOInterfaces() {
return iointerfaces_;
}

string ModuleDecl::getName() { return module_name_; }
string ModuleDecl::getName() const { return module_name_; }

string ModuleDecl::getInstanceName() const { return instance_name_; }

bool ModuleDecl::isModuleClassDeclNull() { return (class_decl_ == nullptr); }

Expand All @@ -322,6 +344,20 @@ CXXRecordDecl *ModuleDecl::getModuleClassDecl() {
return class_decl_;
}

FieldDecl *ModuleDecl::getInstanceFieldDecl() { return instance_field_decl_; }
VarDecl *ModuleDecl::getInstanceVarDecl() { return instance_var_decl_; }

bool ModuleDecl::isInstanceFieldDecl() const {
if ((instance_field_decl_ != nullptr) && (instance_var_decl_ == nullptr)) {
return true;
}
if ((instance_field_decl_ == nullptr) && (instance_var_decl_ != nullptr)) {
return false;
}

return false;
}

void ModuleDecl::dumpInstances(raw_ostream &os, int tabn) {
if (instance_list_.empty()) {
os << " none \n";
Expand All @@ -333,12 +369,12 @@ void ModuleDecl::dumpInstances(raw_ostream &os, int tabn) {
}

void ModuleDecl::dumpSignalBinding(raw_ostream &os, int tabn) {
if (_portSignalMap.empty()) {
if (port_signal_map_.empty()) {
os << " none\n";
return;
}

for (auto it : _portSignalMap) {
for (auto it : port_signal_map_) {
os << "\nPort : " << it.first << " bound to signal : " << it.second;
}
}
Expand Down
Loading

0 comments on commit 8b8c82d

Please sign in to comment.