Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 1 | //===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame^] | 10 | // This file defines routines for manipulating CXCursors. It should be the |
| 11 | // only file that has internal knowledge of the encoding of the data in |
| 12 | // CXCursor. |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "CXCursor.h" |
| 17 | #include "clang/AST/Decl.h" |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/Expr.h" |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | CXCursor cxcursor::MakeCXCursor(CXCursorKind K, Decl *D) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 25 | CXCursor C = { K, { D, 0, 0 } }; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 26 | return C; |
| 27 | } |
| 28 | |
| 29 | CXCursor cxcursor::MakeCXCursor(CXCursorKind K, Decl *D, Stmt *S) { |
| 30 | assert(clang_isReference(K)); |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 31 | CXCursor C = { K, { D, S, 0 } }; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 32 | return C; |
| 33 | } |
| 34 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 35 | static CXCursorKind GetCursorKind(Decl *D) { |
| 36 | switch (D->getKind()) { |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 37 | case Decl::Enum: return CXCursor_EnumDecl; |
| 38 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 39 | case Decl::Field: return CXCursor_FieldDecl; |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 40 | case Decl::Function: |
| 41 | return cast<FunctionDecl>(D)->isThisDeclarationADefinition() |
| 42 | ? CXCursor_FunctionDefn : CXCursor_FunctionDecl; |
| 43 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 44 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryDefn; |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 45 | case Decl::ObjCClass: |
| 46 | // FIXME |
| 47 | return CXCursor_NotImplemented; |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 48 | case Decl::ObjCImplementation: return CXCursor_ObjCClassDefn; |
| 49 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 50 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| 51 | case Decl::ObjCMethod: |
| 52 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 53 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
Ted Kremenek | 10fa3cc | 2010-01-16 02:08:29 +0000 | [diff] [blame] | 54 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 55 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 56 | case Decl::ParmVar: return CXCursor_ParmDecl; |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 57 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 58 | case Decl::Var: return CXCursor_VarDecl; |
| 59 | default: |
| 60 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 61 | switch (TD->getTagKind()) { |
| 62 | case TagDecl::TK_struct: return CXCursor_StructDecl; |
| 63 | case TagDecl::TK_class: return CXCursor_ClassDecl; |
| 64 | case TagDecl::TK_union: return CXCursor_UnionDecl; |
| 65 | case TagDecl::TK_enum: return CXCursor_EnumDecl; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | llvm_unreachable("Invalid Decl"); |
| 71 | return CXCursor_NotImplemented; |
| 72 | } |
| 73 | |
| 74 | CXCursor cxcursor::MakeCXCursor(Decl *D) { |
| 75 | return MakeCXCursor(GetCursorKind(D), D); |
| 76 | } |
| 77 | |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame^] | 78 | CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super, |
| 79 | SourceLocation Loc) { |
| 80 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 81 | CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, 0 } }; |
| 82 | return C; |
| 83 | } |
| 84 | |
| 85 | std::pair<ObjCInterfaceDecl *, SourceLocation> |
| 86 | cxcursor::getCursorObjCSuperClassRef(CXCursor C) { |
| 87 | assert(C.kind == CXCursor_ObjCSuperClassRef); |
| 88 | return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]), |
| 89 | SourceLocation::getFromRawEncoding( |
| 90 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 91 | } |
| 92 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 93 | Decl *cxcursor::getCursorDecl(CXCursor Cursor) { |
| 94 | return (Decl *)Cursor.data[0]; |
| 95 | } |
| 96 | |
| 97 | Expr *cxcursor::getCursorExpr(CXCursor Cursor) { |
| 98 | return dyn_cast_or_null<Expr>(getCursorStmt(Cursor)); |
| 99 | } |
| 100 | |
| 101 | Stmt *cxcursor::getCursorStmt(CXCursor Cursor) { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame^] | 102 | if (Cursor.kind == CXCursor_ObjCSuperClassRef) |
| 103 | return 0; |
| 104 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 105 | return (Stmt *)Cursor.data[1]; |
| 106 | } |
| 107 | |
| 108 | Decl *cxcursor::getCursorReferringDecl(CXCursor Cursor) { |
| 109 | return (Decl *)Cursor.data[2]; |
| 110 | } |
| 111 | |
| 112 | NamedDecl *cxcursor::getCursorInterfaceParent(CXCursor Cursor) { |
| 113 | assert(Cursor.kind == CXCursor_ObjCClassRef); |
| 114 | assert(isa<ObjCInterfaceDecl>(getCursorDecl(Cursor))); |
| 115 | // FIXME: This is a hack (storing the parent decl in the stmt slot). |
| 116 | return static_cast<NamedDecl *>(Cursor.data[1]); |
| 117 | } |
| 118 | |
| 119 | bool cxcursor::operator==(CXCursor X, CXCursor Y) { |
| 120 | return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] && |
| 121 | X.data[2] == Y.data[2]; |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame^] | 122 | } |