1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
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.
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/>.
29 #include <clang-c/Index.h>
31 #include "Functor.hpp"
32 #include "Replace.hpp"
35 static CXChildVisitResult
CYVisit(CXCursor cursor
, CXCursor parent
, CXClientData arg
) {
36 (*reinterpret_cast<const Functor
<void (CXCursor
)> *>(arg
))(cursor
);
37 return CXChildVisit_Continue
;
40 static unsigned CYForChild(CXCursor cursor
, const Functor
<void (CXCursor
)> &visitor
) {
41 return clang_visitChildren(cursor
, &CYVisit
, const_cast<void *>(static_cast<const void *>(&visitor
)));
44 static bool CYOneChild(CXCursor cursor
, const Functor
<void (CXCursor
)> &visitor
) {
46 CYForChild(cursor
, fun([&](CXCursor child
) {
57 CYCXString(CXString value
) :
62 CYCXString(CXCursor cursor
) :
63 value_(clang_getCursorSpelling(cursor
))
67 CYCXString(CXCursorKind kind
) :
68 value_(clang_getCursorKindSpelling(kind
))
72 CYCXString(CXFile file
) :
73 value_(clang_getFileName(file
))
77 CYCXString(CXTranslationUnit unit
, CXToken token
) :
78 value_(clang_getTokenSpelling(unit
, token
))
83 clang_disposeString(value_
);
86 operator const char *() const {
87 return clang_getCString(value_
);
90 const char *Pool(CYPool
&pool
) const {
91 return pool
.strdup(*this);
94 bool operator ==(const char *rhs
) const {
95 const char *lhs(*this);
96 return lhs
== rhs
|| strcmp(lhs
, rhs
) == 0;
100 template <void (&clang_get_Location
)(CXSourceLocation
, CXFile
*, unsigned *, unsigned *, unsigned *) = clang_getSpellingLocation
>
101 struct CYCXPosition
{
107 CYCXPosition(CXSourceLocation location
) {
108 clang_get_Location(location
, &file_
, &line_
, &column_
, &offset_
);
111 CYCXPosition(CXTranslationUnit unit
, CXToken token
) :
112 CYCXPosition(clang_getTokenLocation(unit
, token
))
116 CXSourceLocation
Get(CXTranslationUnit unit
) const {
117 return clang_getLocation(unit
, file_
, line_
, column_
);
121 template <void (&clang_get_Location
)(CXSourceLocation
, CXFile
*, unsigned *, unsigned *, unsigned *)>
122 std::ostream
&operator <<(std::ostream
&out
, const CYCXPosition
<clang_get_Location
> &position
) {
123 if (position
.file_
!= NULL
)
124 out
<< "[" << CYCXString(position
.file_
) << "]:";
125 out
<< position
.line_
<< ":" << position
.column_
<< "@" << position
.offset_
;
130 unsigned priority_
= 0;
136 typedef std::map
<std::string
, CYKey
> CYKeyMap
;
138 struct CYChildBaton
{
139 CXTranslationUnit unit
;
142 CYChildBaton(CXTranslationUnit unit
, CYKeyMap
&keys
) :
151 CXTranslationUnit unit_
;
157 CYTokens(CXTranslationUnit unit
, CXSourceRange range
) :
160 clang_tokenize(unit_
, range
, &tokens_
, &count_
);
163 // libclang's tokenizer is horribly broken and returns "extra" tokens.
164 // this code goes back through the tokens and filters for good ones :/
166 CYCXPosition
<> end(clang_getRangeEnd(range
));
167 CYCXString
file(end
.file_
);
169 for (valid_
= 0; valid_
!= count_
; ++valid_
) {
170 CYCXPosition
<> position(unit
, tokens_
[valid_
]);
171 _assert(CYCXString(position
.file_
) == file
);
172 if (position
.offset_
>= end
.offset_
)
177 CYTokens(CXTranslationUnit unit
, CXCursor cursor
) :
178 CYTokens(unit
, clang_getCursorExtent(cursor
))
183 clang_disposeTokens(unit_
, tokens_
, count_
);
186 operator CXToken
*() const {
190 size_t size() const {
195 static CYUTF8String
CYCXPoolUTF8Range(CYPool
&pool
, CXSourceRange range
) {
196 CYCXPosition
<> start(clang_getRangeStart(range
));
197 CYCXPosition
<> end(clang_getRangeEnd(range
));
198 CYCXString
file(start
.file_
);
199 _assert(file
== CYCXString(end
.file_
));
203 char *data(static_cast<char *>(CYPoolFile(temp
, file
, &size
)));
204 _assert(start
.offset_
<= size
&& end
.offset_
<= size
&& start
.offset_
<= end
.offset_
);
207 code
.size
= end
.offset_
- start
.offset_
;
208 code
.data
= pool
.strndup(data
+ start
.offset_
, code
.size
);
212 static CYExpression
*CYTranslateExpression(CXTranslationUnit unit
, CXCursor cursor
) {
213 switch (CXCursorKind kind
= clang_getCursorKind(cursor
)) {
214 case CXCursor_CallExpr
: {
215 CYExpression
*function(NULL
);
216 CYList
<CYArgument
> arguments
;
217 CYForChild(cursor
, fun([&](CXCursor child
) {
218 CYExpression
*expression(CYTranslateExpression(unit
, child
));
219 if (function
== NULL
)
220 function
= expression
;
222 arguments
->*$
C_(expression
);
224 return $
C(function
, arguments
);
227 case CXCursor_DeclRefExpr
: {
228 return $
V(CYCXString(cursor
).Pool($pool
));
231 case CXCursor_IntegerLiteral
: {
232 // libclang doesn't provide any reasonable way to do this
233 // note: clang_tokenize doesn't work if this is a macro
234 // the token range starts inside the macro but ends after it
235 // the tokenizer freaks out and either fails with 0 tokens
236 // or returns some massive number of tokens ending here :/
238 CYUTF8String
token(CYCXPoolUTF8Range($pool
, clang_getCursorExtent(cursor
)));
239 double value(CYCastDouble(token
));
240 if (std::isnan(value
))
241 return $
V(token
.data
);
242 return $
CYNumber(value
);
245 case CXCursor_CStyleCastExpr
:
246 // XXX: most of the time, this is a "NoOp" integer cast; but we should check it
248 case CXCursor_UnexposedExpr
:
249 // there is a very high probability that this is actually an "ImplicitCastExpr"
250 // "Douglas Gregor" <dgregor@apple.com> err'd on the incorrect side of this one
251 // http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20110926/046998.html
253 case CXCursor_ParenExpr
: {
254 CYExpression
*pass(NULL
);
255 CYOneChild(cursor
, fun([&](CXCursor child
) {
256 pass
= CYTranslateExpression(unit
, child
);
262 //std::cerr << "E:" << CYCXString(kind) << std::endl;
267 static CYStatement
*CYTranslateStatement(CXTranslationUnit unit
, CXCursor cursor
) {
268 switch (CXCursorKind kind
= clang_getCursorKind(cursor
)) {
269 case CXCursor_ReturnStmt
: {
270 CYExpression
*value(NULL
);
271 CYOneChild(cursor
, fun([&](CXCursor child
) {
272 value
= CYTranslateExpression(unit
, child
);
274 return $
CYReturn(value
);
278 //std::cerr << "S:" << CYCXString(kind) << std::endl;
283 static CYStatement
*CYTranslateBlock(CXTranslationUnit unit
, CXCursor cursor
) {
284 CYList
<CYStatement
> statements
;
285 CYForChild(cursor
, fun([&](CXCursor child
) {
286 statements
->*CYTranslateStatement(unit
, child
);
288 return $
CYBlock(statements
);
291 static CYTypedIdentifier
*CYDecodeType(CXType type
);
292 static void CYParseType(CXType type
, CYTypedIdentifier
*typed
);
294 static CYTypedIdentifier
*CYDecodeType(CXType type
, const CYCXString
&identifier
) {
295 CYTypedIdentifier
*typed(CYDecodeType(type
));
296 typed
->identifier_
= $
CYIdentifier(identifier
.Pool($pool
));
300 static void CYParseCursor(CXType type
, CXCursor cursor
, CYTypedIdentifier
*typed
) {
301 CYCXString
spelling(cursor
);
303 switch (CXCursorKind kind
= clang_getCursorKind(cursor
)) {
304 case CXCursor_EnumDecl
:
305 if (spelling
[0] != '\0')
306 // XXX: should we have a special enum keyword?
307 typed
->specifier_
= $
CYTypeVariable($
I(spelling
.Pool($pool
)));
309 // XXX: maybe replace with "enum : int" instead of "int"
310 CYParseType(clang_getEnumDeclIntegerType(cursor
), typed
);
313 case CXCursor_StructDecl
: {
314 if (spelling
[0] != '\0')
315 typed
->specifier_
= $
CYTypeReference($
I(spelling
.Pool($pool
)));
317 CYList
<CYTypeStructField
> fields
;
318 CYForChild(cursor
, fun([&](CXCursor child
) {
319 if (clang_getCursorKind(child
) == CXCursor_FieldDecl
) {
320 CYTypedIdentifier
*field(CYDecodeType(clang_getCursorType(child
), child
));
321 fields
->*$
CYTypeStructField(field
);
325 typed
->specifier_
= $
CYTypeStruct(NULL
, $
CYStructTail(fields
));
329 case CXCursor_UnionDecl
: {
334 std::cerr
<< "C:" << CYCXString(kind
) << std::endl
;
340 static CYTypedParameter
*CYParseSignature(CXType type
, CYTypedIdentifier
*typed
) {
341 CYParseType(clang_getResultType(type
), typed
);
342 CYList
<CYTypedParameter
> parameters
;
343 for (int i(0), e(clang_getNumArgTypes(type
)); i
!= e
; ++i
)
344 parameters
->*$
CYTypedParameter(CYDecodeType(clang_getArgType(type
, i
)));
348 static void CYParseFunction(CXType type
, CYTypedIdentifier
*typed
) {
349 typed
= typed
->Modify($
CYTypeFunctionWith(clang_isFunctionTypeVariadic(type
), CYParseSignature(type
, typed
)));
352 static void CYParseType(CXType type
, CYTypedIdentifier
*typed
) {
353 switch (CXTypeKind kind
= type
.kind
) {
354 case CXType_Unexposed
: {
355 CXType
result(clang_getResultType(type
));
356 if (result
.kind
== CXType_Invalid
)
357 CYParseCursor(type
, clang_getTypeDeclaration(type
), typed
);
359 // clang marks function pointers as Unexposed but still supports them
360 CYParseFunction(type
, typed
);
363 case CXType_Bool
: typed
->specifier_
= $
CYTypeVariable("bool"); break;
364 case CXType_Float
: typed
->specifier_
= $
CYTypeVariable("float"); break;
365 case CXType_Double
: typed
->specifier_
= $
CYTypeVariable("double"); break;
367 case CXType_Char_U
: typed
->specifier_
= $
CYTypeCharacter(CYTypeNeutral
); break;
368 case CXType_Char_S
: typed
->specifier_
= $
CYTypeCharacter(CYTypeNeutral
); break;
369 case CXType_SChar
: typed
->specifier_
= $
CYTypeCharacter(CYTypeSigned
); break;
370 case CXType_UChar
: typed
->specifier_
= $
CYTypeCharacter(CYTypeUnsigned
); break;
372 case CXType_Short
: typed
->specifier_
= $
CYTypeIntegral(CYTypeSigned
, 0); break;
373 case CXType_UShort
: typed
->specifier_
= $
CYTypeIntegral(CYTypeUnsigned
, 0); break;
375 case CXType_Int
: typed
->specifier_
= $
CYTypeIntegral(CYTypeSigned
, 1); break;
376 case CXType_UInt
: typed
->specifier_
= $
CYTypeIntegral(CYTypeUnsigned
, 1); break;
378 case CXType_Long
: typed
->specifier_
= $
CYTypeIntegral(CYTypeSigned
, 2); break;
379 case CXType_ULong
: typed
->specifier_
= $
CYTypeIntegral(CYTypeUnsigned
, 2); break;
381 case CXType_LongLong
: typed
->specifier_
= $
CYTypeIntegral(CYTypeSigned
, 3); break;
382 case CXType_ULongLong
: typed
->specifier_
= $
CYTypeIntegral(CYTypeUnsigned
, 3); break;
384 case CXType_Int128
: typed
->specifier_
= $
CYTypeInt128(CYTypeSigned
); break;
385 case CXType_UInt128
: typed
->specifier_
= $
CYTypeInt128(CYTypeUnsigned
); break;
387 case CXType_BlockPointer
: {
388 CXType
pointee(clang_getPointeeType(type
));
389 _assert(!clang_isFunctionTypeVariadic(pointee
));
390 typed
= typed
->Modify($
CYTypeBlockWith(CYParseSignature(pointee
, typed
)));
393 case CXType_ConstantArray
:
394 CYParseType(clang_getArrayElementType(type
), typed
);
395 typed
= typed
->Modify($
CYTypeArrayOf($
D(clang_getArraySize(type
))));
399 typed
->specifier_
= $
CYTypeVariable($pool
.strdup(CYCXString(clang_getTypeSpelling(type
))));
402 case CXType_FunctionProto
:
403 CYParseFunction(type
, typed
);
406 case CXType_IncompleteArray
:
407 // XXX: I should support these :/
412 typed
->specifier_
= $
CYTypeVariable("id");
415 case CXType_ObjCInterface
:
416 typed
->specifier_
= $
CYTypeVariable($pool
.strdup(CYCXString(clang_getTypeSpelling(type
))));
419 case CXType_ObjCObjectPointer
: {
420 CXType
pointee(clang_getPointeeType(type
));
421 if (pointee
.kind
!= CXType_Unexposed
) {
422 CYParseType(pointee
, typed
);
423 typed
= typed
->Modify($
CYTypePointerTo());
425 // Clang seems to have internal typedefs for id and Class that are awkward
430 typed
->specifier_
= $
CYTypeVariable("SEL");
434 CYParseType(clang_getPointeeType(type
), typed
);
435 typed
= typed
->Modify($
CYTypePointerTo());
439 typed
->specifier_
= $
CYTypeReference($
I($pool
.strdup(CYCXString(clang_getTypeSpelling(type
)))));
443 // use the declaration in order to isolate the name of the typedef itself
444 typed
->specifier_
= $
CYTypeVariable($pool
.strdup(CYCXString(clang_getTypeDeclaration(type
))));
452 typed
->specifier_
= $
CYTypeVoid();
456 std::cerr
<< "T:" << CYCXString(clang_getTypeKindSpelling(kind
)) << std::endl
;
457 std::cerr
<< "_: " << CYCXString(clang_getTypeSpelling(type
)) << std::endl
;
461 if (clang_isConstQualifiedType(type
))
462 typed
= typed
->Modify($
CYTypeConstant());
465 static CYTypedIdentifier
*CYDecodeType(CXType type
) {
466 CYTypedIdentifier
*typed($
CYTypedIdentifier(NULL
));
467 CYParseType(type
, typed
);
471 static CXChildVisitResult
CYChildVisit(CXCursor cursor
, CXCursor parent
, CXClientData arg
) {
472 CYChildBaton
&baton(*static_cast<CYChildBaton
*>(arg
));
473 CXTranslationUnit
&unit(baton
.unit
);
475 CYCXString
spelling(cursor
);
476 std::string
name(spelling
);
477 std::ostringstream value
;
478 unsigned priority(2);
481 /*CXSourceLocation location(clang_getCursorLocation(cursor));
482 CYCXPosition<> position(location);
483 std::cerr << spelling << " " << position << std::endl;*/
485 switch (CXCursorKind kind
= clang_getCursorKind(cursor
)) {
486 case CXCursor_EnumConstantDecl
: {
487 value
<< clang_getEnumConstantDeclValue(cursor
);
490 case CXCursor_MacroDefinition
: try {
491 CXSourceRange
range(clang_getCursorExtent(cursor
));
492 CYTokens
tokens(unit
, range
);
493 _assert(tokens
.size() != 0);
495 CXCursor cursors
[tokens
.size()];
496 clang_annotateTokens(unit
, tokens
, tokens
.size(), cursors
);
499 CYList
<CYFunctionParameter
> parameters
;
502 if (tokens
.size() != 1) {
503 CYCXPosition
<> start(clang_getRangeStart(range
));
504 CYCXString
first(unit
, tokens
[offset
]);
506 CYCXPosition
<> paren(unit
, tokens
[offset
]);
507 if (start
.offset_
+ strlen(spelling
) == paren
.offset_
) {
509 _assert(++offset
!= tokens
.size());
510 CYCXString
token(unit
, tokens
[offset
]);
511 parameters
->*$
P($
B($
I(token
.Pool($pool
))));
512 _assert(++offset
!= tokens
.size());
513 CYCXString
comma(unit
, tokens
[offset
]);
516 _assert(comma
== ",");
523 std::ostringstream body
;
524 for (unsigned i(offset
); i
!= tokens
.size(); ++i
) {
525 CYCXString
token(unit
, tokens
[i
]);
535 CYOutput
out(*value
.rdbuf(), options
);
536 out
<< '(' << "function" << '(';
539 out
<< "return" << ' ';
541 out
<< ';' << '}' << ')';
543 } catch (const CYException
&error
) {
545 //std::cerr << error.PoolCString(pool) << std::endl;
549 case CXCursor_StructDecl
: {
550 if (spelling
[0] == '\0')
552 if (!clang_isCursorDefinition(cursor
))
555 std::ostringstream types
;
556 std::ostringstream names
;
558 CYForChild(cursor
, fun([&](CXCursor child
) {
559 if (clang_getCursorKind(child
) == CXCursor_FieldDecl
) {
560 CXType
type(clang_getCursorType(child
));
561 types
<< "(typedef " << CYCXString(clang_getTypeSpelling(type
)) << "),";
562 names
<< "'" << CYCXString(child
) << "',";
566 value
<< "new Type([" << types
.str() << "],[" << names
.str() << "]).withName(\"" << name
<< "\")";
570 case CXCursor_TypedefDecl
: try {
573 CYTypedIdentifier
*typed(CYDecodeType(clang_getTypedefDeclUnderlyingType(cursor
)));
574 if (typed
->specifier_
== NULL
)
575 value
<< "(typedef " << CYCXString(clang_getTypeSpelling(clang_getTypedefDeclUnderlyingType(cursor
))) << ")";
578 CYOutput
out(*value
.rdbuf(), options
);
579 CYTypeExpression(typed
).Output(out
, CYNoBFC
);
581 } catch (const CYException
&error
) {
583 //std::cerr << error.PoolCString(pool) << std::endl;
587 case CXCursor_FunctionDecl
:
588 case CXCursor_VarDecl
: try {
591 CYList
<CYFunctionParameter
> parameters
;
592 CYStatement
*code(NULL
);
596 CYForChild(cursor
, fun([&](CXCursor child
) {
597 switch (CXCursorKind kind
= clang_getCursorKind(child
)) {
598 case CXCursor_AsmLabelAttr
:
599 label
= CYCXString(child
);
602 case CXCursor_CompoundStmt
:
603 code
= CYTranslateBlock(unit
, child
);
606 case CXCursor_ParmDecl
:
607 parameters
->*$
P($
B($
I(CYCXString(child
).Pool($pool
))));
610 case CXCursor_IntegerLiteral
:
611 case CXCursor_ObjCClassRef
:
612 case CXCursor_TypeRef
:
613 case CXCursor_UnexposedAttr
:
617 //std::cerr << "A:" << CYCXString(child) << std::endl;
625 } else if (label
[0] != '_')
629 CXType
type(clang_getCursorType(cursor
));
630 value
<< "*(typedef " << CYCXString(clang_getTypeSpelling(type
)) << ").pointerTo()(dlsym(RTLD_DEFAULT,'" << label
.substr(1) << "'))";
633 CYOutput
out(*value
.rdbuf(), options
);
634 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters
, code
));
635 function
->Output(out
, CYNoBFC
);
636 //std::cerr << value.str() << std::endl;
638 } catch (const CYException
&error
) {
640 //std::cerr << error.PoolCString(pool) << std::endl;
645 return CXChildVisit_Recurse
;
650 CYKey
&key(baton
.keys
[name
]);
651 if (key
.priority_
<= priority
) {
652 key
.priority_
= priority
;
653 key
.code_
= value
.str();
659 return CXChildVisit_Continue
;
662 int main(int argc
, const char *argv
[]) {
663 CXIndex
index(clang_createIndex(0, 0));
665 const char *file(argv
[1]);
669 argv
[--offset
] = "-ObjC++";
672 CXTranslationUnit
unit(clang_parseTranslationUnit(index
, file
, argv
+ offset
, argc
- offset
, NULL
, 0, CXTranslationUnit_DetailedPreprocessingRecord
));
674 for (unsigned i(0), e(clang_getNumDiagnostics(unit
)); i
!= e
; ++i
) {
675 CXDiagnostic
diagnostic(clang_getDiagnostic(unit
, i
));
676 CYCXString
spelling(clang_getDiagnosticSpelling(diagnostic
));
677 std::cerr
<< spelling
<< std::endl
;
681 CYChildBaton
baton(unit
, keys
);
682 clang_visitChildren(clang_getTranslationUnitCursor(unit
), &CYChildVisit
, &baton
);
684 for (CYKeyMap::const_iterator
key(keys
.begin()); key
!= keys
.end(); ++key
) {
685 std::string
code(key
->second
.code_
);
686 for (size_t i(0), e(code
.size()); i
!= e
; ++i
)
687 if (code
[i
] <= 0 || code
[i
] >= 0x7f || code
[i
] == '\n')
689 std::cout
<< key
->first
<< "|" << key
->second
.flags_
<< "\"" << code
<< "\"" << std::endl
;
692 clang_disposeTranslationUnit(unit
);
693 clang_disposeIndex(index
);