systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
ArrayTypeUtils.cpp
Go to the documentation of this file.
1#include "ArrayTypeUtils.h"
2
3#include "clang/AST/DeclCXX.h"
4#include "clang/AST/ExprCXX.h"
5#include "llvm/Support/Debug.h"
6
7#include <iostream>
8
9namespace sc_ast_matchers {
10namespace utils {
11namespace array_type {
12
14 // clang::InitListExpr *init_expr_list) {
15 clang::CXXCtorInitializer *ctor_init) {
17
18 IndexMapType indices;
19 if (!ctor_init) {
20 return indices;
21 }
22
23 clang::Expr *expr{ctor_init->getInit()->IgnoreImplicit()};
24 if (!expr) {
25 return indices;
26 }
27 // clang::InitListExpr *init_expr_list{
28 // clang::dyn_cast<clang::InitListExpr>(expr)};
29
30 if (auto init_expr_list = clang::dyn_cast<clang::InitListExpr>(expr)) {
32 clang::Expr **iexpr_set{init_expr_list->getInits()};
33
34 for (std::size_t i{0}; i < init_expr_list->getNumInits(); ++i) {
35 LLVM_DEBUG(llvm::dbgs() << "Iterate over init 1d lists: " << i << "\n";);
36 clang::Expr *iexpr{iexpr_set[i]};
37
38 // Level 1
39 // iexpr->dump();
40 if (auto cexpr = clang::dyn_cast<clang::CXXConstructExpr>(iexpr)) {
41 clang::CXXConstructExpr *nested_cexpr{
42 clang::dyn_cast<clang::CXXConstructExpr>(
43 cexpr->getArg(0)->IgnoreImplicit())};
44
45 auto cxxbindexpr{clang::dyn_cast<clang::CXXBindTemporaryExpr>(
46 nested_cexpr->getArg(0)->IgnoreParenImpCasts())};
47
48 auto cxxctor{clang::dyn_cast<clang::CXXConstructExpr>(
49 cxxbindexpr->getSubExpr()->IgnoreParenImpCasts())};
50 clang::StringLiteral *slit{clang::dyn_cast<clang::StringLiteral>(
51 cxxctor->getArg(0)->IgnoreImpCasts())};
52 //
53
54 if (slit) {
55 LLVM_DEBUG(llvm::dbgs()
56 << "Argument 1d: [" << i << ", "
57 << "x, x ] " << slit->getString().str() << " \n";);
58 indices.insert(
59 IndexPairType(slit->getString().str(), std::make_tuple(i, 0, 0)));
60 }
61 }
62
64 if (auto init_expr_list_2d =
65 clang::dyn_cast<clang::InitListExpr>(iexpr)) {
66 clang::Expr **iexpr_2d_set{init_expr_list_2d->getInits()};
67 for (std::size_t j{0}; j < init_expr_list_2d->getNumInits(); ++j) {
68 LLVM_DEBUG(llvm::dbgs()
69 << "Iterate over 2d init lists: " << j << "\n";);
70 clang::Expr *iexpr_2d{iexpr_2d_set[j]};
71
73 // Unwrap CXXConstructExpr
74 clang::CXXConstructExpr *peel{
75 clang::dyn_cast<clang::CXXConstructExpr>(iexpr_2d)};
76 clang::CXXConstructExpr *cexpr{nullptr};
77 while (peel) {
78 cexpr = peel;
79 peel = clang::dyn_cast<clang::CXXConstructExpr>(
80 peel->getArg(0)->IgnoreImplicit());
81 }
82 llvm::dbgs() << "unwrap 2d dump\n";
83 cexpr->dump();
84
85 if (cexpr) { // auto cexpr =
86 // clang::dyn_cast<clang::CXXConstructExpr>(iexpr_2d)) {
87 llvm::dbgs() << "0 arg dump\n";
88 clang::StringLiteral *slit{clang::dyn_cast<clang::StringLiteral>(
89 cexpr->getArg(0)->IgnoreImpCasts())};
90 LLVM_DEBUG(llvm::dbgs()
91 << "Argument 2d: [" << i << ", " << j << "] "
92 << slit->getString().str() << " \n";);
93 indices.insert(IndexPairType(slit->getString().str(),
94 std::make_tuple(i, j, 0)));
95 }
96
98 if (auto init_expr_list_3d =
99 clang::dyn_cast<clang::InitListExpr>(iexpr_2d)) {
100 clang::Expr **iexpr_3d_set{init_expr_list_3d->getInits()};
101 init_expr_list_3d->dump();
102 for (std::size_t k{0}; k < init_expr_list_3d->getNumInits(); ++k) {
103 LLVM_DEBUG(llvm::dbgs()
104 << "Iterate over 3d init lists: " << k << "\n";);
105 clang::Expr *iexpr_3d{iexpr_3d_set[k]};
106 iexpr_3d->dump();
107
108 clang::CXXConstructExpr *peel{
109 clang::dyn_cast<clang::CXXConstructExpr>(iexpr_3d)};
110 clang::CXXConstructExpr *cexpr{nullptr};
111 while (peel) {
112 cexpr = peel;
113 peel = clang::dyn_cast<clang::CXXConstructExpr>(
114 peel->getArg(0)->IgnoreImplicit());
115 }
116 llvm::dbgs() << "unwrap 3d dump\n";
117 cexpr->dump();
118
119 if (cexpr) {
120 // auto cexpr =
121 // clang::dyn_cast<clang::CXXConstructExpr>(iexpr_3d)) {
122 // clang::CXXConstructExpr *nested_cexpr{
123 // clang::dyn_cast<clang::CXXConstructExpr>(
124 // cexpr->getArg(0)->IgnoreImplicit())};
125 // auto cxxbindexpr{clang::dyn_cast<clang::CXXBindTemporaryExpr>(
126 // nested_cexpr->getArg(0)->IgnoreParenImpCasts())};
127 // auto cxxctor{clang::dyn_cast<clang::CXXConstructExpr>(
128 // cxxbindexpr->getSubExpr()->IgnoreParenImpCasts())};
129 clang::StringLiteral *slit{
130 clang::dyn_cast<clang::StringLiteral>(
131 cexpr->getArg(0)->IgnoreImpCasts())};
132 // slit->dump();
133 LLVM_DEBUG(llvm::dbgs() << "Argument 3d: [" << i << ", " << j
134 << ", " << k << "] "
135 << slit->getString().str() << " \n";);
136 indices.insert(IndexPairType(slit->getString().str(),
137 std::make_tuple(i, j, k)));
138 }
139 }
140 }
141 }
142 }
143 }
144 }
145 return indices;
146}
147
148// ArraySizesType getConstantArraySizes(const clang::FieldDecl *fd) {
149ArraySizesType getConstantArraySizes(const clang::ValueDecl *fd) {
150 ArraySizesType sizes;
151
152 clang::QualType field_type{fd->getType()};
153 clang::QualType save_field_type = field_type;
154 if (field_type->isReferenceType()) {
155 // need a qualtype
156 field_type = field_type->getPointeeType()
157 ->getLocallyUnqualifiedSingleStepDesugaredType();
158 }
159
160 auto array_type{clang::dyn_cast<clang::ConstantArrayType>(field_type)};
161 while (array_type != nullptr) {
162 llvm::APInt array_size{};
163 array_size = array_type->getSize();
164 LLVM_DEBUG(llvm::dbgs() << "Size of array: " << array_size << "\n";);
165 array_type =
166 clang::dyn_cast<clang::ConstantArrayType>(array_type->getElementType());
167
168 sizes.push_back(array_size);
169 }
170
171 return sizes;
172}
173
174ArraySizesExprType getArraySubscripts(const clang::Expr *expr) {
175 ArraySizesExprType subscripts;
176
178 auto arr_sub_expr{clang::dyn_cast<clang::ArraySubscriptExpr>(expr)};
179 while (arr_sub_expr != nullptr) {
180 arr_sub_expr->getIdx()->dump();
181
185
186 auto int_lit{
187 clang::dyn_cast<clang::IntegerLiteral>(arr_sub_expr->getIdx())};
188 auto decl_ref_expr{clang::dyn_cast<clang::DeclRefExpr>(
189 arr_sub_expr->getIdx()->IgnoreImpCasts())};
190
191 if (int_lit) {
192 // llvm::outs() << "SUBSCRIPT: " << int_lit->getValue() << "\n";
193 // subscripts.insert(subscripts.begin(), int_lit->getValue());
194 subscripts.insert(subscripts.begin(), arr_sub_expr->getIdx());
195 }
196
197 if (decl_ref_expr) {
198 LLVM_DEBUG(llvm::dbgs()
199 << "SUBSCRIPT: "
200 << decl_ref_expr->getNameInfo().getName().getAsString()
201 << "\n";);
202 subscripts.insert(subscripts.begin(), decl_ref_expr);
206 }
207
211 arr_sub_expr = clang::dyn_cast<clang::ArraySubscriptExpr>(
212 arr_sub_expr->getBase()->IgnoreParenImpCasts());
213 }
214 return subscripts;
215}
216
217const clang::MemberExpr *getArrayMemberExprName(const clang::Expr *expr) {
219 auto arr_sub_expr{clang::dyn_cast<clang::ArraySubscriptExpr>(expr)};
220 while (arr_sub_expr != nullptr) {
221 auto nested_sub_expr{clang::dyn_cast<clang::ArraySubscriptExpr>(
222 arr_sub_expr->getBase()->IgnoreParenImpCasts())};
223
225 if (nested_sub_expr == nullptr) {
226 const clang::MemberExpr *name_me{clang::dyn_cast<clang::MemberExpr>(
227 arr_sub_expr->getBase()->IgnoreParenImpCasts())};
228 return name_me;
229 }
230 arr_sub_expr = nested_sub_expr;
231 }
232 return nullptr;
233}
234
235} // namespace array_type
236
237} // namespace utils
238} // namespace sc_ast_matchers
std::pair< std::string, std::tuple< std::size_t, std::size_t, std::size_t > > IndexPairType
ArraySizesType getConstantArraySizes(const clang::ValueDecl *fd)
IndexMapType getArrayInstanceIndex(clang::CXXCtorInitializer *ctor_init)
std::map< std::string, std::tuple< std::size_t, std::size_t, std::size_t > > IndexMapType
ArraySizesExprType getArraySubscripts(const clang::Expr *expr)
std::vector< const clang::Expr * > ArraySizesExprType
std::vector< llvm::APInt > ArraySizesType
const clang::MemberExpr * getArrayMemberExprName(const clang::Expr *expr)