-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrewrite_functions.cpp
212 lines (176 loc) · 7.59 KB
/
rewrite_functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "kslicer.h"
#include "class_gen.h"
#include "ast_matchers.h"
#include <sstream>
#include <algorithm>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::string kslicer::FunctionRewriter::FunctionCallRewrite(const CallExpr* call)
{
const FunctionDecl* fDecl = call->getDirectCallee();
if(fDecl == nullptr) // definitely can't process nullpointer
return "[FunctionRewriter::FunctionCallRewrite_ERROR]";
std::string argsType = "";
if(call->getNumArgs() > 0)
{
const Expr* firstArgExpr = call->getArgs()[0];
const QualType qt = firstArgExpr->getType();
argsType = qt.getAsString();
}
std::string fname = fDecl->getNameInfo().getName().getAsString();
std::string textRes = m_codeInfo->pShaderCC->ReplaceCallFromStdNamespace(fname, argsType);
//std::string textRes = fDecl->getNameInfo().getName().getAsString();
textRes += "(";
for(unsigned i=0;i<call->getNumArgs();i++)
{
textRes += RecursiveRewrite(call->getArg(i));
if(i < call->getNumArgs()-1)
textRes += ",";
}
textRes += ")";
return textRes;
}
std::string kslicer::FunctionRewriter::FunctionCallRewriteNoName(const CXXConstructExpr* call)
{
std::string textRes = "(";
for(unsigned i=0;i<call->getNumArgs();i++)
{
textRes += RecursiveRewrite(kslicer::RemoveImplicitCast(call->getArg(i)));
if(i < call->getNumArgs()-1)
textRes += ",";
}
return textRes + ")";
}
std::string kslicer::FunctionRewriter::FunctionCallRewrite(const CXXConstructExpr* call)
{
std::string textRes = call->getConstructor()->getNameInfo().getName().getAsString();
return textRes + FunctionCallRewriteNoName(call);
}
bool kslicer::FunctionRewriter::VisitCallExpr_Impl(CallExpr* call)
{
if(isa<CXXMemberCallExpr>(call)) // process in VisitCXXMemberCallExpr
return true;
const FunctionDecl* fDecl = call->getDirectCallee();
if(fDecl == nullptr) // definitely can't process nullpointer
return true;
// Get name of function
//
std::string fname = fDecl->getNameInfo().getName().getAsString();
//std::cout << "[VisitCallExpr]: fname = " << fname.c_str() << std::endl;
if(fDecl->isInStdNamespace())
{
std::string argsType = "";
if(call->getNumArgs() > 0)
{
const Expr* firstArgExpr = call->getArgs()[0];
const QualType qt = firstArgExpr->getType();
argsType = qt.getAsString();
}
if(WasNotRewrittenYet(call))
{
auto debugMeIn = GetRangeSourceCode(call->getSourceRange(), m_compiler);
auto textRes = FunctionCallRewrite(call);
ReplaceTextOrWorkAround(call->getSourceRange(), textRes);
MarkRewritten(call);
//std::cout << " " << text.c_str() << " of type " << argsType.c_str() << "; --> " << textRes.c_str() << std::endl;
}
}
return true;
}
std::string kslicer::FunctionRewriter::VectorTypeContructorReplace(const std::string& fname, const std::string& callText)
{
return std::string("make_") + fname + callText;
}
bool kslicer::FunctionRewriter::VisitCXXConstructExpr_Impl(CXXConstructExpr* call)
{
CXXConstructorDecl* ctorDecl = call->getConstructor();
assert(ctorDecl != nullptr);
const std::string debugText = GetRangeSourceCode(call->getSourceRange(), m_compiler);
const std::string fname = ctorDecl->getNameInfo().getName().getAsString();
if(debugText.find("float4x4") != std::string::npos)
{
int a = 2;
}
if(kslicer::IsVectorContructorNeedsReplacement(fname) && WasNotRewrittenYet(call) && !ctorDecl->isCopyOrMoveConstructor() && call->getNumArgs() > 0 ) //
{
const std::string text = FunctionCallRewriteNoName(call);
std::string textRes = VectorTypeContructorReplace(fname, text);
if(fname == "complex" && call->getNumArgs() == 1)
textRes = "to_complex" + text;
else if(fname == "complex" && call->getNumArgs() == 2)
textRes = "make_complex" + text;
ReplaceTextOrWorkAround(call->getSourceRange(), textRes); //
//m_rewriter.ReplaceText(call->getSourceRange(), textRes); //
MarkRewritten(call);
}
return true;
}
bool kslicer::FunctionRewriter::VisitCXXOperatorCallExpr_Impl(clang::CXXOperatorCallExpr* expr)
{
std::string op = kslicer::GetRangeSourceCode(clang::SourceRange(expr->getOperatorLoc()), m_compiler);
std::string debugText = kslicer::GetRangeSourceCode(expr->getSourceRange(), m_compiler);
static std::unordered_map<std::string, std::string> remapOp = {{"+","add"}, {"-","sub"}, {"*","mul"}, {"/","div"}};
if((op == "+" || op == "-" || op == "*" || op == "/") && WasNotRewrittenYet(expr))
{
clang::Expr* left = kslicer::RemoveImplicitCast(expr->getArg(0));
clang::Expr* right = kslicer::RemoveImplicitCast(expr->getArg(1));
const std::string leftType = left->getType().getAsString();
const std::string rightType = right->getType().getAsString();
const std::string keyType = "complex";
if(debugText == "eta * cosThetaI")
{
int a = 2;
//expr->dump();
}
if(leftType == keyType || rightType == keyType)
{
const std::string leftText = RecursiveRewrite(left);
const std::string rightText = RecursiveRewrite(right);
std::string rewrittenOp;
{
if(leftType == keyType && rightType == keyType)
rewrittenOp = keyType + "_" + remapOp[op] + "(" + leftText + "," + rightText + ")";
else if (leftType == keyType)
rewrittenOp = keyType + "_" + remapOp[op] + "_real(" + leftText + "," + rightText + ")";
else if(rightType == keyType)
rewrittenOp = "real_" + remapOp[op] + "_" + keyType + "(" + leftText + "," + rightText + ")";
}
ReplaceTextOrWorkAround(expr->getSourceRange(), rewrittenOp);
MarkRewritten(expr);
}
}
return true;
}
std::string kslicer::FunctionRewriter::RecursiveRewrite(const clang::Stmt* expr)
{
if(expr == nullptr)
return "";
FunctionRewriter rvCopy = *this;
rvCopy.TraverseStmt(const_cast<clang::Stmt*>(expr));
auto range = expr->getSourceRange();
auto p = rvCopy.m_workAround.find(GetHashOfSourceRange(range));
if(p != rvCopy.m_workAround.end())
return p->second;
else
return m_rewriter.getRewrittenText(range);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool kslicer::NodesMarker::VisitStmt(clang::Stmt* expr)
{
auto hash = kslicer::GetHashOfSourceRange(expr->getSourceRange());
m_rewrittenNodes.insert(hash);
return true;
}
void kslicer::MarkRewrittenRecursive(const clang::Stmt* currNode, std::unordered_set<uint64_t>& a_rewrittenNodes)
{
kslicer::NodesMarker rv(a_rewrittenNodes);
rv.TraverseStmt(const_cast<clang::Stmt*>(currNode));
}
void kslicer::MarkRewrittenRecursive(const clang::Decl* currNode, std::unordered_set<uint64_t>& a_rewrittenNodes)
{
kslicer::NodesMarker rv(a_rewrittenNodes);
rv.TraverseDecl(const_cast<clang::Decl*>(currNode));
}