Ted Kremenek | 87553c4 | 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 | // |
| 10 | // This file defines routines for manipulating CXCursors. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CXCursor.h" |
| 15 | #include "clang/AST/Decl.h" |
Douglas Gregor | c58d05b | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
| 17 | #include "clang/AST/Expr.h" |
Ted Kremenek | c2aa0f1 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | 87553c4 | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | CXCursor cxcursor::MakeCXCursor(CXCursorKind K, Decl *D) { |
Douglas Gregor | c58d05b | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 23 | CXCursor C = { K, { D, 0, 0 } }; |
Ted Kremenek | 87553c4 | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 24 | return C; |
| 25 | } |
| 26 | |
| 27 | CXCursor cxcursor::MakeCXCursor(CXCursorKind K, Decl *D, Stmt *S) { |
| 28 | assert(clang_isReference(K)); |
Douglas Gregor | c58d05b | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 29 | CXCursor C = { K, { D, S, 0 } }; |
Ted Kremenek | 87553c4 | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 30 | return C; |
| 31 | } |
| 32 | |
Ted Kremenek | c2aa0f1 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 33 | static CXCursorKind GetCursorKind(Decl *D) { |
| 34 | switch (D->getKind()) { |
| 35 | case Decl::Function: |
| 36 | return cast<FunctionDecl>(D)->isThisDeclarationADefinition() |
| 37 | ? CXCursor_FunctionDefn : CXCursor_FunctionDecl; |
| 38 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 39 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryDefn; |
| 40 | case Decl::ObjCImplementation: return CXCursor_ObjCClassDefn; |
| 41 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
| 42 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
| 43 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 44 | case Decl::Var: return CXCursor_VarDecl; |
| 45 | default: |
| 46 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 47 | switch (TD->getTagKind()) { |
| 48 | case TagDecl::TK_struct: return CXCursor_StructDecl; |
| 49 | case TagDecl::TK_class: return CXCursor_ClassDecl; |
| 50 | case TagDecl::TK_union: return CXCursor_UnionDecl; |
| 51 | case TagDecl::TK_enum: return CXCursor_EnumDecl; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | llvm_unreachable("Invalid Decl"); |
| 57 | return CXCursor_NotImplemented; |
| 58 | } |
| 59 | |
| 60 | CXCursor cxcursor::MakeCXCursor(Decl *D) { |
| 61 | return MakeCXCursor(GetCursorKind(D), D); |
| 62 | } |
| 63 | |
Douglas Gregor | c58d05b | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 64 | Decl *cxcursor::getCursorDecl(CXCursor Cursor) { |
| 65 | return (Decl *)Cursor.data[0]; |
| 66 | } |
| 67 | |
| 68 | Expr *cxcursor::getCursorExpr(CXCursor Cursor) { |
| 69 | return dyn_cast_or_null<Expr>(getCursorStmt(Cursor)); |
| 70 | } |
| 71 | |
| 72 | Stmt *cxcursor::getCursorStmt(CXCursor Cursor) { |
| 73 | return (Stmt *)Cursor.data[1]; |
| 74 | } |
| 75 | |
| 76 | Decl *cxcursor::getCursorReferringDecl(CXCursor Cursor) { |
| 77 | return (Decl *)Cursor.data[2]; |
| 78 | } |
| 79 | |
| 80 | NamedDecl *cxcursor::getCursorInterfaceParent(CXCursor Cursor) { |
| 81 | assert(Cursor.kind == CXCursor_ObjCClassRef); |
| 82 | assert(isa<ObjCInterfaceDecl>(getCursorDecl(Cursor))); |
| 83 | // FIXME: This is a hack (storing the parent decl in the stmt slot). |
| 84 | return static_cast<NamedDecl *>(Cursor.data[1]); |
| 85 | } |
| 86 | |
| 87 | bool cxcursor::operator==(CXCursor X, CXCursor Y) { |
| 88 | return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] && |
| 89 | X.data[2] == Y.data[2]; |
| 90 | } |