-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetter_process.cpp
More file actions
195 lines (161 loc) · 6.8 KB
/
setter_process.cpp
File metadata and controls
195 lines (161 loc) · 6.8 KB
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
#include <stdio.h>
#include <vector>
#include <system_error>
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <iomanip>
#include "kslicer.h"
class SetterVisitor : public clang::RecursiveASTVisitor<SetterVisitor>
{
public:
SetterVisitor(const clang::CompilerInstance& a_compiler, clang::Rewriter &R, const std::unordered_map<std::string, const clang::CXXRecordDecl*>& a_types) :
m_compiler(a_compiler), m_sm(a_compiler.getSourceManager()), m_rewriter(R), m_targetTypes(a_types)
{
}
bool VisitMemberExpr(const clang::MemberExpr* expr)
{
const clang::ValueDecl* pDecl = expr->getMemberDecl();
const clang::QualType qt = pDecl->getType();
const std::string typeName = qt.getAsString();
auto pFound = m_targetTypes.find(typeName);
if(pFound != m_targetTypes.end())
m_foundMembers[pDecl->getNameAsString()] = typeName;
std::string text = kslicer::GetRangeSourceCode(expr->getSourceRange(), m_compiler);
m_rewriter.ReplaceText(expr->getSourceRange(), text + "Vulkan");
return true;
}
const std::unordered_map<std::string, std::string>& GetMembers() const { return m_foundMembers; }
private:
const clang::CompilerInstance& m_compiler;
const clang::SourceManager& m_sm;
clang::Rewriter& m_rewriter;
const std::unordered_map<std::string, const clang::CXXRecordDecl*>& m_targetTypes;
std::unordered_map<std::string, std::string> m_foundMembers; ///<! member name --> member type name
};
std::unordered_map<std::string, const clang::CXXRecordDecl*> ListStructParamTypes(const clang::CXXMethodDecl* node)
{
std::unordered_map<std::string, const clang::CXXRecordDecl*> structTypeNames;
for(unsigned paramId = 0; paramId < node->getNumParams(); paramId++)
{
const clang::ParmVarDecl* pParam = node->getParamDecl(paramId);
const clang::QualType typeOfParam = pParam->getType();
const clang::CXXRecordDecl* pDecl = typeOfParam->getAsCXXRecordDecl();
if(pDecl == nullptr)
continue;
structTypeNames[typeOfParam.getAsString()] = pDecl;
}
return structTypeNames;
}
void kslicer::MainClassInfo::ProcessAllSetters(const std::unordered_map<std::string, const clang::CXXMethodDecl*>& a_setterFunc, clang::CompilerInstance& a_compiler)
{
auto& a_rewrittenDecls = m_setterStructDecls;
auto& a_rewrittenFuncs = m_setterFuncDecls;
auto& a_variables = m_setterVars;
a_rewrittenDecls.clear();
a_rewrittenFuncs.clear();
a_variables.clear();
std::unordered_map<std::string, const clang::CXXRecordDecl*> allStructTypes;
for(const auto kv : a_setterFunc)
{
const clang::CXXMethodDecl* node = kv.second;
auto structTypeNames = ListStructParamTypes(node);
const std::string fname = node->getNameInfo().getName().getAsString();
allStructTypes.insert(structTypeNames.begin(), structTypeNames.end());
// (1) traverse type decl, rewrite (pointer, texture, accel_struct) members
//
for(const auto& kv : structTypeNames)
{
const clang::CXXRecordDecl* pDecl = kv.second;
std::stringstream strOut;
strOut << "struct " << kv.first.c_str() << "Vulkan" << "{" << std::endl;
for(const auto field : pDecl->fields()) // clang::FieldDecl
{
const std::string varName = field->getNameAsString();
const clang::QualType qt = field->getType();
if(qt->isPointerType())
{
strOut << " VkBuffer " << varName.c_str() << "Buffer = VK_NULL_HANDLE; size_t " << varName.c_str() << "Offset = 0;" << std::endl;
}
else if(qt->isReferenceType() && kslicer::IsTexture(qt))
{
strOut << " VkImage " << varName.c_str() << "Image = VK_NULL_HANDLE; VkImageView " << varName.c_str() << "View = VK_NULL_HANDLE;" << std::endl;
}
else
{
strOut << " " << qt.getAsString() << " " << varName.c_str() << ";" << std::endl;
}
}
strOut << "};" << std::endl;
a_rewrittenDecls.push_back(strOut.str());
}
// (2) traverse setter function node, rename all structure members and parameters of any type of 'structTypeNames'
//
clang::Rewriter rewrite;
rewrite.setSourceMgr(a_compiler.getSourceManager(), a_compiler.getLangOpts());
SetterVisitor visitor(a_compiler, rewrite, structTypeNames);
visitor.TraverseDecl(const_cast<clang::CXXMethodDecl*>(node));
std::string rewrittenFunc = rewrite.getRewrittenText(node->getSourceRange());
std::string rewrittenBody = rewrittenFunc.substr(rewrittenFunc.find("{"));
std::stringstream strOutFD;
strOutFD << "void " << fname.c_str() << "Vulkan(";
for(unsigned paramId = 0; paramId < node->getNumParams(); paramId++)
{
const clang::ParmVarDecl* pParam = node->getParamDecl(paramId);
const clang::QualType typeOfParam = pParam->getType();
const clang::CXXRecordDecl* pDecl = typeOfParam->getAsCXXRecordDecl();
if(pDecl != nullptr)
strOutFD << typeOfParam.getAsString() << "Vulkan " << pParam->getNameAsString();
else
strOutFD << kslicer::GetRangeSourceCode(pParam->getSourceRange(), a_compiler);
if(paramId < node->getNumParams()-1)
strOutFD << ", ";
}
strOutFD << ")";
std::string finalFuncText = strOutFD.str() + rewrittenBody;
a_rewrittenFuncs.push_back(finalFuncText);
for(const auto kv : visitor.GetMembers())
a_variables[kv.first] = kv.second;
}
//// (3) exclude all setter structs from allDataMembers
//
for(auto var : a_variables)
{
auto p = allDataMembers.find(var.first);
if(p != allDataMembers.end())
allDataMembers.erase(p);
}
//// (4) add pointers (and other) for all setters to m_setterData
//
for(auto var : a_variables)
{
auto ssName = var.first;
auto ssType = var.second;
auto pFound = allStructTypes.find(ssType);
if(pFound == allStructTypes.end())
continue;
const clang::CXXRecordDecl* pDecl = pFound->second;
for(const auto field : pDecl->fields()) // clang::FieldDecl
{
const std::string varName = field->getNameAsString();
const clang::QualType qt = field->getType();
if(qt->isPointerType())
{
auto qtOfData = qt->getPointeeType();
kslicer::DataMemberInfo data;
data.name = ssName + "_" + field->getNameAsString();
data.type = qt.getAsString();
data.kind = kslicer::DATA_KIND::KIND_POINTER;
data.isContainer = true;
data.containerType = "vector"; ///<! std::vector usually
data.containerDataType = qtOfData.getAsString(); ///<! data type 'T' inside of std::vector<T>
m_setterData[data.name] = data;
}
else if(qt->isReferenceType() && kslicer::IsTexture(qt))
{
// #TODO: implement it
}
}
}
}