]> git.saurik.com Git - cycript.git/blame - Analyze.cpp
Generate functions to "bind" parameterized macros.
[cycript.git] / Analyze.cpp
CommitLineData
2e43a0b0
JF
1/* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3*/
4
5/* GNU Affero General Public License, Version 3 {{{ */
6/*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19**/
20/* }}} */
21
3935b9e5 22#include <cmath>
8d20f0f1
JF
23#include <cstring>
24#include <iostream>
25#include <map>
26#include <sstream>
27#include <string>
28
29#include <clang-c/Index.h>
30
2e43a0b0
JF
31#include "Functor.hpp"
32#include "Replace.hpp"
33#include "Syntax.hpp"
34
35static CXChildVisitResult CYVisit(CXCursor cursor, CXCursor parent, CXClientData arg) {
36 (*reinterpret_cast<const Functor<void (CXCursor)> *>(arg))(cursor);
37 return CXChildVisit_Continue;
38}
39
40static unsigned CYForChild(CXCursor cursor, const Functor<void (CXCursor)> &visitor) {
41 return clang_visitChildren(cursor, &CYVisit, const_cast<void *>(static_cast<const void *>(&visitor)));
42}
43
44static bool CYOneChild(CXCursor cursor, const Functor<void (CXCursor)> &visitor) {
45 bool visited(false);
46 CYForChild(cursor, fun([&](CXCursor child) {
47 _assert(!visited);
48 visited = true;
49 visitor(child);
50 }));
51 return visited;
52}
53
8d20f0f1
JF
54struct CYCXString {
55 CXString value_;
56
57 CYCXString(CXString value) :
58 value_(value)
59 {
60 }
61
2e43a0b0
JF
62 CYCXString(CXCursor cursor) :
63 value_(clang_getCursorSpelling(cursor))
64 {
65 }
66
67 CYCXString(CXCursorKind kind) :
68 value_(clang_getCursorKindSpelling(kind))
69 {
70 }
71
3935b9e5
JF
72 CYCXString(CXFile file) :
73 value_(clang_getFileName(file))
74 {
75 }
76
2e43a0b0
JF
77 CYCXString(CXTranslationUnit unit, CXToken token) :
78 value_(clang_getTokenSpelling(unit, token))
79 {
80 }
81
8d20f0f1
JF
82 ~CYCXString() {
83 clang_disposeString(value_);
84 }
85
86 operator const char *() const {
87 return clang_getCString(value_);
88 }
8d20f0f1 89
2e43a0b0
JF
90 const char *Pool(CYPool &pool) const {
91 return pool.strdup(*this);
8d20f0f1 92 }
7752205a
JF
93
94 bool operator ==(const char *rhs) const {
95 const char *lhs(*this);
96 return lhs == rhs || strcmp(lhs, rhs) == 0;
97 }
b7854baa
JF
98};
99
3935b9e5
JF
100template <void (&clang_get_Location)(CXSourceLocation, CXFile *, unsigned *, unsigned *, unsigned *) = clang_getSpellingLocation>
101struct CYCXPosition {
102 CXFile file_;
103 unsigned line_;
104 unsigned column_;
105 unsigned offset_;
106
107 CYCXPosition(CXSourceLocation location) {
108 clang_get_Location(location, &file_, &line_, &column_, &offset_);
109 }
110
7752205a
JF
111 CYCXPosition(CXTranslationUnit unit, CXToken token) :
112 CYCXPosition(clang_getTokenLocation(unit, token))
113 {
114 }
115
3935b9e5
JF
116 CXSourceLocation Get(CXTranslationUnit unit) const {
117 return clang_getLocation(unit, file_, line_, column_);
118 }
119};
120
121template <void (&clang_get_Location)(CXSourceLocation, CXFile *, unsigned *, unsigned *, unsigned *)>
122std::ostream &operator <<(std::ostream &out, const CYCXPosition<clang_get_Location> &position) {
123 if (position.file_ != NULL)
124 out << "[" << CYCXString(position.file_) << "]:";
7752205a
JF
125 out << position.line_ << ":" << position.column_ << "@" << position.offset_;
126 return out;
3935b9e5
JF
127}
128
8d20f0f1
JF
129typedef std::map<std::string, std::string> CYKeyMap;
130
131struct CYChildBaton {
132 CXTranslationUnit unit;
133 CYKeyMap &keys;
134
135 CYChildBaton(CXTranslationUnit unit, CYKeyMap &keys) :
136 unit(unit),
137 keys(keys)
138 {
139 }
140};
141
142struct CYTokens {
7752205a 143 private:
3935b9e5
JF
144 CXTranslationUnit unit_;
145 CXToken *tokens_;
146 unsigned count_;
7752205a 147 unsigned valid_;
3935b9e5 148
7752205a 149 public:
3935b9e5
JF
150 CYTokens(CXTranslationUnit unit, CXSourceRange range) :
151 unit_(unit)
152 {
153 clang_tokenize(unit_, range, &tokens_, &count_);
7752205a
JF
154
155
156 // libclang's tokenizer is horribly broken and returns "extra" tokens.
157 // this code goes back through the tokens and filters for good ones :/
158
159 CYCXPosition<> end(clang_getRangeEnd(range));
160 CYCXString file(end.file_);
161
162 for (valid_ = 0; valid_ != count_; ++valid_) {
163 CYCXPosition<> position(unit, tokens_[valid_]);
164 _assert(CYCXString(position.file_) == file);
165 if (position.offset_ >= end.offset_)
166 break;
167 }
3935b9e5 168 }
8d20f0f1
JF
169
170 CYTokens(CXTranslationUnit unit, CXCursor cursor) :
3935b9e5 171 CYTokens(unit, clang_getCursorExtent(cursor))
8d20f0f1 172 {
8d20f0f1
JF
173 }
174
175 ~CYTokens() {
3935b9e5 176 clang_disposeTokens(unit_, tokens_, count_);
8d20f0f1
JF
177 }
178
179 operator CXToken *() const {
3935b9e5 180 return tokens_;
8d20f0f1 181 }
7752205a
JF
182
183 size_t size() const {
184 return valid_;
185 }
8d20f0f1
JF
186};
187
38c824bf
JF
188static CYUTF8String CYCXPoolUTF8Range(CYPool &pool, CXSourceRange range) {
189 CYCXPosition<> start(clang_getRangeStart(range));
190 CYCXPosition<> end(clang_getRangeEnd(range));
191 CYCXString file(start.file_);
192 _assert(file == CYCXString(end.file_));
193
194 CYPool temp;
195 size_t size;
196 char *data(static_cast<char *>(CYPoolFile(temp, file, &size)));
197 _assert(start.offset_ <= size && end.offset_ <= size && start.offset_ <= end.offset_);
198
199 CYUTF8String code;
200 code.size = end.offset_ - start.offset_;
201 code.data = pool.strndup(data + start.offset_, code.size);
202 return code;
203}
204
2e43a0b0
JF
205static CYExpression *CYTranslateExpression(CXTranslationUnit unit, CXCursor cursor) {
206 switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
207 case CXCursor_CallExpr: {
208 CYExpression *function(NULL);
209 CYList<CYArgument> arguments;
210 CYForChild(cursor, fun([&](CXCursor child) {
211 CYExpression *expression(CYTranslateExpression(unit, child));
212 if (function == NULL)
213 function = expression;
214 else
215 arguments->*$C_(expression);
216 }));
217 return $C(function, arguments);
218 } break;
219
220 case CXCursor_DeclRefExpr: {
221 return $V(CYCXString(cursor).Pool($pool));
222 } break;
223
224 case CXCursor_IntegerLiteral: {
3935b9e5
JF
225 // libclang doesn't provide any reasonable way to do this
226 // note: clang_tokenize doesn't work if this is a macro
227 // the token range starts inside the macro but ends after it
228 // the tokenizer freaks out and either fails with 0 tokens
229 // or returns some massive number of tokens ending here :/
230
38c824bf 231 CYUTF8String token(CYCXPoolUTF8Range($pool, clang_getCursorExtent(cursor)));
3935b9e5 232 double value(CYCastDouble(token));
38c824bf
JF
233 if (std::isnan(value))
234 return $V(token.data);
235 return $ CYNumber(value);
2e43a0b0
JF
236 } break;
237
238 case CXCursor_CStyleCastExpr:
239 // XXX: most of the time, this is a "NoOp" integer cast; but we should check it
240
241 case CXCursor_UnexposedExpr:
242 // there is a very high probability that this is actually an "ImplicitCastExpr"
243 // "Douglas Gregor" <dgregor@apple.com> err'd on the incorrect side of this one
244 // http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20110926/046998.html
245
246 case CXCursor_ParenExpr: {
247 CYExpression *pass(NULL);
248 CYOneChild(cursor, fun([&](CXCursor child) {
249 pass = CYTranslateExpression(unit, child);
250 }));
251 return pass;
252 } break;
253
254 default:
255 //std::cerr << "E:" << CYCXString(kind) << std::endl;
256 _assert(false);
257 }
258}
259
260static CYStatement *CYTranslateStatement(CXTranslationUnit unit, CXCursor cursor) {
261 switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
262 case CXCursor_ReturnStmt: {
263 CYExpression *value(NULL);
264 CYOneChild(cursor, fun([&](CXCursor child) {
265 value = CYTranslateExpression(unit, child);
266 }));
267 return $ CYReturn(value);
268 } break;
269
270 default:
271 //std::cerr << "S:" << CYCXString(kind) << std::endl;
272 _assert(false);
273 }
274}
275
276static CYStatement *CYTranslateBlock(CXTranslationUnit unit, CXCursor cursor) {
277 CYList<CYStatement> statements;
278 CYForChild(cursor, fun([&](CXCursor child) {
279 statements->*CYTranslateStatement(unit, child);
280 }));
281 return $ CYBlock(statements);
282}
283
8d20f0f1
JF
284static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClientData arg) {
285 CYChildBaton &baton(*static_cast<CYChildBaton *>(arg));
286 CXTranslationUnit &unit(baton.unit);
287
2e43a0b0 288 CYCXString spelling(cursor);
8d20f0f1
JF
289 std::string name(spelling);
290 std::ostringstream value;
291
292 /*CXSourceLocation location(clang_getCursorLocation(cursor));
3935b9e5
JF
293 CYCXPosition<> position(location);
294 std::cout << spelling << " " << position << std::endl;*/
8d20f0f1 295
2e43a0b0 296 switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
8d20f0f1
JF
297 case CXCursor_EnumConstantDecl: {
298 value << clang_getEnumConstantDeclValue(cursor);
299 } break;
300
7752205a
JF
301 case CXCursor_MacroDefinition: try {
302 CXSourceRange range(clang_getCursorExtent(cursor));
303 CYTokens tokens(unit, range);
304 _assert(tokens.size() != 0);
8d20f0f1 305
7752205a
JF
306 CXCursor cursors[tokens.size()];
307 clang_annotateTokens(unit, tokens, tokens.size(), cursors);
8d20f0f1 308
dee38f6c
JF
309 CYLocalPool local;
310 CYList<CYFunctionParameter> parameters;
311 unsigned offset(1);
312
313 if (tokens.size() != 1) {
314 CYCXPosition<> start(clang_getRangeStart(range));
315 CYCXString first(unit, tokens[offset]);
316 if (first == "(") {
317 CYCXPosition<> paren(unit, tokens[offset]);
318 if (start.offset_ + strlen(spelling) == paren.offset_) {
319 for (;;) {
320 _assert(++offset != tokens.size());
321 CYCXString token(unit, tokens[offset]);
322 parameters->*$P($B($I(token.Pool($pool))));
323 _assert(++offset != tokens.size());
324 CYCXString comma(unit, tokens[offset]);
325 if (comma == ")")
326 break;
327 _assert(comma == ",");
328 }
329 ++offset;
330 }
331 }
7752205a
JF
332 }
333
dee38f6c
JF
334 std::ostringstream body;
335 for (unsigned i(offset); i != tokens.size(); ++i) {
2e43a0b0 336 CYCXString token(unit, tokens[i]);
dee38f6c
JF
337 if (i != offset)
338 body << " ";
339 body << token;
340 }
341
342 if (!parameters)
343 value << body.str();
344 else {
345 CYOptions options;
346 CYOutput out(*value.rdbuf(), options);
347 out << '(' << "function" << '(';
348 out << parameters;
349 out << ')' << '{';
350 out << "return" << ' ';
351 value << body.str();
352 out << ';' << '}' << ')';
8d20f0f1 353 }
7752205a
JF
354 } catch (const CYException &error) {
355 CYPool pool;
356 //std::cerr << error.PoolCString(pool) << std::endl;
357 goto skip;
8d20f0f1
JF
358 } break;
359
360 case CXCursor_StructDecl: {
361 if (!clang_isCursorDefinition(cursor))
362 goto skip;
363 if (spelling[0] == '\0')
364 goto skip;
365
2e43a0b0
JF
366 std::ostringstream types;
367 std::ostringstream names;
368
369 CYForChild(cursor, fun([&](CXCursor child) {
370 if (clang_getCursorKind(child) == CXCursor_FieldDecl) {
371 CXType type(clang_getCursorType(child));
372 types << "(typedef " << CYCXString(clang_getTypeSpelling(type)) << "),";
373 names << "'" << CYCXString(child) << "',";
374 }
375 }));
8d20f0f1
JF
376
377 name += "$cy";
2e43a0b0 378 value << "new Type([" << types.str() << "],[" << names.str() << "])";
8d20f0f1
JF
379 } break;
380
381 case CXCursor_TypedefDecl: {
382 CXType type(clang_getTypedefDeclUnderlyingType(cursor));
383 value << "(typedef " << CYCXString(clang_getTypeSpelling(type)) << ")";
384 } break;
385
386 case CXCursor_FunctionDecl:
2e43a0b0
JF
387 case CXCursor_VarDecl: try {
388 std::string label;
389
390 CYList<CYFunctionParameter> parameters;
391 CYStatement *code(NULL);
392
393 CYLocalPool local;
394
395 CYForChild(cursor, fun([&](CXCursor child) {
396 switch (CXCursorKind kind = clang_getCursorKind(child)) {
397 case CXCursor_AsmLabelAttr:
398 label = CYCXString(child);
399 break;
400
401 case CXCursor_CompoundStmt:
402 code = CYTranslateBlock(unit, child);
403 break;
404
405 case CXCursor_ParmDecl:
406 parameters->*$P($B($I(CYCXString(child).Pool($pool))));
407 break;
408
409 case CXCursor_IntegerLiteral:
410 case CXCursor_ObjCClassRef:
411 case CXCursor_TypeRef:
412 case CXCursor_UnexposedAttr:
413 break;
414
415 default:
3935b9e5 416 //std::cerr << "A:" << CYCXString(child) << std::endl;
2e43a0b0
JF
417 break;
418 }
419 }));
420
421 if (label.empty()) {
422 label = spelling;
423 label = '_' + label;
424 } else if (label[0] != '_')
b7854baa
JF
425 goto skip;
426
2e43a0b0
JF
427 if (code == NULL) {
428 CXType type(clang_getCursorType(cursor));
429 value << "*(typedef " << CYCXString(clang_getTypeSpelling(type)) << ").pointerTo()(dlsym(RTLD_DEFAULT,'" << label.substr(1) << "'))";
430 } else {
431 CYOptions options;
432 CYOutput out(*value.rdbuf(), options);
433 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters, code));
434 function->Output(out, CYNoBFC);
435 //std::cerr << value.str() << std::endl;
436 }
437 } catch (const CYException &error) {
438 CYPool pool;
439 //std::cerr << error.PoolCString(pool) << std::endl;
440 goto skip;
8d20f0f1
JF
441 } break;
442
443 default: {
444 return CXChildVisit_Recurse;
445 } break;
446 }
447
448 baton.keys[name] = value.str();
449
450 skip:
451 return CXChildVisit_Continue;
452}
453
454int main(int argc, const char *argv[]) {
455 CXIndex index(clang_createIndex(0, 0));
456
457 const char *file(argv[1]);
458
459 unsigned offset(3);
460#if CY_OBJECTIVEC
461 argv[--offset] = "-ObjC++";
462#endif
463
2e43a0b0 464 CXTranslationUnit unit(clang_parseTranslationUnit(index, file, argv + offset, argc - offset, NULL, 0, CXTranslationUnit_DetailedPreprocessingRecord));
8d20f0f1
JF
465
466 for (unsigned i(0), e(clang_getNumDiagnostics(unit)); i != e; ++i) {
467 CXDiagnostic diagnostic(clang_getDiagnostic(unit, i));
468 CYCXString spelling(clang_getDiagnosticSpelling(diagnostic));
469 std::cerr << spelling << std::endl;
470 }
471
472 CYKeyMap keys;
473 CYChildBaton baton(unit, keys);
474 clang_visitChildren(clang_getTranslationUnitCursor(unit), &CYChildVisit, &baton);
475
476 for (CYKeyMap::const_iterator key(keys.begin()); key != keys.end(); ++key) {
477 std::string value(key->second);
478 for (size_t i(0), e(value.size()); i != e; ++i)
479 if (value[i] <= 0 || value[i] >= 0x7f || value[i] == '\n')
480 goto skip;
481 std::cout << key->first << "|\"" << value << "\"" << std::endl;
482 skip:; }
483
484 clang_disposeTranslationUnit(unit);
485 clang_disposeIndex(index);
486
487 return 0;
488}