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 | |
Ted Kremenek | 0a90d32 | 2010-11-17 23:24:11 +0000 | [diff] [blame] | 16 | #include "CXTranslationUnit.h" |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 17 | #include "CXCursor.h" |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 18 | #include "CXString.h" |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/ASTUnit.h" |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
| 23 | #include "clang/AST/Expr.h" |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 25 | #include "clang-c/Index.h" |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 29 | using namespace cxcursor; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 31 | CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) { |
| 32 | assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid); |
| 33 | CXCursor C = { K, { 0, 0, 0 } }; |
| 34 | return C; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 37 | static CXCursorKind GetCursorKind(const Attr *A) { |
| 38 | assert(A && "Invalid arguments!"); |
| 39 | switch (A->getKind()) { |
| 40 | default: break; |
Sean Hunt | 387475d | 2010-06-16 23:43:53 +0000 | [diff] [blame] | 41 | case attr::IBAction: return CXCursor_IBActionAttr; |
| 42 | case attr::IBOutlet: return CXCursor_IBOutletAttr; |
| 43 | case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr; |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | return CXCursor_UnexposedAttr; |
| 47 | } |
| 48 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 49 | CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent, |
| 50 | CXTranslationUnit TU) { |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 51 | assert(A && Parent && TU && "Invalid arguments!"); |
| 52 | CXCursor C = { GetCursorKind(A), { Parent, (void*)A, TU } }; |
| 53 | return C; |
| 54 | } |
| 55 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 56 | CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU, |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 57 | bool FirstInDeclGroup) { |
Daniel Dunbar | 54d67ca | 2010-01-25 00:40:30 +0000 | [diff] [blame] | 58 | assert(D && TU && "Invalid arguments!"); |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 59 | CXCursor C = { getCursorKindForDecl(D), |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 60 | { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU } |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 61 | }; |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 62 | return C; |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 65 | CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, |
| 66 | CXTranslationUnit TU) { |
Daniel Dunbar | 54d67ca | 2010-01-25 00:40:30 +0000 | [diff] [blame] | 67 | assert(S && TU && "Invalid arguments!"); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 68 | CXCursorKind K = CXCursor_NotImplemented; |
| 69 | |
| 70 | switch (S->getStmtClass()) { |
| 71 | case Stmt::NoStmtClass: |
| 72 | break; |
| 73 | |
| 74 | case Stmt::NullStmtClass: |
| 75 | case Stmt::CompoundStmtClass: |
| 76 | case Stmt::CaseStmtClass: |
| 77 | case Stmt::DefaultStmtClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 78 | case Stmt::IfStmtClass: |
| 79 | case Stmt::SwitchStmtClass: |
| 80 | case Stmt::WhileStmtClass: |
| 81 | case Stmt::DoStmtClass: |
| 82 | case Stmt::ForStmtClass: |
| 83 | case Stmt::GotoStmtClass: |
| 84 | case Stmt::IndirectGotoStmtClass: |
| 85 | case Stmt::ContinueStmtClass: |
| 86 | case Stmt::BreakStmtClass: |
| 87 | case Stmt::ReturnStmtClass: |
| 88 | case Stmt::DeclStmtClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 89 | case Stmt::AsmStmtClass: |
| 90 | case Stmt::ObjCAtTryStmtClass: |
| 91 | case Stmt::ObjCAtCatchStmtClass: |
| 92 | case Stmt::ObjCAtFinallyStmtClass: |
| 93 | case Stmt::ObjCAtThrowStmtClass: |
| 94 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 95 | case Stmt::ObjCForCollectionStmtClass: |
| 96 | case Stmt::CXXCatchStmtClass: |
| 97 | case Stmt::CXXTryStmtClass: |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 98 | case Stmt::CXXForRangeStmtClass: |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame^] | 99 | case Stmt::SEHTryStmtClass: |
| 100 | case Stmt::SEHExceptStmtClass: |
| 101 | case Stmt::SEHFinallyStmtClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 102 | K = CXCursor_UnexposedStmt; |
| 103 | break; |
| 104 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 105 | case Stmt::LabelStmtClass: |
| 106 | K = CXCursor_LabelStmt; |
| 107 | break; |
| 108 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 109 | case Stmt::PredefinedExprClass: |
| 110 | case Stmt::IntegerLiteralClass: |
| 111 | case Stmt::FloatingLiteralClass: |
| 112 | case Stmt::ImaginaryLiteralClass: |
| 113 | case Stmt::StringLiteralClass: |
| 114 | case Stmt::CharacterLiteralClass: |
| 115 | case Stmt::ParenExprClass: |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 116 | case Stmt::UnaryOperatorClass: |
| 117 | case Stmt::OffsetOfExprClass: |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 118 | case Stmt::UnaryExprOrTypeTraitExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 119 | case Stmt::ArraySubscriptExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 120 | case Stmt::BinaryOperatorClass: |
| 121 | case Stmt::CompoundAssignOperatorClass: |
| 122 | case Stmt::ConditionalOperatorClass: |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 123 | case Stmt::BinaryConditionalOperatorClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 124 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 125 | case Stmt::CStyleCastExprClass: |
| 126 | case Stmt::CompoundLiteralExprClass: |
| 127 | case Stmt::ExtVectorElementExprClass: |
| 128 | case Stmt::InitListExprClass: |
| 129 | case Stmt::DesignatedInitExprClass: |
| 130 | case Stmt::ImplicitValueInitExprClass: |
| 131 | case Stmt::ParenListExprClass: |
| 132 | case Stmt::VAArgExprClass: |
| 133 | case Stmt::AddrLabelExprClass: |
| 134 | case Stmt::StmtExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 135 | case Stmt::ChooseExprClass: |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 136 | case Stmt::GenericSelectionExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 137 | case Stmt::GNUNullExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 138 | case Stmt::CXXStaticCastExprClass: |
| 139 | case Stmt::CXXDynamicCastExprClass: |
| 140 | case Stmt::CXXReinterpretCastExprClass: |
| 141 | case Stmt::CXXConstCastExprClass: |
| 142 | case Stmt::CXXFunctionalCastExprClass: |
| 143 | case Stmt::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 144 | case Stmt::CXXUuidofExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 145 | case Stmt::CXXBoolLiteralExprClass: |
| 146 | case Stmt::CXXNullPtrLiteralExprClass: |
| 147 | case Stmt::CXXThisExprClass: |
| 148 | case Stmt::CXXThrowExprClass: |
| 149 | case Stmt::CXXDefaultArgExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 150 | case Stmt::CXXScalarValueInitExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 151 | case Stmt::CXXNewExprClass: |
| 152 | case Stmt::CXXDeleteExprClass: |
| 153 | case Stmt::CXXPseudoDestructorExprClass: |
| 154 | case Stmt::UnresolvedLookupExprClass: |
| 155 | case Stmt::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 156 | case Stmt::BinaryTypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 157 | case Stmt::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 158 | case Stmt::ExpressionTraitExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 159 | case Stmt::DependentScopeDeclRefExprClass: |
| 160 | case Stmt::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 161 | case Stmt::ExprWithCleanupsClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 162 | case Stmt::CXXUnresolvedConstructExprClass: |
| 163 | case Stmt::CXXDependentScopeMemberExprClass: |
| 164 | case Stmt::UnresolvedMemberExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 165 | case Stmt::CXXNoexceptExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 166 | case Stmt::ObjCStringLiteralClass: |
| 167 | case Stmt::ObjCEncodeExprClass: |
| 168 | case Stmt::ObjCSelectorExprClass: |
| 169 | case Stmt::ObjCProtocolExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 170 | case Stmt::ObjCIsaExprClass: |
| 171 | case Stmt::ShuffleVectorExprClass: |
| 172 | case Stmt::BlockExprClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 173 | case Stmt::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 174 | case Stmt::PackExpansionExprClass: |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 175 | case Stmt::SizeOfPackExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 176 | K = CXCursor_UnexposedExpr; |
| 177 | break; |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 178 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 179 | case Stmt::DeclRefExprClass: |
| 180 | case Stmt::BlockDeclRefExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 181 | case Stmt::SubstNonTypeTemplateParmPackExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 182 | // FIXME: UnresolvedLookupExpr? |
| 183 | // FIXME: DependentScopeDeclRefExpr? |
| 184 | K = CXCursor_DeclRefExpr; |
| 185 | break; |
| 186 | |
| 187 | case Stmt::MemberExprClass: |
| 188 | case Stmt::ObjCIvarRefExprClass: |
| 189 | case Stmt::ObjCPropertyRefExprClass: |
| 190 | // FIXME: UnresolvedMemberExpr? |
| 191 | // FIXME: CXXDependentScopeMemberExpr? |
| 192 | K = CXCursor_MemberRefExpr; |
| 193 | break; |
| 194 | |
| 195 | case Stmt::CallExprClass: |
| 196 | case Stmt::CXXOperatorCallExprClass: |
| 197 | case Stmt::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 198 | case Stmt::CUDAKernelCallExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 199 | case Stmt::CXXConstructExprClass: |
| 200 | case Stmt::CXXTemporaryObjectExprClass: |
| 201 | // FIXME: CXXUnresolvedConstructExpr |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 202 | K = CXCursor_CallExpr; |
| 203 | break; |
| 204 | |
| 205 | case Stmt::ObjCMessageExprClass: |
| 206 | K = CXCursor_ObjCMessageExpr; |
| 207 | break; |
| 208 | } |
| 209 | |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 210 | CXCursor C = { K, { Parent, S, TU } }; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 211 | return C; |
| 212 | } |
| 213 | |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 214 | CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super, |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 215 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 216 | CXTranslationUnit TU) { |
Daniel Dunbar | 54d67ca | 2010-01-25 00:40:30 +0000 | [diff] [blame] | 217 | assert(Super && TU && "Invalid arguments!"); |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 218 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 219 | CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } }; |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 220 | return C; |
| 221 | } |
| 222 | |
| 223 | std::pair<ObjCInterfaceDecl *, SourceLocation> |
| 224 | cxcursor::getCursorObjCSuperClassRef(CXCursor C) { |
| 225 | assert(C.kind == CXCursor_ObjCSuperClassRef); |
| 226 | return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]), |
| 227 | SourceLocation::getFromRawEncoding( |
| 228 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 229 | } |
| 230 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 231 | CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super, |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 232 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 233 | CXTranslationUnit TU) { |
Daniel Dunbar | 54d67ca | 2010-01-25 00:40:30 +0000 | [diff] [blame] | 234 | assert(Super && TU && "Invalid arguments!"); |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 235 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 236 | CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } }; |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 237 | return C; |
| 238 | } |
| 239 | |
| 240 | std::pair<ObjCProtocolDecl *, SourceLocation> |
| 241 | cxcursor::getCursorObjCProtocolRef(CXCursor C) { |
| 242 | assert(C.kind == CXCursor_ObjCProtocolRef); |
| 243 | return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]), |
| 244 | SourceLocation::getFromRawEncoding( |
| 245 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 246 | } |
| 247 | |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 248 | CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class, |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 249 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 250 | CXTranslationUnit TU) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 251 | // 'Class' can be null for invalid code. |
| 252 | if (!Class) |
| 253 | return MakeCXCursorInvalid(CXCursor_InvalidCode); |
| 254 | assert(TU && "Invalid arguments!"); |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 255 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 256 | CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } }; |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 257 | return C; |
| 258 | } |
| 259 | |
| 260 | std::pair<ObjCInterfaceDecl *, SourceLocation> |
| 261 | cxcursor::getCursorObjCClassRef(CXCursor C) { |
| 262 | assert(C.kind == CXCursor_ObjCClassRef); |
| 263 | return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]), |
| 264 | SourceLocation::getFromRawEncoding( |
| 265 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 266 | } |
| 267 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 268 | CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 269 | CXTranslationUnit TU) { |
Daniel Dunbar | 54d67ca | 2010-01-25 00:40:30 +0000 | [diff] [blame] | 270 | assert(Type && TU && "Invalid arguments!"); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 271 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 272 | CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } }; |
| 273 | return C; |
| 274 | } |
| 275 | |
| 276 | std::pair<TypeDecl *, SourceLocation> |
| 277 | cxcursor::getCursorTypeRef(CXCursor C) { |
| 278 | assert(C.kind == CXCursor_TypeRef); |
| 279 | return std::make_pair(static_cast<TypeDecl *>(C.data[0]), |
| 280 | SourceLocation::getFromRawEncoding( |
| 281 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 282 | } |
| 283 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 284 | CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 285 | SourceLocation Loc, |
| 286 | CXTranslationUnit TU) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 287 | assert(Template && TU && "Invalid arguments!"); |
| 288 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 289 | CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } }; |
| 290 | return C; |
| 291 | } |
| 292 | |
| 293 | std::pair<TemplateDecl *, SourceLocation> |
| 294 | cxcursor::getCursorTemplateRef(CXCursor C) { |
| 295 | assert(C.kind == CXCursor_TemplateRef); |
| 296 | return std::make_pair(static_cast<TemplateDecl *>(C.data[0]), |
| 297 | SourceLocation::getFromRawEncoding( |
| 298 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 299 | } |
| 300 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 301 | CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 302 | CXTranslationUnit TU) { |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 303 | |
| 304 | assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU && |
| 305 | "Invalid arguments!"); |
| 306 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 307 | CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } }; |
| 308 | return C; |
| 309 | } |
| 310 | |
| 311 | std::pair<NamedDecl *, SourceLocation> |
| 312 | cxcursor::getCursorNamespaceRef(CXCursor C) { |
| 313 | assert(C.kind == CXCursor_NamespaceRef); |
| 314 | return std::make_pair(static_cast<NamedDecl *>(C.data[0]), |
| 315 | SourceLocation::getFromRawEncoding( |
| 316 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 317 | } |
| 318 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 319 | CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 320 | CXTranslationUnit TU) { |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 321 | |
| 322 | assert(Field && TU && "Invalid arguments!"); |
| 323 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 324 | CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } }; |
| 325 | return C; |
| 326 | } |
| 327 | |
| 328 | std::pair<FieldDecl *, SourceLocation> |
| 329 | cxcursor::getCursorMemberRef(CXCursor C) { |
| 330 | assert(C.kind == CXCursor_MemberRef); |
| 331 | return std::make_pair(static_cast<FieldDecl *>(C.data[0]), |
| 332 | SourceLocation::getFromRawEncoding( |
| 333 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 334 | } |
| 335 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 336 | CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, |
| 337 | CXTranslationUnit TU){ |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 338 | CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } }; |
| 339 | return C; |
| 340 | } |
| 341 | |
| 342 | CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) { |
| 343 | assert(C.kind == CXCursor_CXXBaseSpecifier); |
| 344 | return static_cast<CXXBaseSpecifier*>(C.data[0]); |
| 345 | } |
| 346 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 347 | CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 348 | CXTranslationUnit TU) { |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 349 | CXCursor C = { CXCursor_PreprocessingDirective, |
| 350 | { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()), |
| 351 | reinterpret_cast<void *>(Range.getEnd().getRawEncoding()), |
| 352 | TU } |
| 353 | }; |
| 354 | return C; |
| 355 | } |
| 356 | |
| 357 | SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) { |
| 358 | assert(C.kind == CXCursor_PreprocessingDirective); |
| 359 | return SourceRange(SourceLocation::getFromRawEncoding( |
| 360 | reinterpret_cast<uintptr_t> (C.data[0])), |
| 361 | SourceLocation::getFromRawEncoding( |
| 362 | reinterpret_cast<uintptr_t> (C.data[1]))); |
| 363 | } |
| 364 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 365 | CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, |
| 366 | CXTranslationUnit TU) { |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 367 | CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } }; |
| 368 | return C; |
| 369 | } |
| 370 | |
| 371 | MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) { |
| 372 | assert(C.kind == CXCursor_MacroDefinition); |
| 373 | return static_cast<MacroDefinition *>(C.data[0]); |
| 374 | } |
| 375 | |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 376 | CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 377 | CXTranslationUnit TU) { |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 378 | CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } }; |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 379 | return C; |
| 380 | } |
| 381 | |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 382 | MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) { |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 383 | assert(C.kind == CXCursor_MacroInstantiation); |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 384 | return static_cast<MacroInstantiation *>(C.data[0]); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 387 | CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 388 | CXTranslationUnit TU) { |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 389 | CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } }; |
| 390 | return C; |
| 391 | } |
| 392 | |
| 393 | InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) { |
| 394 | assert(C.kind == CXCursor_InclusionDirective); |
| 395 | return static_cast<InclusionDirective *>(C.data[0]); |
| 396 | } |
| 397 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 398 | CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 399 | CXTranslationUnit TU) { |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 400 | |
| 401 | assert(Label && TU && "Invalid arguments!"); |
| 402 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 403 | CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } }; |
| 404 | return C; |
| 405 | } |
| 406 | |
| 407 | std::pair<LabelStmt*, SourceLocation> |
| 408 | cxcursor::getCursorLabelRef(CXCursor C) { |
| 409 | assert(C.kind == CXCursor_LabelRef); |
| 410 | return std::make_pair(static_cast<LabelStmt *>(C.data[0]), |
| 411 | SourceLocation::getFromRawEncoding( |
| 412 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 413 | } |
| 414 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 415 | CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 416 | CXTranslationUnit TU) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 417 | assert(E && TU && "Invalid arguments!"); |
| 418 | OverloadedDeclRefStorage Storage(E); |
| 419 | void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding()); |
| 420 | CXCursor C = { |
| 421 | CXCursor_OverloadedDeclRef, |
| 422 | { Storage.getOpaqueValue(), RawLoc, TU } |
| 423 | }; |
| 424 | return C; |
| 425 | } |
| 426 | |
| 427 | CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D, |
| 428 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 429 | CXTranslationUnit TU) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 430 | assert(D && TU && "Invalid arguments!"); |
| 431 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 432 | OverloadedDeclRefStorage Storage(D); |
| 433 | CXCursor C = { |
| 434 | CXCursor_OverloadedDeclRef, |
| 435 | { Storage.getOpaqueValue(), RawLoc, TU } |
| 436 | }; |
| 437 | return C; |
| 438 | } |
| 439 | |
| 440 | CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name, |
| 441 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 442 | CXTranslationUnit TU) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 443 | assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!"); |
| 444 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 445 | OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate()); |
| 446 | CXCursor C = { |
| 447 | CXCursor_OverloadedDeclRef, |
| 448 | { Storage.getOpaqueValue(), RawLoc, TU } |
| 449 | }; |
| 450 | return C; |
| 451 | } |
| 452 | |
| 453 | std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation> |
| 454 | cxcursor::getCursorOverloadedDeclRef(CXCursor C) { |
| 455 | assert(C.kind == CXCursor_OverloadedDeclRef); |
| 456 | return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]), |
| 457 | SourceLocation::getFromRawEncoding( |
| 458 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 459 | } |
| 460 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 461 | Decl *cxcursor::getCursorDecl(CXCursor Cursor) { |
| 462 | return (Decl *)Cursor.data[0]; |
| 463 | } |
| 464 | |
| 465 | Expr *cxcursor::getCursorExpr(CXCursor Cursor) { |
| 466 | return dyn_cast_or_null<Expr>(getCursorStmt(Cursor)); |
| 467 | } |
| 468 | |
| 469 | Stmt *cxcursor::getCursorStmt(CXCursor Cursor) { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 470 | if (Cursor.kind == CXCursor_ObjCSuperClassRef || |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 471 | Cursor.kind == CXCursor_ObjCProtocolRef || |
| 472 | Cursor.kind == CXCursor_ObjCClassRef) |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 473 | return 0; |
| 474 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 475 | return (Stmt *)Cursor.data[1]; |
| 476 | } |
| 477 | |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 478 | Attr *cxcursor::getCursorAttr(CXCursor Cursor) { |
| 479 | return (Attr *)Cursor.data[1]; |
| 480 | } |
| 481 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 482 | ASTContext &cxcursor::getCursorContext(CXCursor Cursor) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 483 | return getCursorASTUnit(Cursor)->getASTContext(); |
| 484 | } |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 485 | |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 486 | ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 487 | return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2]) |
| 488 | ->TUData); |
| 489 | } |
| 490 | |
| 491 | CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) { |
| 492 | return static_cast<CXTranslationUnit>(Cursor.data[2]); |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 495 | bool cxcursor::operator==(CXCursor X, CXCursor Y) { |
| 496 | return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] && |
| 497 | X.data[2] == Y.data[2]; |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 498 | } |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 499 | |
| 500 | // FIXME: Remove once we can model DeclGroups and their appropriate ranges |
| 501 | // properly in the ASTs. |
| 502 | bool cxcursor::isFirstInDeclGroup(CXCursor C) { |
| 503 | assert(clang_isDeclaration(C.kind)); |
| 504 | return ((uintptr_t) (C.data[1])) != 0; |
| 505 | } |
| 506 | |
Ted Kremenek | eca099b | 2010-12-08 23:43:14 +0000 | [diff] [blame] | 507 | //===----------------------------------------------------------------------===// |
| 508 | // CXCursorSet. |
| 509 | //===----------------------------------------------------------------------===// |
| 510 | |
| 511 | typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl; |
| 512 | |
| 513 | static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) { |
| 514 | return (CXCursorSet) setImpl; |
| 515 | } |
| 516 | static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) { |
| 517 | return (CXCursorSet_Impl*) set; |
| 518 | } |
| 519 | namespace llvm { |
Ted Kremenek | da6fb69 | 2010-12-09 00:33:41 +0000 | [diff] [blame] | 520 | template<> struct DenseMapInfo<CXCursor> { |
Ted Kremenek | eca099b | 2010-12-08 23:43:14 +0000 | [diff] [blame] | 521 | public: |
| 522 | static inline CXCursor getEmptyKey() { |
| 523 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
| 524 | } |
| 525 | static inline CXCursor getTombstoneKey() { |
| 526 | return MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 527 | } |
| 528 | static inline unsigned getHashValue(const CXCursor &cursor) { |
| 529 | return llvm::DenseMapInfo<std::pair<void*,void*> > |
| 530 | ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1])); |
| 531 | } |
| 532 | static inline bool isEqual(const CXCursor &x, const CXCursor &y) { |
| 533 | return x.kind == y.kind && |
| 534 | x.data[0] == y.data[0] && |
| 535 | x.data[1] == y.data[1]; |
| 536 | } |
| 537 | }; |
| 538 | } |
| 539 | |
| 540 | extern "C" { |
| 541 | CXCursorSet clang_createCXCursorSet() { |
| 542 | return packCXCursorSet(new CXCursorSet_Impl()); |
| 543 | } |
| 544 | |
| 545 | void clang_disposeCXCursorSet(CXCursorSet set) { |
| 546 | delete unpackCXCursorSet(set); |
| 547 | } |
| 548 | |
| 549 | unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) { |
| 550 | CXCursorSet_Impl *setImpl = unpackCXCursorSet(set); |
| 551 | if (!setImpl) |
| 552 | return 0; |
| 553 | return setImpl->find(cursor) == setImpl->end(); |
| 554 | } |
| 555 | |
Anders Carlsson | e8b3de0 | 2010-12-09 01:00:12 +0000 | [diff] [blame] | 556 | unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) { |
Ted Kremenek | eca099b | 2010-12-08 23:43:14 +0000 | [diff] [blame] | 557 | // Do not insert invalid cursors into the set. |
| 558 | if (cursor.kind >= CXCursor_FirstInvalid && |
| 559 | cursor.kind <= CXCursor_LastInvalid) |
| 560 | return 1; |
| 561 | |
| 562 | CXCursorSet_Impl *setImpl = unpackCXCursorSet(set); |
| 563 | if (!setImpl) |
| 564 | return 1; |
| 565 | unsigned &entry = (*setImpl)[cursor]; |
| 566 | unsigned flag = entry == 0 ? 1 : 0; |
| 567 | entry = 1; |
| 568 | return flag; |
| 569 | } |
| 570 | } // end: extern "C" |