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. It should be the 11 // only file that has internal knowledge of the encoding of the data in 12 // CXCursor. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "CXTranslationUnit.h" 17 #include "CXCursor.h" 18 #include "CXString.h" 19 #include "clang/Frontend/ASTUnit.h" 20 #include "clang/AST/Decl.h" 21 #include "clang/AST/DeclCXX.h" 22 #include "clang/AST/DeclObjC.h" 23 #include "clang/AST/DeclTemplate.h" 24 #include "clang/AST/Expr.h" 25 #include "clang/AST/ExprCXX.h" 26 #include "clang/AST/ExprObjC.h" 27 #include "clang-c/Index.h" 28 #include "llvm/Support/ErrorHandling.h" 29 30 using namespace clang; 31 using namespace cxcursor; 32 33 CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) { 34 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid); 35 CXCursor C = { K, 0, { 0, 0, 0 } }; 36 return C; 37 } 38 39 static CXCursorKind GetCursorKind(const Attr *A) { 40 assert(A && "Invalid arguments!"); 41 switch (A->getKind()) { 42 default: break; 43 case attr::IBAction: return CXCursor_IBActionAttr; 44 case attr::IBOutlet: return CXCursor_IBOutletAttr; 45 case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr; 46 case attr::Final: return CXCursor_CXXFinalAttr; 47 case attr::Override: return CXCursor_CXXOverrideAttr; 48 case attr::Annotate: return CXCursor_AnnotateAttr; 49 } 50 51 return CXCursor_UnexposedAttr; 52 } 53 54 CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent, 55 CXTranslationUnit TU) { 56 assert(A && Parent && TU && "Invalid arguments!"); 57 CXCursor C = { GetCursorKind(A), 0, { Parent, (void*)A, TU } }; 58 return C; 59 } 60 61 CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU, 62 SourceRange RegionOfInterest, 63 bool FirstInDeclGroup) { 64 assert(D && TU && "Invalid arguments!"); 65 66 CXCursorKind K = getCursorKindForDecl(D); 67 68 if (K == CXCursor_ObjCClassMethodDecl || 69 K == CXCursor_ObjCInstanceMethodDecl) { 70 int SelectorIdIndex = -1; 71 // Check if cursor points to a selector id. 72 if (RegionOfInterest.isValid() && 73 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) { 74 SmallVector<SourceLocation, 16> SelLocs; 75 cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs); 76 SmallVector<SourceLocation, 16>::iterator 77 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin()); 78 if (I != SelLocs.end()) 79 SelectorIdIndex = I - SelLocs.begin(); 80 } 81 CXCursor C = { K, SelectorIdIndex, 82 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }}; 83 return C; 84 } 85 86 CXCursor C = { K, 0, { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }}; 87 return C; 88 } 89 90 CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU, 91 SourceRange RegionOfInterest) { 92 assert(S && TU && "Invalid arguments!"); 93 CXCursorKind K = CXCursor_NotImplemented; 94 95 switch (S->getStmtClass()) { 96 case Stmt::NoStmtClass: 97 break; 98 99 case Stmt::CaseStmtClass: 100 K = CXCursor_CaseStmt; 101 break; 102 103 case Stmt::DefaultStmtClass: 104 K = CXCursor_DefaultStmt; 105 break; 106 107 case Stmt::IfStmtClass: 108 K = CXCursor_IfStmt; 109 break; 110 111 case Stmt::SwitchStmtClass: 112 K = CXCursor_SwitchStmt; 113 break; 114 115 case Stmt::WhileStmtClass: 116 K = CXCursor_WhileStmt; 117 break; 118 119 case Stmt::DoStmtClass: 120 K = CXCursor_DoStmt; 121 break; 122 123 case Stmt::ForStmtClass: 124 K = CXCursor_ForStmt; 125 break; 126 127 case Stmt::GotoStmtClass: 128 K = CXCursor_GotoStmt; 129 break; 130 131 case Stmt::IndirectGotoStmtClass: 132 K = CXCursor_IndirectGotoStmt; 133 break; 134 135 case Stmt::ContinueStmtClass: 136 K = CXCursor_ContinueStmt; 137 break; 138 139 case Stmt::BreakStmtClass: 140 K = CXCursor_BreakStmt; 141 break; 142 143 case Stmt::ReturnStmtClass: 144 K = CXCursor_ReturnStmt; 145 break; 146 147 case Stmt::AsmStmtClass: 148 K = CXCursor_AsmStmt; 149 break; 150 151 case Stmt::ObjCAtTryStmtClass: 152 K = CXCursor_ObjCAtTryStmt; 153 break; 154 155 case Stmt::ObjCAtCatchStmtClass: 156 K = CXCursor_ObjCAtCatchStmt; 157 break; 158 159 case Stmt::ObjCAtFinallyStmtClass: 160 K = CXCursor_ObjCAtFinallyStmt; 161 break; 162 163 case Stmt::ObjCAtThrowStmtClass: 164 K = CXCursor_ObjCAtThrowStmt; 165 break; 166 167 case Stmt::ObjCAtSynchronizedStmtClass: 168 K = CXCursor_ObjCAtSynchronizedStmt; 169 break; 170 171 case Stmt::ObjCAutoreleasePoolStmtClass: 172 K = CXCursor_ObjCAutoreleasePoolStmt; 173 break; 174 175 case Stmt::ObjCForCollectionStmtClass: 176 K = CXCursor_ObjCForCollectionStmt; 177 break; 178 179 case Stmt::CXXCatchStmtClass: 180 K = CXCursor_CXXCatchStmt; 181 break; 182 183 case Stmt::CXXTryStmtClass: 184 K = CXCursor_CXXTryStmt; 185 break; 186 187 case Stmt::CXXForRangeStmtClass: 188 K = CXCursor_CXXForRangeStmt; 189 break; 190 191 case Stmt::SEHTryStmtClass: 192 K = CXCursor_SEHTryStmt; 193 break; 194 195 case Stmt::SEHExceptStmtClass: 196 K = CXCursor_SEHExceptStmt; 197 break; 198 199 case Stmt::SEHFinallyStmtClass: 200 K = CXCursor_SEHFinallyStmt; 201 break; 202 203 case Stmt::ArrayTypeTraitExprClass: 204 case Stmt::AsTypeExprClass: 205 case Stmt::AtomicExprClass: 206 case Stmt::BinaryConditionalOperatorClass: 207 case Stmt::BinaryTypeTraitExprClass: 208 case Stmt::CXXBindTemporaryExprClass: 209 case Stmt::CXXDefaultArgExprClass: 210 case Stmt::CXXScalarValueInitExprClass: 211 case Stmt::CXXUuidofExprClass: 212 case Stmt::ChooseExprClass: 213 case Stmt::DesignatedInitExprClass: 214 case Stmt::ExprWithCleanupsClass: 215 case Stmt::ExpressionTraitExprClass: 216 case Stmt::ExtVectorElementExprClass: 217 case Stmt::ImplicitCastExprClass: 218 case Stmt::ImplicitValueInitExprClass: 219 case Stmt::MaterializeTemporaryExprClass: 220 case Stmt::ObjCIndirectCopyRestoreExprClass: 221 case Stmt::OffsetOfExprClass: 222 case Stmt::ParenListExprClass: 223 case Stmt::PredefinedExprClass: 224 case Stmt::ShuffleVectorExprClass: 225 case Stmt::UnaryExprOrTypeTraitExprClass: 226 case Stmt::UnaryTypeTraitExprClass: 227 case Stmt::VAArgExprClass: 228 K = CXCursor_UnexposedExpr; 229 break; 230 231 case Stmt::OpaqueValueExprClass: 232 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr()) 233 return MakeCXCursor(Src, Parent, TU, RegionOfInterest); 234 K = CXCursor_UnexposedExpr; 235 break; 236 237 case Stmt::PseudoObjectExprClass: 238 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), 239 Parent, TU, RegionOfInterest); 240 241 case Stmt::CompoundStmtClass: 242 K = CXCursor_CompoundStmt; 243 break; 244 245 case Stmt::NullStmtClass: 246 K = CXCursor_NullStmt; 247 break; 248 249 case Stmt::LabelStmtClass: 250 K = CXCursor_LabelStmt; 251 break; 252 253 case Stmt::DeclStmtClass: 254 K = CXCursor_DeclStmt; 255 break; 256 257 case Stmt::IntegerLiteralClass: 258 K = CXCursor_IntegerLiteral; 259 break; 260 261 case Stmt::FloatingLiteralClass: 262 K = CXCursor_FloatingLiteral; 263 break; 264 265 case Stmt::ImaginaryLiteralClass: 266 K = CXCursor_ImaginaryLiteral; 267 break; 268 269 case Stmt::StringLiteralClass: 270 K = CXCursor_StringLiteral; 271 break; 272 273 case Stmt::CharacterLiteralClass: 274 K = CXCursor_CharacterLiteral; 275 break; 276 277 case Stmt::ParenExprClass: 278 K = CXCursor_ParenExpr; 279 break; 280 281 case Stmt::UnaryOperatorClass: 282 K = CXCursor_UnaryOperator; 283 break; 284 285 case Stmt::CXXNoexceptExprClass: 286 K = CXCursor_UnaryExpr; 287 break; 288 289 case Stmt::ArraySubscriptExprClass: 290 K = CXCursor_ArraySubscriptExpr; 291 break; 292 293 case Stmt::BinaryOperatorClass: 294 K = CXCursor_BinaryOperator; 295 break; 296 297 case Stmt::CompoundAssignOperatorClass: 298 K = CXCursor_CompoundAssignOperator; 299 break; 300 301 case Stmt::ConditionalOperatorClass: 302 K = CXCursor_ConditionalOperator; 303 break; 304 305 case Stmt::CStyleCastExprClass: 306 K = CXCursor_CStyleCastExpr; 307 break; 308 309 case Stmt::CompoundLiteralExprClass: 310 K = CXCursor_CompoundLiteralExpr; 311 break; 312 313 case Stmt::InitListExprClass: 314 K = CXCursor_InitListExpr; 315 break; 316 317 case Stmt::AddrLabelExprClass: 318 K = CXCursor_AddrLabelExpr; 319 break; 320 321 case Stmt::StmtExprClass: 322 K = CXCursor_StmtExpr; 323 break; 324 325 case Stmt::GenericSelectionExprClass: 326 K = CXCursor_GenericSelectionExpr; 327 break; 328 329 case Stmt::GNUNullExprClass: 330 K = CXCursor_GNUNullExpr; 331 break; 332 333 case Stmt::CXXStaticCastExprClass: 334 K = CXCursor_CXXStaticCastExpr; 335 break; 336 337 case Stmt::CXXDynamicCastExprClass: 338 K = CXCursor_CXXDynamicCastExpr; 339 break; 340 341 case Stmt::CXXReinterpretCastExprClass: 342 K = CXCursor_CXXReinterpretCastExpr; 343 break; 344 345 case Stmt::CXXConstCastExprClass: 346 K = CXCursor_CXXConstCastExpr; 347 break; 348 349 case Stmt::CXXFunctionalCastExprClass: 350 K = CXCursor_CXXFunctionalCastExpr; 351 break; 352 353 case Stmt::CXXTypeidExprClass: 354 K = CXCursor_CXXTypeidExpr; 355 break; 356 357 case Stmt::CXXBoolLiteralExprClass: 358 K = CXCursor_CXXBoolLiteralExpr; 359 break; 360 361 case Stmt::CXXNullPtrLiteralExprClass: 362 K = CXCursor_CXXNullPtrLiteralExpr; 363 break; 364 365 case Stmt::CXXThisExprClass: 366 K = CXCursor_CXXThisExpr; 367 break; 368 369 case Stmt::CXXThrowExprClass: 370 K = CXCursor_CXXThrowExpr; 371 break; 372 373 case Stmt::CXXNewExprClass: 374 K = CXCursor_CXXNewExpr; 375 break; 376 377 case Stmt::CXXDeleteExprClass: 378 K = CXCursor_CXXDeleteExpr; 379 break; 380 381 case Stmt::ObjCStringLiteralClass: 382 K = CXCursor_ObjCStringLiteral; 383 break; 384 385 case Stmt::ObjCEncodeExprClass: 386 K = CXCursor_ObjCEncodeExpr; 387 break; 388 389 case Stmt::ObjCSelectorExprClass: 390 K = CXCursor_ObjCSelectorExpr; 391 break; 392 393 case Stmt::ObjCProtocolExprClass: 394 K = CXCursor_ObjCProtocolExpr; 395 break; 396 397 case Stmt::ObjCBridgedCastExprClass: 398 K = CXCursor_ObjCBridgedCastExpr; 399 break; 400 401 case Stmt::BlockExprClass: 402 K = CXCursor_BlockExpr; 403 break; 404 405 case Stmt::PackExpansionExprClass: 406 K = CXCursor_PackExpansionExpr; 407 break; 408 409 case Stmt::SizeOfPackExprClass: 410 K = CXCursor_SizeOfPackExpr; 411 break; 412 413 case Stmt::BlockDeclRefExprClass: 414 case Stmt::DeclRefExprClass: 415 case Stmt::DependentScopeDeclRefExprClass: 416 case Stmt::SubstNonTypeTemplateParmExprClass: 417 case Stmt::SubstNonTypeTemplateParmPackExprClass: 418 case Stmt::UnresolvedLookupExprClass: 419 K = CXCursor_DeclRefExpr; 420 break; 421 422 case Stmt::CXXDependentScopeMemberExprClass: 423 case Stmt::CXXPseudoDestructorExprClass: 424 case Stmt::MemberExprClass: 425 case Stmt::ObjCIsaExprClass: 426 case Stmt::ObjCIvarRefExprClass: 427 case Stmt::ObjCPropertyRefExprClass: 428 case Stmt::UnresolvedMemberExprClass: 429 K = CXCursor_MemberRefExpr; 430 break; 431 432 case Stmt::CallExprClass: 433 case Stmt::CXXOperatorCallExprClass: 434 case Stmt::CXXMemberCallExprClass: 435 case Stmt::CUDAKernelCallExprClass: 436 case Stmt::CXXConstructExprClass: 437 case Stmt::CXXTemporaryObjectExprClass: 438 case Stmt::CXXUnresolvedConstructExprClass: 439 K = CXCursor_CallExpr; 440 break; 441 442 case Stmt::ObjCMessageExprClass: { 443 K = CXCursor_ObjCMessageExpr; 444 int SelectorIdIndex = -1; 445 // Check if cursor points to a selector id. 446 if (RegionOfInterest.isValid() && 447 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) { 448 SmallVector<SourceLocation, 16> SelLocs; 449 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs); 450 SmallVector<SourceLocation, 16>::iterator 451 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin()); 452 if (I != SelLocs.end()) 453 SelectorIdIndex = I - SelLocs.begin(); 454 } 455 CXCursor C = { K, 0, { Parent, S, TU } }; 456 return getSelectorIdentifierCursor(SelectorIdIndex, C); 457 } 458 459 case Stmt::MSDependentExistsStmtClass: 460 K = CXCursor_UnexposedStmt; 461 break; 462 } 463 464 CXCursor C = { K, 0, { Parent, S, TU } }; 465 return C; 466 } 467 468 CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super, 469 SourceLocation Loc, 470 CXTranslationUnit TU) { 471 assert(Super && TU && "Invalid arguments!"); 472 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); 473 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } }; 474 return C; 475 } 476 477 std::pair<ObjCInterfaceDecl *, SourceLocation> 478 cxcursor::getCursorObjCSuperClassRef(CXCursor C) { 479 assert(C.kind == CXCursor_ObjCSuperClassRef); 480 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]), 481 SourceLocation::getFromRawEncoding( 482 reinterpret_cast<uintptr_t>(C.data[1]))); 483 } 484 485 CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto, 486 SourceLocation Loc, 487 CXTranslationUnit TU) { 488 assert(Proto && TU && "Invalid arguments!"); 489 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); 490 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } }; 491 return C; 492 } 493 494 std::pair<ObjCProtocolDecl *, SourceLocation> 495 cxcursor::getCursorObjCProtocolRef(CXCursor C) { 496 assert(C.kind == CXCursor_ObjCProtocolRef); 497 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]), 498 SourceLocation::getFromRawEncoding( 499 reinterpret_cast<uintptr_t>(C.data[1]))); 500 } 501 502 CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class, 503 SourceLocation Loc, 504 CXTranslationUnit TU) { 505 // 'Class' can be null for invalid code. 506 if (!Class) 507 return MakeCXCursorInvalid(CXCursor_InvalidCode); 508 assert(TU && "Invalid arguments!"); 509 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); 510 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } }; 511 return C; 512 } 513 514 std::pair<ObjCInterfaceDecl *, SourceLocation> 515 cxcursor::getCursorObjCClassRef(CXCursor C) { 516 assert(C.kind == CXCursor_ObjCClassRef); 517 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]), 518 SourceLocation::getFromRawEncoding( 519 reinterpret_cast<uintptr_t>(C.data[1]))); 520 } 521 522 CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc, 523 CXTranslationUnit TU) { 524 assert(Type && TU && "Invalid arguments!"); 525 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); 526 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } }; 527 return C; 528 } 529 530 std::pair<TypeDecl *, SourceLocation> 531 cxcursor::getCursorTypeRef(CXCursor C) { 532 assert(C.kind == CXCursor_TypeRef); 533 return std::make_pair(static_cast<TypeDecl *>(C.data[0]), 534 SourceLocation::getFromRawEncoding( 535 reinterpret_cast<uintptr_t>(C.data[1]))); 536 } 537 538 CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template, 539 SourceLocation Loc, 540 CXTranslationUnit TU) { 541 assert(Template && TU && "Invalid arguments!"); 542 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); 543 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } }; 544 return C; 545 } 546 547 std::pair<TemplateDecl *, SourceLocation> 548 cxcursor::getCursorTemplateRef(CXCursor C) { 549 assert(C.kind == CXCursor_TemplateRef); 550 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]), 551 SourceLocation::getFromRawEncoding( 552 reinterpret_cast<uintptr_t>(C.data[1]))); 553 } 554 555 CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc, 556 CXTranslationUnit TU) { 557 558 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU && 559 "Invalid arguments!"); 560 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); 561 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } }; 562 return C; 563 } 564 565 std::pair<NamedDecl *, SourceLocation> 566 cxcursor::getCursorNamespaceRef(CXCursor C) { 567 assert(C.kind == CXCursor_NamespaceRef); 568 return std::make_pair(static_cast<NamedDecl *>(C.data[0]), 569 SourceLocation::getFromRawEncoding( 570 reinterpret_cast<uintptr_t>(C.data[1]))); 571 } 572 573 CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc, 574 CXTranslationUnit TU) { 575 576 assert(Field && TU && "Invalid arguments!"); 577 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); 578 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } }; 579 return C; 580 } 581 582 std::pair<FieldDecl *, SourceLocation> 583 cxcursor::getCursorMemberRef(CXCursor C) { 584 assert(C.kind == CXCursor_MemberRef); 585 return std::make_pair(static_cast<FieldDecl *>(C.data[0]), 586 SourceLocation::getFromRawEncoding( 587 reinterpret_cast<uintptr_t>(C.data[1]))); 588 } 589 590 CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, 591 CXTranslationUnit TU){ 592 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } }; 593 return C; 594 } 595 596 CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) { 597 assert(C.kind == CXCursor_CXXBaseSpecifier); 598 return static_cast<CXXBaseSpecifier*>(C.data[0]); 599 } 600 601 CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range, 602 CXTranslationUnit TU) { 603 CXCursor C = { CXCursor_PreprocessingDirective, 0, 604 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()), 605 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()), 606 TU } 607 }; 608 return C; 609 } 610 611 SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) { 612 assert(C.kind == CXCursor_PreprocessingDirective); 613 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding( 614 reinterpret_cast<uintptr_t> (C.data[0])), 615 SourceLocation::getFromRawEncoding( 616 reinterpret_cast<uintptr_t> (C.data[1]))); 617 ASTUnit *TU = getCursorASTUnit(C); 618 return TU->mapRangeFromPreamble(Range); 619 } 620 621 CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, 622 CXTranslationUnit TU) { 623 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } }; 624 return C; 625 } 626 627 MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) { 628 assert(C.kind == CXCursor_MacroDefinition); 629 return static_cast<MacroDefinition *>(C.data[0]); 630 } 631 632 CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI, 633 CXTranslationUnit TU) { 634 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } }; 635 return C; 636 } 637 638 MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) { 639 assert(C.kind == CXCursor_MacroExpansion); 640 return static_cast<MacroExpansion *>(C.data[0]); 641 } 642 643 CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID, 644 CXTranslationUnit TU) { 645 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } }; 646 return C; 647 } 648 649 InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) { 650 assert(C.kind == CXCursor_InclusionDirective); 651 return static_cast<InclusionDirective *>(C.data[0]); 652 } 653 654 CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc, 655 CXTranslationUnit TU) { 656 657 assert(Label && TU && "Invalid arguments!"); 658 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); 659 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } }; 660 return C; 661 } 662 663 std::pair<LabelStmt*, SourceLocation> 664 cxcursor::getCursorLabelRef(CXCursor C) { 665 assert(C.kind == CXCursor_LabelRef); 666 return std::make_pair(static_cast<LabelStmt *>(C.data[0]), 667 SourceLocation::getFromRawEncoding( 668 reinterpret_cast<uintptr_t>(C.data[1]))); 669 } 670 671 CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E, 672 CXTranslationUnit TU) { 673 assert(E && TU && "Invalid arguments!"); 674 OverloadedDeclRefStorage Storage(E); 675 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding()); 676 CXCursor C = { 677 CXCursor_OverloadedDeclRef, 0, 678 { Storage.getOpaqueValue(), RawLoc, TU } 679 }; 680 return C; 681 } 682 683 CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D, 684 SourceLocation Loc, 685 CXTranslationUnit TU) { 686 assert(D && TU && "Invalid arguments!"); 687 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); 688 OverloadedDeclRefStorage Storage(D); 689 CXCursor C = { 690 CXCursor_OverloadedDeclRef, 0, 691 { Storage.getOpaqueValue(), RawLoc, TU } 692 }; 693 return C; 694 } 695 696 CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name, 697 SourceLocation Loc, 698 CXTranslationUnit TU) { 699 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!"); 700 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); 701 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate()); 702 CXCursor C = { 703 CXCursor_OverloadedDeclRef, 0, 704 { Storage.getOpaqueValue(), RawLoc, TU } 705 }; 706 return C; 707 } 708 709 std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation> 710 cxcursor::getCursorOverloadedDeclRef(CXCursor C) { 711 assert(C.kind == CXCursor_OverloadedDeclRef); 712 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]), 713 SourceLocation::getFromRawEncoding( 714 reinterpret_cast<uintptr_t>(C.data[1]))); 715 } 716 717 Decl *cxcursor::getCursorDecl(CXCursor Cursor) { 718 return (Decl *)Cursor.data[0]; 719 } 720 721 Expr *cxcursor::getCursorExpr(CXCursor Cursor) { 722 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor)); 723 } 724 725 Stmt *cxcursor::getCursorStmt(CXCursor Cursor) { 726 if (Cursor.kind == CXCursor_ObjCSuperClassRef || 727 Cursor.kind == CXCursor_ObjCProtocolRef || 728 Cursor.kind == CXCursor_ObjCClassRef) 729 return 0; 730 731 return (Stmt *)Cursor.data[1]; 732 } 733 734 Attr *cxcursor::getCursorAttr(CXCursor Cursor) { 735 return (Attr *)Cursor.data[1]; 736 } 737 738 Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) { 739 return (Decl *)Cursor.data[0]; 740 } 741 742 ASTContext &cxcursor::getCursorContext(CXCursor Cursor) { 743 return getCursorASTUnit(Cursor)->getASTContext(); 744 } 745 746 ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) { 747 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2]) 748 ->TUData); 749 } 750 751 CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) { 752 return static_cast<CXTranslationUnit>(Cursor.data[2]); 753 } 754 755 static void CollectOverriddenMethods(CXTranslationUnit TU, 756 DeclContext *Ctx, 757 ObjCMethodDecl *Method, 758 SmallVectorImpl<CXCursor> &Methods) { 759 if (!Ctx) 760 return; 761 762 // If we have a class or category implementation, jump straight to the 763 // interface. 764 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx)) 765 return CollectOverriddenMethods(TU, Impl->getClassInterface(), 766 Method, Methods); 767 768 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx); 769 if (!Container) 770 return; 771 772 // Check whether we have a matching method at this level. 773 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(), 774 Method->isInstanceMethod())) 775 if (Method != Overridden) { 776 // We found an override at this level; there is no need to look 777 // into other protocols or categories. 778 Methods.push_back(MakeCXCursor(Overridden, TU)); 779 return; 780 } 781 782 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { 783 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), 784 PEnd = Protocol->protocol_end(); 785 P != PEnd; ++P) 786 CollectOverriddenMethods(TU, *P, Method, Methods); 787 } 788 789 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { 790 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), 791 PEnd = Category->protocol_end(); 792 P != PEnd; ++P) 793 CollectOverriddenMethods(TU, *P, Method, Methods); 794 } 795 796 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { 797 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(), 798 PEnd = Interface->protocol_end(); 799 P != PEnd; ++P) 800 CollectOverriddenMethods(TU, *P, Method, Methods); 801 802 for (ObjCCategoryDecl *Category = Interface->getCategoryList(); 803 Category; Category = Category->getNextClassCategory()) 804 CollectOverriddenMethods(TU, Category, Method, Methods); 805 806 // We only look into the superclass if we haven't found anything yet. 807 if (Methods.empty()) 808 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) 809 return CollectOverriddenMethods(TU, Super, Method, Methods); 810 } 811 } 812 813 void cxcursor::getOverriddenCursors(CXCursor cursor, 814 SmallVectorImpl<CXCursor> &overridden) { 815 if (!clang_isDeclaration(cursor.kind)) 816 return; 817 818 Decl *D = getCursorDecl(cursor); 819 if (!D) 820 return; 821 822 // Handle C++ member functions. 823 CXTranslationUnit TU = getCursorTU(cursor); 824 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { 825 for (CXXMethodDecl::method_iterator 826 M = CXXMethod->begin_overridden_methods(), 827 MEnd = CXXMethod->end_overridden_methods(); 828 M != MEnd; ++M) 829 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU)); 830 return; 831 } 832 833 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); 834 if (!Method) 835 return; 836 837 // Handle Objective-C methods. 838 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden); 839 } 840 841 std::pair<int, SourceLocation> 842 cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) { 843 if (cursor.kind == CXCursor_ObjCMessageExpr) { 844 if (cursor.xdata != -1) 845 return std::make_pair(cursor.xdata, 846 cast<ObjCMessageExpr>(getCursorExpr(cursor)) 847 ->getSelectorLoc(cursor.xdata)); 848 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl || 849 cursor.kind == CXCursor_ObjCInstanceMethodDecl) { 850 if (cursor.xdata != -1) 851 return std::make_pair(cursor.xdata, 852 cast<ObjCMethodDecl>(getCursorDecl(cursor)) 853 ->getSelectorLoc(cursor.xdata)); 854 } 855 856 return std::make_pair(-1, SourceLocation()); 857 } 858 859 CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) { 860 CXCursor newCursor = cursor; 861 862 if (cursor.kind == CXCursor_ObjCMessageExpr) { 863 if (SelIdx == -1 || 864 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor)) 865 ->getNumSelectorLocs()) 866 newCursor.xdata = -1; 867 else 868 newCursor.xdata = SelIdx; 869 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl || 870 cursor.kind == CXCursor_ObjCInstanceMethodDecl) { 871 if (SelIdx == -1 || 872 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor)) 873 ->getNumSelectorLocs()) 874 newCursor.xdata = -1; 875 else 876 newCursor.xdata = SelIdx; 877 } 878 879 return newCursor; 880 } 881 882 CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) { 883 if (cursor.kind != CXCursor_CallExpr) 884 return cursor; 885 886 if (cursor.xdata == 0) 887 return cursor; 888 889 Expr *E = getCursorExpr(cursor); 890 TypeSourceInfo *Type = 0; 891 if (CXXUnresolvedConstructExpr * 892 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) { 893 Type = UnCtor->getTypeSourceInfo(); 894 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){ 895 Type = Tmp->getTypeSourceInfo(); 896 } 897 898 if (!Type) 899 return cursor; 900 901 CXTranslationUnit TU = getCursorTU(cursor); 902 QualType Ty = Type->getType(); 903 TypeLoc TL = Type->getTypeLoc(); 904 SourceLocation Loc = TL.getBeginLoc(); 905 906 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) { 907 Ty = ElabT->getNamedType(); 908 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL); 909 Loc = ElabTL.getNamedTypeLoc().getBeginLoc(); 910 } 911 912 if (const TypedefType *Typedef = Ty->getAs<TypedefType>()) 913 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU); 914 if (const TagType *Tag = Ty->getAs<TagType>()) 915 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU); 916 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>()) 917 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU); 918 919 return cursor; 920 } 921 922 bool cxcursor::operator==(CXCursor X, CXCursor Y) { 923 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] && 924 X.data[2] == Y.data[2]; 925 } 926 927 // FIXME: Remove once we can model DeclGroups and their appropriate ranges 928 // properly in the ASTs. 929 bool cxcursor::isFirstInDeclGroup(CXCursor C) { 930 assert(clang_isDeclaration(C.kind)); 931 return ((uintptr_t) (C.data[1])) != 0; 932 } 933 934 //===----------------------------------------------------------------------===// 935 // libclang CXCursor APIs 936 //===----------------------------------------------------------------------===// 937 938 extern "C" { 939 940 int clang_Cursor_isNull(CXCursor cursor) { 941 return clang_equalCursors(cursor, clang_getNullCursor()); 942 } 943 944 CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) { 945 return getCursorTU(cursor); 946 } 947 948 } // end: extern "C" 949 950 //===----------------------------------------------------------------------===// 951 // CXCursorSet. 952 //===----------------------------------------------------------------------===// 953 954 typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl; 955 956 static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) { 957 return (CXCursorSet) setImpl; 958 } 959 static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) { 960 return (CXCursorSet_Impl*) set; 961 } 962 namespace llvm { 963 template<> struct DenseMapInfo<CXCursor> { 964 public: 965 static inline CXCursor getEmptyKey() { 966 return MakeCXCursorInvalid(CXCursor_InvalidFile); 967 } 968 static inline CXCursor getTombstoneKey() { 969 return MakeCXCursorInvalid(CXCursor_NoDeclFound); 970 } 971 static inline unsigned getHashValue(const CXCursor &cursor) { 972 return llvm::DenseMapInfo<std::pair<void*,void*> > 973 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1])); 974 } 975 static inline bool isEqual(const CXCursor &x, const CXCursor &y) { 976 return x.kind == y.kind && 977 x.data[0] == y.data[0] && 978 x.data[1] == y.data[1]; 979 } 980 }; 981 } 982 983 extern "C" { 984 CXCursorSet clang_createCXCursorSet() { 985 return packCXCursorSet(new CXCursorSet_Impl()); 986 } 987 988 void clang_disposeCXCursorSet(CXCursorSet set) { 989 delete unpackCXCursorSet(set); 990 } 991 992 unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) { 993 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set); 994 if (!setImpl) 995 return 0; 996 return setImpl->find(cursor) == setImpl->end(); 997 } 998 999 unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) { 1000 // Do not insert invalid cursors into the set. 1001 if (cursor.kind >= CXCursor_FirstInvalid && 1002 cursor.kind <= CXCursor_LastInvalid) 1003 return 1; 1004 1005 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set); 1006 if (!setImpl) 1007 return 1; 1008 unsigned &entry = (*setImpl)[cursor]; 1009 unsigned flag = entry == 0 ? 1 : 0; 1010 entry = 1; 1011 return flag; 1012 } 1013 1014 CXCompletionString clang_getCursorCompletionString(CXCursor cursor) { 1015 enum CXCursorKind kind = clang_getCursorKind(cursor); 1016 if (clang_isDeclaration(kind)) { 1017 Decl *decl = getCursorDecl(cursor); 1018 if (isa<NamedDecl>(decl)) { 1019 NamedDecl *namedDecl = (NamedDecl *)decl; 1020 ASTUnit *unit = getCursorASTUnit(cursor); 1021 if (unit->hasSema()) { 1022 Sema &S = unit->getSema(); 1023 CodeCompletionAllocator *Allocator 1024 = unit->getCursorCompletionAllocator().getPtr(); 1025 CodeCompletionResult Result(namedDecl); 1026 CodeCompletionString *String 1027 = Result.CreateCodeCompletionString(S, *Allocator); 1028 return String; 1029 } 1030 } 1031 } 1032 else if (kind == CXCursor_MacroDefinition) { 1033 MacroDefinition *definition = getCursorMacroDefinition(cursor); 1034 const IdentifierInfo *MacroInfo = definition->getName(); 1035 ASTUnit *unit = getCursorASTUnit(cursor); 1036 if (unit->hasSema()) { 1037 Sema &S = unit->getSema(); 1038 CodeCompletionAllocator *Allocator 1039 = unit->getCursorCompletionAllocator().getPtr(); 1040 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo)); 1041 CodeCompletionString *String 1042 = Result.CreateCodeCompletionString(S, *Allocator); 1043 return String; 1044 } 1045 } 1046 return NULL; 1047 } 1048 1049 } // end: extern "C" 1050