Skip to content

Commit

Permalink
Templates should handle integral values too.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrf committed Dec 23, 2019
1 parent 589f136 commit cbf1cb5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
49 changes: 41 additions & 8 deletions src/FindTemplateParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ using namespace scpar;

FindTemplateParameters::FindTemplateParameters(CXXRecordDecl *declaration,
llvm::raw_ostream &os)
: declaration_{declaration}, os_{os}, template_parameters_{nullptr}, template_args_{nullptr} {
: declaration_{declaration},
os_{os},
template_parameters_{nullptr},
template_args_{nullptr} {
if (declaration->hasDefinition() == true) {
TraverseDecl(declaration);
}
Expand All @@ -25,13 +28,26 @@ bool FindTemplateParameters::VisitCXXRecordDecl(CXXRecordDecl *declaration) {

if (const auto tdecl =
dyn_cast<ClassTemplateSpecializationDecl>(declaration)) {
//os_ << "@@ template specialization args: " << module_name << "\n";
//tdecl->dump();
template_args_ = &tdecl->getTemplateArgs();
os_ << "@@ template specialization args: " << module_name
<< ", #args: " << template_args_->size() << "\n";
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";
// Check the kind of the argument.
switch (template_args_->get(i).getKind()) {
case TemplateArgument::ArgKind::Integral: {
auto q{template_args_->get(i).getAsIntegral()};
// auto name{q.getAsString()};
os_ << "@@ Integral: " << q << "\n";
}; break;
case TemplateArgument::ArgKind::Type: {
auto q{template_args_->get(i).getAsType()};
auto name{q.getAsString()};
os_ << "@@ arg: " << name << "\n";
}; break;
default: {
}
};
}
}

Expand Down Expand Up @@ -69,8 +85,25 @@ vector<string> FindTemplateParameters::getTemplateArgs() const {
}

for (const auto &arg : template_args_->asArray()) {
arg_list.push_back(arg.getAsType().getAsString());
os_ << "Arg: " << arg.getAsType().getAsString() << "\n";
std::string name{};
switch (arg.getKind()) {
case TemplateArgument::ArgKind::Integral: {
// Return an LLVM APSInt type.
auto number{arg.getAsIntegral()};
SmallString<10> small_str;
number.toString(small_str);
arg_list.push_back(string(small_str.c_str()));
os_ << "Arg: " << small_str << "\n";
}; break;
case TemplateArgument::ArgKind::Type: {
auto q{arg.getAsType()};
name = q.getAsString();
arg_list.push_back(name);
os_ << "Arg: " << name << "\n";
}; break;
default: {
}
};
}
return arg_list;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ set( UNIT_TEST_LIST
# t1
# t2
# t3
# sreg-test
sreg-test
# t4-matchers
t5-template-matching
# subtree-matcher-test
Expand Down
1 change: 1 addition & 0 deletions tests/sreg-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ TEST_CASE("sreg example",
SystemCConsumer sc{from_ast};
sc.HandleTranslationUnit(from_ast->getASTContext());
auto model{sc.getSystemCModel()};
// These are instances.
auto module_decl{model->getModuleDecl()};

cout << "\n";
Expand Down

0 comments on commit cbf1cb5

Please sign in to comment.