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