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 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame^] | 29 | CXCursor cxcursor::MakeCXCursor(CXCursorKind K, Decl *D, Stmt *S, |
| 30 | ASTContext &Context) { |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 31 | assert(clang_isReference(K)); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame^] | 32 | CXCursor C = { K, { D, S, &Context } }; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 33 | return C; |
| 34 | } |
| 35 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 36 | static CXCursorKind GetCursorKind(Decl *D) { |
| 37 | switch (D->getKind()) { |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 38 | case Decl::Enum: return CXCursor_EnumDecl; |
| 39 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 40 | case Decl::Field: return CXCursor_FieldDecl; |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 41 | case Decl::Function: |
| 42 | return cast<FunctionDecl>(D)->isThisDeclarationADefinition() |
| 43 | ? CXCursor_FunctionDefn : CXCursor_FunctionDecl; |
| 44 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 45 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryDefn; |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 46 | case Decl::ObjCClass: |
| 47 | // FIXME |
| 48 | return CXCursor_NotImplemented; |
Ted Kremenek | 6483a77 | 2010-01-18 22:07:45 +0000 | [diff] [blame] | 49 | case Decl::ObjCForwardProtocol: |
| 50 | // FIXME |
| 51 | return CXCursor_NotImplemented; |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 52 | case Decl::ObjCImplementation: return CXCursor_ObjCClassDefn; |
| 53 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 54 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| 55 | case Decl::ObjCMethod: |
| 56 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 57 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
Ted Kremenek | 10fa3cc | 2010-01-16 02:08:29 +0000 | [diff] [blame] | 58 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 59 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 60 | case Decl::ParmVar: return CXCursor_ParmDecl; |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 61 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 62 | case Decl::Var: return CXCursor_VarDecl; |
| 63 | default: |
| 64 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 65 | switch (TD->getTagKind()) { |
| 66 | case TagDecl::TK_struct: return CXCursor_StructDecl; |
| 67 | case TagDecl::TK_class: return CXCursor_ClassDecl; |
| 68 | case TagDecl::TK_union: return CXCursor_UnionDecl; |
| 69 | case TagDecl::TK_enum: return CXCursor_EnumDecl; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | llvm_unreachable("Invalid Decl"); |
| 75 | return CXCursor_NotImplemented; |
| 76 | } |
| 77 | |
| 78 | CXCursor cxcursor::MakeCXCursor(Decl *D) { |
| 79 | return MakeCXCursor(GetCursorKind(D), D); |
| 80 | } |
| 81 | |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 82 | CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super, |
| 83 | SourceLocation Loc) { |
| 84 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 85 | CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, 0 } }; |
| 86 | return C; |
| 87 | } |
| 88 | |
| 89 | std::pair<ObjCInterfaceDecl *, SourceLocation> |
| 90 | cxcursor::getCursorObjCSuperClassRef(CXCursor C) { |
| 91 | assert(C.kind == CXCursor_ObjCSuperClassRef); |
| 92 | return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]), |
| 93 | SourceLocation::getFromRawEncoding( |
| 94 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 95 | } |
| 96 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 97 | CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super, |
| 98 | SourceLocation Loc) { |
| 99 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 100 | CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, 0 } }; |
| 101 | return C; |
| 102 | } |
| 103 | |
| 104 | std::pair<ObjCProtocolDecl *, SourceLocation> |
| 105 | cxcursor::getCursorObjCProtocolRef(CXCursor C) { |
| 106 | assert(C.kind == CXCursor_ObjCProtocolRef); |
| 107 | return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]), |
| 108 | SourceLocation::getFromRawEncoding( |
| 109 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 110 | } |
| 111 | |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 112 | CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class, |
| 113 | SourceLocation Loc) { |
| 114 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 115 | CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, 0 } }; |
| 116 | return C; |
| 117 | } |
| 118 | |
| 119 | std::pair<ObjCInterfaceDecl *, SourceLocation> |
| 120 | cxcursor::getCursorObjCClassRef(CXCursor C) { |
| 121 | assert(C.kind == CXCursor_ObjCClassRef); |
| 122 | return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]), |
| 123 | SourceLocation::getFromRawEncoding( |
| 124 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 125 | } |
| 126 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 127 | Decl *cxcursor::getCursorDecl(CXCursor Cursor) { |
| 128 | return (Decl *)Cursor.data[0]; |
| 129 | } |
| 130 | |
| 131 | Expr *cxcursor::getCursorExpr(CXCursor Cursor) { |
| 132 | return dyn_cast_or_null<Expr>(getCursorStmt(Cursor)); |
| 133 | } |
| 134 | |
| 135 | Stmt *cxcursor::getCursorStmt(CXCursor Cursor) { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 136 | if (Cursor.kind == CXCursor_ObjCSuperClassRef || |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 137 | Cursor.kind == CXCursor_ObjCProtocolRef || |
| 138 | Cursor.kind == CXCursor_ObjCClassRef) |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 139 | return 0; |
| 140 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 141 | return (Stmt *)Cursor.data[1]; |
| 142 | } |
| 143 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame^] | 144 | ASTContext &cxcursor::getCursorContext(CXCursor Cursor) { |
| 145 | switch (Cursor.kind) { |
| 146 | case CXCursor_TypedefDecl: |
| 147 | case CXCursor_StructDecl: |
| 148 | case CXCursor_UnionDecl: |
| 149 | case CXCursor_ClassDecl: |
| 150 | case CXCursor_EnumDecl: |
| 151 | case CXCursor_FieldDecl: |
| 152 | case CXCursor_EnumConstantDecl: |
| 153 | case CXCursor_FunctionDecl: |
| 154 | case CXCursor_VarDecl: |
| 155 | case CXCursor_ParmDecl: |
| 156 | case CXCursor_ObjCInterfaceDecl: |
| 157 | case CXCursor_ObjCCategoryDecl: |
| 158 | case CXCursor_ObjCProtocolDecl: |
| 159 | case CXCursor_ObjCPropertyDecl: |
| 160 | case CXCursor_ObjCIvarDecl: |
| 161 | case CXCursor_ObjCInstanceMethodDecl: |
| 162 | case CXCursor_ObjCClassMethodDecl: |
| 163 | case CXCursor_FunctionDefn: |
| 164 | case CXCursor_ObjCClassDefn: |
| 165 | case CXCursor_ObjCCategoryDefn: |
| 166 | case CXCursor_ObjCInstanceMethodDefn: |
| 167 | case CXCursor_ObjCClassMethodDefn: |
| 168 | return static_cast<Decl *>(Cursor.data[0])->getASTContext(); |
| 169 | |
| 170 | case CXCursor_ObjCSuperClassRef: |
| 171 | case CXCursor_ObjCProtocolRef: |
| 172 | case CXCursor_ObjCClassRef: |
| 173 | return static_cast<Decl *>(Cursor.data[0])->getASTContext(); |
| 174 | |
| 175 | case CXCursor_ObjCSelectorRef: |
| 176 | case CXCursor_ObjCIvarRef: |
| 177 | case CXCursor_VarRef: |
| 178 | case CXCursor_FunctionRef: |
| 179 | case CXCursor_EnumConstantRef: |
| 180 | case CXCursor_MemberRef: |
| 181 | return *static_cast<ASTContext *>(Cursor.data[2]); |
| 182 | |
| 183 | case CXCursor_InvalidFile: |
| 184 | case CXCursor_NoDeclFound: |
| 185 | case CXCursor_NotImplemented: |
| 186 | llvm_unreachable("No context in an invalid cursor"); |
| 187 | } |
| 188 | |
| 189 | llvm_unreachable("No context available"); |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 192 | bool cxcursor::operator==(CXCursor X, CXCursor Y) { |
| 193 | return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] && |
| 194 | X.data[2] == Y.data[2]; |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 195 | } |