systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
FindTemplateTypes.cpp
Go to the documentation of this file.
1#include "FindTemplateTypes.h"
2
4#include "llvm/Support/raw_ostream.h"
5
6using namespace systemc_clang;
7using namespace sc_ast_matchers;
8
10// TemplateType
12TemplateType::TemplateType() : type_name_{""}, type_ptr_{nullptr} {}
13
14TemplateType::TemplateType(std::string name, const Type *t)
15 : type_name_{name}, type_ptr_{t} {}
16
21
23
24std::string TemplateType::getTypeName() const { return type_name_; }
25
26std::string TemplateType::toString() const { return getTypeName(); }
27
28const Type *TemplateType::getTypePtr() const { return type_ptr_; }
29
31// FindTemplateTypes
33
37
38// Constructor
40
45
49
50// Destructor
52
53void FindTemplateTypes::Enumerate(const Type *type) {
54 if (!type) {
55 return;
56 }
57
58 TraverseType(QualType(type->getUnqualifiedDesugaredType(), 1));
59}
60
62 dre->dump();
63
64 if (FunctionDecl *fd = dyn_cast<FunctionDecl>(dre->getDecl())) {
65 //llvm::outs() << " ### FUNCTION DECL\n";
66 return true;
67 }
68
69 std::string template_parm{dre->getNameInfo().getAsString()};
70
71 auto new_node{template_args_.addNode(
72 TemplateType{template_parm, dre->getType().getTypePtr()})};
73 template_args_.addEdge(current_type_node_, new_node);
74
75 return true;
76}
77
79 TemplateSpecializationType *special_type) {
80
86 if (special_type->isSugared()) {
87 auto ut{special_type->desugar().getTypePtr()};
88 TraverseType(special_type->desugar());
90 return false;
91 }
92
93 auto template_name{special_type->getTemplateName()};
94
95 clang::LangOptions LangOpts;
96 LangOpts.CPlusPlus = true;
97 clang::PrintingPolicy Policy(LangOpts);
98 //Policy.SuppressTagKeyword = true;
99 Policy.adjustForCPlusPlus();
100
101 std::string name_string;
102 llvm::raw_string_ostream sstream(name_string);
103 template_name.print(sstream, Policy);
104
105 auto new_node{
106 template_args_.addNode(TemplateType{sstream.str(), special_type})};
107
108 current_type_node_ = new_node;
109
110 if (template_args_.size() == 1) {
112 }
113
114 if (stack_current_node_.size() > 0) {
116 }
117
119
120 return true;
121}
122
123bool FindTemplateTypes::VisitCXXRecordDecl(CXXRecordDecl *cxx_record) {
124 if (cxx_record != nullptr) {
125 IdentifierInfo *info{cxx_record->getIdentifier()};
126 if (info != nullptr) {
127 // llvm::outs() << " ==> CXXRecord type: " << info->getNameStart() <<
128 // "\n";
129 // template_types_.push_back(
130 // TemplateType(info->getNameStart(), cxx_record->getTypeForDecl()));
131 }
132 }
133
134 return true;
135}
136
137bool FindTemplateTypes::VisitBuiltinType(BuiltinType *bi_type) {
138 //llvm::outs() << "=VisitBuiltinType= \n";
139 // bi_type->dump();
140
141 clang::LangOptions LangOpts;
142 LangOpts.CPlusPlus = true;
143 //LangOpts.Bool = 1;
144 clang::PrintingPolicy Policy(LangOpts);
145 Policy.adjustForCPlusPlus();
146
147 // auto type_name{bi_type->getNameAsCString(Policy)};
148 auto type_name{bi_type->getName(Policy)};
149 //llvm::outs() << "type is : " << type_name << "\n";
150
151 TemplateType tt{type_name.str(), bi_type};
153 // template_types_.push_back(TemplateType(type_name, bi_type));
154
155 if (template_args_.size() == 1) {
157 }
158
159 return false;
160}
161
163 LLVM_DEBUG(
164 llvm::dbgs() << "=VisitEnumType=\n";
165 e->dump();
166 );
167
168 auto type_name{e->getDecl()->getName()};
169 llvm::dbgs() << "type_name: " << type_name << "\n";
170 TemplateType tt{type_name.str(), e->desugar().getTypePtr()};
171
172 e->desugar().getTypePtr()->dump();
173 auto tp{e->desugar().getTypePtr()};
174
176
177 if (template_args_.size() == 1) {
179 }
180
181 return false;
182}
183
184bool FindTemplateTypes::VisitDependentNameType(DependentNameType *type) {
185 TemplateType tt{type->desugar().getAsString(), type};
187 if (template_args_.size() == 1) {
189 }
190
191 return true;
192}
193bool FindTemplateTypes::VisitTypedefType(TypedefType *typedef_type) {
194 LLVM_DEBUG(llvm::dbgs() << "=VisitTypedefType=\n";
195 typedef_type->dump(););
196 // child nodes of TemplateSpecializationType are not being invoked.
197 if (auto special_type = typedef_type->getAs<TemplateSpecializationType>()) {
198 TraverseType(QualType(special_type->getUnqualifiedDesugaredType(), 0));
199 }
200 return true;
201}
202
204 auto type_decl{rt->getDecl()};
205 auto type_name{type_decl->getName().str()};
206
207 current_type_node_ = template_args_.addNode(TemplateType(type_name, rt));
208
209 if (template_args_.size() == 1) {
211 }
212
213 if (stack_current_node_.size() > 0) {
215 }
216
218 // template_args_.dump();
219
220 if (auto ctsd = dyn_cast<ClassTemplateSpecializationDecl>(type_decl)) {
221 // llvm::outs() << " ==> CTSD : " << ctsd->getTemplateArgs().size() << "\n";
222
223 const TemplateArgumentList &arg_list{ctsd->getTemplateArgs()};
224 for (unsigned int i{0}; i < arg_list.size(); ++i) {
225 const TemplateArgument &targ{arg_list[i]};
226 // llvm::outs() << " ====> template argument: ";
227 targ.dump();
228 // llvm::outs() << "\n";
229 // TODO Write this into the vector.
230 // llvm::outs() << " ====> template type : " << targ.getKind() << "\n";
231 //
232 if (targ.getKind() == TemplateArgument::ArgKind::Type) {
233 QualType template_name{targ.getAsType()};
234 const Type *arg_type{targ.getAsType().getTypePtr()};
235
236 if (!arg_type->isBuiltinType()) {
238 TraverseType(QualType(
239 arg_list[i].getAsType()->getUnqualifiedDesugaredType(), 1));
242 } else {
243 auto new_node{template_args_.addNode(
244 TemplateType{template_name.getAsString(), arg_type})};
245 template_args_.addEdge(current_type_node_, new_node);
246 }
247 } else if (targ.getKind() == TemplateArgument::ArgKind::Integral) {
248 QualType template_name{targ.getNonTypeTemplateArgumentType()};
249 // template_name.dump();
250
251 // Special case:
252 // Ignore when there are implicit arguments.
253 //
254 // sc_core::sc_writer_policy
255 //
256 // llvm::outs() << " ====> Integral : ";
257 //
258
259 if (template_name.getAsString() != "enum sc_core::sc_writer_policy") {
260 auto integral{targ.getAsIntegral()};
261 SmallString<16> integral_string{};
262 integral.toString(integral_string);
263 // llvm::outs() << integral_string << "\n";
264
265 auto new_node{template_args_.addNode(TemplateType{
266 integral_string.c_str(), template_name.getTypePtr()})};
267 template_args_.addEdge(current_type_node_, new_node);
268 }
269 }
270 }
271 }
272 // llvm::outs() << ">>> stack size: " << stack_current_node_.size() << "\n";
275
276 // llvm::outs() << "=END VisitRecordType=\n";
277 return true;
278}
279
281 // llvm::outs() << "\n=VisitIntegerLiteral: \n";
282 //<< l->getValue().toString(10,true) <<
283 //"\n"; _os << "== type ptr: " << l->getType().getTypePtr() << "\n"; _os <<
284 //"== type name: " << l->getType().getAsString() << "\n";
285 //
286 return false;
287}
288
292
294 std::string str{};
295
296 auto const args{getTemplateArgTreePtr()};
297
298 for (auto const &node : *args) {
299 // Returns a TreeNodePtr
300 auto type_data{node->getDataPtr()};
301 auto parent_node{node->getParent()};
302 auto parent_data{parent_node->getDataPtr()};
303 if (parent_node->getDataPtr() == node->getDataPtr()) {
304 // llvm::outs() << "\nInsert parent node: " << type_data->getTypeName()
305 // << "\n";
306 //tree_j[type_data->getTypeName()] = nullptr;
307 str += "type_name: " + type_data->getTypeName() + ": nullptr \n";
308 } else {
309 // FIXME: This does not print the tree properly.
310 // There does not seem to be a simple way to access the appropriate
311 // location for the insertion of the new values in this JSON.
312 // TODO: Perhaps the way to do this is to construct a string that JSON can
313 // use.
314 //tree_j[parent_data->getTypeName()].push_back(type_data->getTypeName());
315 str += "parent_type_name: " + parent_data->getTypeName() + ": " + type_data->getTypeName();
316 }
317 }
318 str += "\n";
319 LLVM_DEBUG(llvm::dbgs() << str; );
320 return str;
321}
322
323void FindTemplateTypes::printTemplateArguments(llvm::raw_ostream &os) {
324 auto root_node{template_args_.getRoot()};
325 auto s{template_args_.dft(root_node)};
326 os << "> Template args (DFT): " << s << "\n";
327}
bool VisitDeclRefExpr(clang::DeclRefExpr *dre)
bool VisitBuiltinType(clang::BuiltinType *bi_type)
Tree< TemplateType >::TreeNodePtr current_type_node_
bool VisitEnumType(clang::EnumType *e)
void printTemplateArguments(llvm::raw_ostream &os)
bool VisitIntegerLiteral(clang::IntegerLiteral *l)
std::stack< Tree< TemplateType >::TreeNodePtr > stack_current_node_
Tree< TemplateType > * getTemplateArgTreePtr()
bool VisitTemplateSpecializationType(clang::TemplateSpecializationType *special_type)
bool VisitDependentNameType(clang::DependentNameType *type)
bool VisitTypedefType(clang::TypedefType *typedef_type)
bool VisitCXXRecordDecl(clang::CXXRecordDecl *cxx_type)
std::string asString()
Returns the TemplateType data as a std::string.
bool VisitRecordType(clang::RecordType *rt)
void Enumerate(const clang::Type *type)
const clang::Type * getTypePtr() const
const clang::Type * type_ptr_
const TreeNodePtr getRoot() const
Definition Tree.h:121