+static CYUTF8String CYCXPoolUTF8Range(CYPool &pool, CXSourceRange range) {
+ CYCXPosition<> start(clang_getRangeStart(range));
+ CYCXPosition<> end(clang_getRangeEnd(range));
+ CYCXString file(start.file_);
+ _assert(file == CYCXString(end.file_));
+
+ CYPool temp;
+ size_t size;
+ char *data(static_cast<char *>(CYPoolFile(temp, file, &size)));
+ _assert(start.offset_ <= size && end.offset_ <= size && start.offset_ <= end.offset_);
+
+ CYUTF8String code;
+ code.size = end.offset_ - start.offset_;
+ code.data = pool.strndup(data + start.offset_, code.size);
+ return code;
+}
+
+static CYExpression *CYTranslateExpression(CXTranslationUnit unit, CXCursor cursor) {
+ switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
+ case CXCursor_CallExpr: {
+ CYExpression *function(NULL);
+ CYList<CYArgument> arguments;
+ CYForChild(cursor, fun([&](CXCursor child) {
+ CYExpression *expression(CYTranslateExpression(unit, child));
+ if (function == NULL)
+ function = expression;
+ else
+ arguments->*$C_(expression);
+ }));
+ return $C(function, arguments);
+ } break;
+
+ case CXCursor_DeclRefExpr: {
+ return $V(CYCXString(cursor).Pool($pool));
+ } break;
+
+ case CXCursor_IntegerLiteral: {
+ // libclang doesn't provide any reasonable way to do this
+ // note: clang_tokenize doesn't work if this is a macro
+ // the token range starts inside the macro but ends after it
+ // the tokenizer freaks out and either fails with 0 tokens
+ // or returns some massive number of tokens ending here :/
+
+ CYUTF8String token(CYCXPoolUTF8Range($pool, clang_getCursorExtent(cursor)));
+ double value(CYCastDouble(token));
+ if (std::isnan(value))
+ return $V(token.data);
+ return $ CYNumber(value);
+ } break;
+
+ case CXCursor_CStyleCastExpr:
+ // XXX: most of the time, this is a "NoOp" integer cast; but we should check it
+
+ case CXCursor_UnexposedExpr:
+ // there is a very high probability that this is actually an "ImplicitCastExpr"
+ // "Douglas Gregor" <dgregor@apple.com> err'd on the incorrect side of this one
+ // http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20110926/046998.html
+
+ case CXCursor_ParenExpr: {
+ CYExpression *pass(NULL);
+ CYOneChild(cursor, fun([&](CXCursor child) {
+ pass = CYTranslateExpression(unit, child);
+ }));
+ return pass;
+ } break;
+
+ default:
+ //std::cerr << "E:" << CYCXString(kind) << std::endl;
+ _assert(false);
+ }
+}
+
+static CYStatement *CYTranslateStatement(CXTranslationUnit unit, CXCursor cursor) {
+ switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
+ case CXCursor_ReturnStmt: {
+ CYExpression *value(NULL);
+ CYOneChild(cursor, fun([&](CXCursor child) {
+ value = CYTranslateExpression(unit, child);
+ }));
+ return $ CYReturn(value);
+ } break;
+
+ default:
+ //std::cerr << "S:" << CYCXString(kind) << std::endl;
+ _assert(false);
+ }
+}
+
+static CYStatement *CYTranslateBlock(CXTranslationUnit unit, CXCursor cursor) {
+ CYList<CYStatement> statements;
+ CYForChild(cursor, fun([&](CXCursor child) {
+ statements->*CYTranslateStatement(unit, child);
+ }));
+ return $ CYBlock(statements);
+}
+