X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/b0e6429c745211a7fa3036f22a74e85ae227af2b..5b4dabb2071e2e09cd70a156beed03b12bec477a:/Analyze.cpp diff --git a/Analyze.cpp b/Analyze.cpp index 49ff5cb..f1ef107 100644 --- a/Analyze.cpp +++ b/Analyze.cpp @@ -289,43 +289,45 @@ static CYStatement *CYTranslateBlock(CXTranslationUnit unit, CXCursor cursor) { return $ CYBlock(statements); } -static CYTypedIdentifier *CYDecodeType(CXType type); -static void CYParseType(CXType type, CYTypedIdentifier *typed); +static CYType *CYDecodeType(CXType type); +static void CYParseType(CXType type, CYType *typed); -static CYTypedIdentifier *CYDecodeType(CXType type, const CYCXString &identifier) { - CYTypedIdentifier *typed(CYDecodeType(type)); - typed->identifier_ = $ CYIdentifier(identifier.Pool($pool)); - return typed; +static void CYParseEnumeration(CXCursor cursor, CYType *typed) { + CYList constants; + + CYForChild(cursor, fun([&](CXCursor child) { + if (clang_getCursorKind(child) == CXCursor_EnumConstantDecl) + constants->*$ CYEnumConstant($I($pool.strdup(CYCXString(child))), $D(clang_getEnumConstantDeclValue(child))); + })); + + CYType *integer(CYDecodeType(clang_getEnumDeclIntegerType(cursor))); + typed->specifier_ = $ CYTypeEnum(NULL, integer->specifier_, constants); } -static void CYParseStructure(CXCursor cursor, CYTypedIdentifier *typed) { +static void CYParseStructure(CXCursor cursor, CYType *typed) { CYList fields; CYForChild(cursor, fun([&](CXCursor child) { - if (clang_getCursorKind(child) == CXCursor_FieldDecl) { - CYTypedIdentifier *field(CYDecodeType(clang_getCursorType(child), child)); - fields->*$ CYTypeStructField(field); - } + if (clang_getCursorKind(child) == CXCursor_FieldDecl) + fields->*$ CYTypeStructField(CYDecodeType(clang_getCursorType(child)), $I(CYCXString(child).Pool($pool))); })); typed->specifier_ = $ CYTypeStruct(NULL, $ CYStructTail(fields)); } -static void CYParseCursor(CXType type, CXCursor cursor, CYTypedIdentifier *typed) { +static void CYParseCursor(CXType type, CXCursor cursor, CYType *typed) { CYCXString spelling(cursor); switch (CXCursorKind kind = clang_getCursorKind(cursor)) { case CXCursor_EnumDecl: if (spelling[0] != '\0') - // XXX: should we have a special enum keyword? - typed->specifier_ = $ CYTypeVariable($I(spelling.Pool($pool))); + typed->specifier_ = $ CYTypeReference(CYTypeReferenceEnum, $I(spelling.Pool($pool))); else - // XXX: maybe replace with "enum : int" instead of "int" - CYParseType(clang_getEnumDeclIntegerType(cursor), typed); + CYParseEnumeration(cursor, typed); break; case CXCursor_StructDecl: { if (spelling[0] != '\0') - typed->specifier_ = $ CYTypeReference($I(spelling.Pool($pool))); + typed->specifier_ = $ CYTypeReference(CYTypeReferenceStruct, $I(spelling.Pool($pool))); else CYParseStructure(cursor, typed); } break; @@ -341,19 +343,19 @@ static void CYParseCursor(CXType type, CXCursor cursor, CYTypedIdentifier *typed } } -static CYTypedParameter *CYParseSignature(CXType type, CYTypedIdentifier *typed) { +static CYTypedParameter *CYParseSignature(CXType type, CYType *typed) { CYParseType(clang_getResultType(type), typed); CYList parameters; for (int i(0), e(clang_getNumArgTypes(type)); i != e; ++i) - parameters->*$ CYTypedParameter(CYDecodeType(clang_getArgType(type, i))); + parameters->*$ CYTypedParameter(CYDecodeType(clang_getArgType(type, i)), NULL); return parameters; } -static void CYParseFunction(CXType type, CYTypedIdentifier *typed) { +static void CYParseFunction(CXType type, CYType *typed) { typed = typed->Modify($ CYTypeFunctionWith(clang_isFunctionTypeVariadic(type), CYParseSignature(type, typed))); } -static void CYParseType(CXType type, CYTypedIdentifier *typed) { +static void CYParseType(CXType type, CYType *typed) { switch (CXTypeKind kind = type.kind) { case CXType_Unexposed: { CXType result(clang_getResultType(type)); @@ -408,8 +410,9 @@ static void CYParseType(CXType type, CYTypedIdentifier *typed) { break; case CXType_IncompleteArray: - // XXX: I should support these :/ - _assert(false); + // XXX: I probably should not decay to Pointer + CYParseType(clang_getArrayElementType(type), typed); + typed = typed->Modify($ CYTypePointerTo()); break; case CXType_ObjCClass: @@ -444,7 +447,7 @@ static void CYParseType(CXType type, CYTypedIdentifier *typed) { break; case CXType_Record: - typed->specifier_ = $ CYTypeReference($I($pool.strdup(CYCXString(clang_getTypeSpelling(type))))); + typed->specifier_ = $ CYTypeReference(CYTypeReferenceStruct, $I($pool.strdup(CYCXString(clang_getTypeSpelling(type))))); break; case CXType_Typedef: @@ -470,8 +473,8 @@ static void CYParseType(CXType type, CYTypedIdentifier *typed) { typed = typed->Modify($ CYTypeConstant()); } -static CYTypedIdentifier *CYDecodeType(CXType type) { - CYTypedIdentifier *typed($ CYTypedIdentifier(NULL)); +static CYType *CYDecodeType(CXType type) { + CYType *typed($ CYType(NULL)); CYParseType(type, typed); return typed; } @@ -480,6 +483,7 @@ static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClien CYChildBaton &baton(*static_cast(arg)); CXTranslationUnit &unit(baton.unit); + CXChildVisitResult result(CXChildVisit_Continue); CYCXString spelling(cursor); std::string name(spelling); std::ostringstream value; @@ -495,6 +499,31 @@ static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClien value << clang_getEnumConstantDeclValue(cursor); } break; + case CXCursor_EnumDecl: { + if (spelling[0] == '\0') + goto skip; + // XXX: this was blindly copied from StructDecl + if (!clang_isCursorDefinition(cursor)) + priority = 1; + + CYLocalPool pool; + + CYType typed; + CYParseEnumeration(cursor, &typed); + + CYOptions options; + CYOutput out(*value.rdbuf(), options); + CYTypeExpression(&typed).Output(out, CYNoBFC); + + value << ".withName(\"" << name << "\")"; + name = "$cye" + name; + flags = CYBridgeType; + + // the enum constants are implemented separately *also* + // XXX: maybe move output logic to function we can call + result = CXChildVisit_Recurse; + } break; + case CXCursor_MacroDefinition: { CXSourceRange range(clang_getCursorExtent(cursor)); CYTokens tokens(unit, range); @@ -558,7 +587,7 @@ static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClien CYLocalPool pool; - CYTypedIdentifier typed(NULL); + CYType typed; CYParseStructure(cursor, &typed); CYOptions options; @@ -566,14 +595,14 @@ static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClien CYTypeExpression(&typed).Output(out, CYNoBFC); value << ".withName(\"" << name << "\")"; - name += "$cy"; + name = "$cys" + name; flags = CYBridgeType; } break; case CXCursor_TypedefDecl: { CYLocalPool local; - CYTypedIdentifier *typed(CYDecodeType(clang_getTypedefDeclUnderlyingType(cursor))); + CYType *typed(CYDecodeType(clang_getTypedefDeclUnderlyingType(cursor))); if (typed->specifier_ == NULL) value << "(typedef " << CYCXString(clang_getTypeSpelling(clang_getTypedefDeclUnderlyingType(cursor))) << ")"; else { @@ -636,9 +665,10 @@ static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClien } } break; - default: { - return CXChildVisit_Recurse; - } break; + default: + result = CXChildVisit_Recurse; + goto skip; + break; } { CYKey &key(baton.keys[name]); if (key.priority_ <= priority) { @@ -652,7 +682,7 @@ static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClien } skip: - return CXChildVisit_Continue; + return result; } int main(int argc, const char *argv[]) {