forked from samanbarghi/CPP2C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPP2C.cpp
325 lines (275 loc) · 10.4 KB
/
CPP2C.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <clang/AST/AST.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/ASTMatchers/ASTMatchers.h>
#include <clang/Driver/Options.h>
#include <clang/Frontend/ASTConsumers.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Rewrite/Core/Rewriter.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/Tooling/Tooling.h>
#include <iostream>
#include <llvm/ADT/StringExtras.h>
#include <map>
#include <set>
#include <sstream>
#include <tuple>
#include <vector>
using namespace std;
using namespace clang;
using namespace clang::driver;
using namespace clang::tooling;
using namespace clang::ast_matchers;
using namespace llvm;
/** Options **/
static cl::OptionCategory CPP2CCategory("CPP2C options");
static cl::opt<std::string> OutputFilename(
"o", cl::desc(getDriverOptTable().getOptionHelpText((options::OPT_o))),
cl::cat(CPP2CCategory));
static cl::opt<std::string> ClassesToGenrate(
"classes", cl::desc("Classes to generate C binding, split by space"),
cl::init("uThread kThread Cluster Connection Mutex OwnerLock "
"ConditionVariable Semaphore uThreadPool"),
cl::cat(CPP2CCategory));
/** Classes to be mapped to C **/
struct OutputStreams {
string headerString;
string bodyString;
llvm::raw_string_ostream HeaderOS;
llvm::raw_string_ostream BodyOS;
OutputStreams() : HeaderOS(headerString), BodyOS(bodyString){};
};
llvm::SmallVector<llvm::StringRef, 16> ClassList;
map<string, int> funcList;
/** Matchers **/
/** Handlers **/
class classMatchHandler : public MatchFinder::MatchCallback {
public:
classMatchHandler(OutputStreams &os) : OS(os) {}
tuple<string, string, bool, bool> determineCType(const QualType &qt) {
string CType;
string CastType; // whether this should be casted or not
bool isPointer = false;
bool shoulReturn = true;
// if it is builtint type use it as is
if (qt->isBuiltinType() ||
(qt->isPointerType() && qt->getPointeeType()->isBuiltinType())) {
CType = qt.getAsString();
if (qt->isVoidType())
shoulReturn = false;
// if it is a CXXrecordDecl then return a pointer to WName*
} else if (qt->isRecordType()) {
const CXXRecordDecl *crd = qt->getAsCXXRecordDecl();
string recordName = crd->getNameAsString();
CType = "W" + recordName + "*";
CastType = recordName + "*";
} else if ((qt->isReferenceType() || qt->isPointerType()) &&
qt->getPointeeType()->isRecordType()) {
isPointer = true; // to properly differentiate among cast types
const CXXRecordDecl *crd = qt->getPointeeType()->getAsCXXRecordDecl();
string recordName = crd->getNameAsString();
if (std::find(ClassList.begin(), ClassList.end(), recordName) !=
ClassList.end()) {
CType = "W" + recordName + "*";
CastType = recordName + "*";
} else {
CType = recordName + "*";
}
}
return make_tuple(CType, CastType, isPointer, shoulReturn);
}
virtual void run(const MatchFinder::MatchResult &Result) {
if (const CXXMethodDecl *cmd =
Result.Nodes.getNodeAs<CXXMethodDecl>("publicMethodDecl")) {
string methodName;
string className = cmd->getParent()->getDeclName().getAsString();
string returnType;
string returnCast;
bool shouldReturn, isPointer;
string self = "W" + className + "* self";
string separator = ", ";
string bodyEnd;
std::stringstream functionBody;
// ignore operator overloadings
if (cmd->isOverloadedOperator())
return;
// constructor
if (const CXXConstructorDecl *ccd = dyn_cast<CXXConstructorDecl>(cmd)) {
if (ccd->isCopyConstructor() || ccd->isMoveConstructor())
return;
methodName = "_create";
returnType = "W" + className + "*";
self = "";
separator = "";
functionBody << "return reinterpret_cast<" << returnType << ">( new "
<< className << "(";
bodyEnd += "))";
} else if (isa<CXXDestructorDecl>(cmd)) {
methodName = "_destroy";
returnType = "void";
functionBody << " delete reinterpret_cast<" << className << "*>(self)";
} else {
methodName = "_" + cmd->getNameAsString();
const QualType qt = cmd->getReturnType();
std::tie(returnType, returnCast, isPointer, shouldReturn) =
determineCType(qt);
// should this function return?
if (shouldReturn)
functionBody << "return ";
if (returnCast != "") {
// if not pointer and it needs to be casted, then return the pointer
if (!isPointer)
functionBody << "&";
functionBody << "reinterpret_cast<" << returnType << ">(";
bodyEnd += ")";
}
// if Static call it properly
if (cmd->isStatic())
functionBody << className << "::" << cmd->getNameAsString() << "(";
// if not use the passed object to call the method
else
functionBody << "reinterpret_cast<" << className << "*>(self)->"
<< cmd->getNameAsString() << "(";
bodyEnd += ")";
}
std::stringstream funcname;
funcname << returnType << " " << className << methodName;
auto it = funcList.find(funcname.str());
if (it != funcList.end()) {
it->second++;
funcname << "_" << it->second;
} else {
funcList[funcname.str()] = 0;
}
funcname << "(" << self;
for (unsigned int i = 0; i < cmd->getNumParams(); i++) {
const QualType qt = cmd->parameters()[i]->getType();
std::tie(returnType, returnCast, isPointer, shouldReturn) =
determineCType(qt);
funcname << separator << returnType << " ";
funcname << cmd->parameters()[i]->getQualifiedNameAsString() << "";
if (i != 0)
functionBody << separator;
if (returnCast == "")
functionBody << cmd->parameters()[i]->getQualifiedNameAsString();
else {
if (!isPointer)
functionBody << "*";
functionBody << "reinterpret_cast<" << returnCast << ">("
<< cmd->parameters()[i]->getQualifiedNameAsString()
<< ")";
}
string separator = ", ";
}
funcname << ")";
OS.HeaderOS << funcname.str() << ";\n";
OS.BodyOS << funcname.str() << "{\n ";
OS.BodyOS << functionBody.str();
OS.BodyOS << bodyEnd << "; \n}\n";
}
}
virtual void onEndOfTranslationUnit() {}
private:
OutputStreams &OS;
};
/****************** /Member Functions *******************************/
// Implementation of the ASTConsumer interface for reading an AST produced
// by the Clang parser. It registers a couple of matchers and runs them on
// the AST.
class MyASTConsumer : public ASTConsumer {
public:
MyASTConsumer(OutputStreams &os) : OS(os), HandlerForClassMatcher(os) {
// Add a simple matcher for finding 'if' statements.
for (const std::string &className : ClassList) {
OS.HeaderOS << "struct W" << className
<< "; \n"
"typedef struct W"
<< className << " W" << className << ";\n";
// oss.push_back(std::move(os))
DeclarationMatcher classMatcher =
cxxMethodDecl(isPublic(), ofClass(hasName(className)))
.bind("publicMethodDecl");
Matcher.addMatcher(classMatcher, &HandlerForClassMatcher);
}
}
void HandleTranslationUnit(ASTContext &Context) override {
// Run the matchers when we have the whole TU parsed.
Matcher.matchAST(Context);
}
private:
OutputStreams &OS;
classMatchHandler HandlerForClassMatcher;
MatchFinder Matcher;
};
// For each source file provided to the tool, a new FrontendAction is created.
class MyFrontendAction : public ASTFrontendAction {
public:
MyFrontendAction() {
OS.HeaderOS << "#ifndef UTHREADS_CWRAPPER_H\n"
"#define UTHREADS_CWRAPPER_H_\n"
"#include <pthread.h>\n"
"#include <sys/types.h>\n"
"#include <sys/socket.h>\n"
"#include <inttypes.h>\n\n"
"#ifdef __cplusplus\n"
"extern \"C\"{\n"
"#endif\n"
"#include <stdbool.h>\n";
OS.BodyOS << "#include \"generic/basics.h\"\n"
"#include \"cwrapper.h\"\n"
"#include \"runtime/uThread.h\"\n"
"#include \"runtime/uThreadPool.h\"\n"
"#include \"runtime/kThread.h\"\n"
"#include \"io/Network.h\"\n"
"#ifdef __cplusplus\n"
"extern \"C\"{\n"
"#endif\n";
}
void EndSourceFileAction() override {
StringRef headerFile("cwrapper.h");
StringRef bodyFile("cwrapper.cpp");
// Open the output file
std::error_code EC;
llvm::raw_fd_ostream HOS(headerFile, EC, llvm::sys::fs::F_None);
if (EC) {
llvm::errs() << "while opening '" << headerFile << "': " << EC.message()
<< '\n';
exit(1);
}
llvm::raw_fd_ostream BOS(bodyFile, EC, llvm::sys::fs::F_None);
if (EC) {
llvm::errs() << "while opening '" << bodyFile << "': " << EC.message()
<< '\n';
exit(1);
}
OS.HeaderOS << "#ifdef __cplusplus\n"
"}\n"
"#endif\n"
"#endif /* UTHREADS_CWRAPPER_H_ */\n";
OS.BodyOS << "#ifdef __cplusplus\n"
"}\n"
"#endif\n";
OS.HeaderOS.flush();
OS.BodyOS.flush();
HOS << OS.headerString << "\n";
BOS << OS.bodyString << "\n";
}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef file) override {
return std::make_unique<MyASTConsumer>(OS);
}
private:
OutputStreams OS;
};
int main(int argc, const char **argv) {
// parse the command-line args passed to your code
CommonOptionsParser op(argc, argv, CPP2CCategory);
// create a new Clang Tool instance (a LibTooling environment)
ClangTool Tool(op.getCompilations(), op.getSourcePathList());
llvm::SplitString(ClassesToGenrate, ClassList, " ");
// run the Clang Tool, creating a new FrontendAction
return Tool.run(newFrontendActionFactory<MyFrontendAction>().get());
}