systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
FindTemplateParameters.cpp
Go to the documentation of this file.
2#include "FindTemplateTypes.h"
3
4#include "llvm/Support/Debug.h"
5using namespace systemc_clang;
6using namespace clang;
7
9 : declaration_{declaration},
10 template_parameters_{nullptr},
11 template_args_{nullptr} {
12 if (declaration->hasDefinition() == true) {
13 TraverseDecl(declaration);
14 }
15}
16
17bool FindTemplateParameters::VisitCXXRecordDecl(CXXRecordDecl *declaration) {
18 if (IdentifierInfo *info = declaration_->getIdentifier()) {
19 auto module_name = info->getNameStart();
20 // Check if the class is a templated module class.
21
22 // This will only be called on instances of template specialized classes (so
23 // it seems). This should provide to the actual template arguments. For
24 // example, int.
25
26 if (const auto tdecl =
27 dyn_cast<ClassTemplateSpecializationDecl>(declaration)) {
28 template_args_ = &tdecl->getTemplateArgs();
29 LLVM_DEBUG(llvm::dbgs()
30 << "@@ template specialization args: " << module_name
31 << ", #args: " << template_args_->size() << "\n";);
32 for (size_t i{0}; i < template_args_->size(); ++i) {
33 // Check the kind of the argument.
34 switch (template_args_->get(i).getKind()) {
35 case TemplateArgument::ArgKind::Integral: {
36 auto q{template_args_->get(i).getAsIntegral()};
37 // auto name{q.getAsString()};
38 LLVM_DEBUG(llvm::dbgs() << "@@ Integral: " << q << "\n";);
39 }; break;
40 case TemplateArgument::ArgKind::Type: {
41 auto q{template_args_->get(i).getAsType()};
42 auto name{q.getAsString()};
43 LLVM_DEBUG(llvm::dbgs() << "@@ arg: " << name << "\n";);
44 }; break;
45 default: {
46 }
47 };
48 }
49 }
50
51 // TODO: I'm not sure if this is required since the above should capture
52 // sc_in declarations too. But, I'll have to add a test for it.
53 // This will provide access to the actual template parameters
54 auto template_args{declaration->getDescribedClassTemplate()};
55 if (template_args != nullptr) {
56 LLVM_DEBUG(llvm::dbgs() << "@@ template described class args: "
57 << module_name << "\n";);
58 template_parameters_ = template_args->getTemplateParameters();
59 }
60 }
61
62 return false;
63}
64
65const std::vector<std::string> FindTemplateParameters::getTemplateParameters()
66 const {
67 std::vector<std::string> parm_list;
68 if ((template_parameters_ == nullptr) ||
69 (template_parameters_->size() <= 0)) {
70 return parm_list;
71 }
72
73 for (auto parm : template_parameters_->asArray()) {
74 parm_list.push_back(parm->getName().str());
75 LLVM_DEBUG(llvm::dbgs() << "Parm: " << parm->getName() << "\n";);
76 }
77 return parm_list;
78}
79
80const std::vector<std::string> FindTemplateParameters::getTemplateArgs() const {
81 std::vector<std::string> arg_list;
82 if ((template_args_ == nullptr) || (template_args_->size() == 0)) {
83 return arg_list;
84 }
85
86 for (const auto &arg : template_args_->asArray()) {
87 std::string name{};
88 switch (arg.getKind()) {
89 case TemplateArgument::ArgKind::Integral: {
90 // Return an LLVM APSInt type.
91 auto number{arg.getAsIntegral()};
92 SmallString<10> small_str;
93 number.toString(small_str);
94 arg_list.push_back(std::string(small_str.c_str()));
95 LLVM_DEBUG(llvm::dbgs() << "Arg: " << small_str << "\n";);
96 }; break;
97 case TemplateArgument::ArgKind::Type: {
98 auto q{arg.getAsType()};
99 name = q.getAsString();
100 arg_list.push_back(name);
101 LLVM_DEBUG(llvm::dbgs() << "Arg: " << name << "\n";);
102 }; break;
103 default: {
104 }
105 };
106 }
107 return arg_list;
108}
109
111
virtual bool VisitCXXRecordDecl(clang::CXXRecordDecl *decl)
const std::vector< std::string > getTemplateArgs() const
const clang::TemplateArgumentList * template_args_
clang::TemplateParameterList * template_parameters_
const std::vector< std::string > getTemplateParameters() const
Clang forward declarations.
Definition FindArgument.h:6