1 //===- CIndexUSR.cpp - Clang-C Source Indexing Library --------------------===// 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 implements the generation and use of USRs from CXEntities. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CIndexer.h" 15 #include "CXCursor.h" 16 #include "CXString.h" 17 #include "clang/AST/DeclTemplate.h" 18 #include "clang/AST/DeclVisitor.h" 19 #include "clang/Frontend/ASTUnit.h" 20 #include "clang/Lex/PreprocessingRecord.h" 21 #include "llvm/ADT/SmallString.h" 22 #include "llvm/Support/raw_ostream.h" 23 24 using namespace clang; 25 26 //===----------------------------------------------------------------------===// 27 // USR generation. 28 //===----------------------------------------------------------------------===// 29 30 namespace { 31 class USRGenerator : public ConstDeclVisitor<USRGenerator> { 32 OwningPtr<SmallString<128> > OwnedBuf; 33 SmallVectorImpl<char> &Buf; 34 llvm::raw_svector_ostream Out; 35 bool IgnoreResults; 36 ASTContext *Context; 37 bool generatedLoc; 38 39 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions; 40 41 public: 42 explicit USRGenerator(ASTContext *Ctx = 0, SmallVectorImpl<char> *extBuf = 0) 43 : OwnedBuf(extBuf ? 0 : new SmallString<128>()), 44 Buf(extBuf ? *extBuf : *OwnedBuf.get()), 45 Out(Buf), 46 IgnoreResults(false), 47 Context(Ctx), 48 generatedLoc(false) 49 { 50 // Add the USR space prefix. 51 Out << "c:"; 52 } 53 54 StringRef str() { 55 return Out.str(); 56 } 57 58 USRGenerator* operator->() { return this; } 59 60 template <typename T> 61 llvm::raw_svector_ostream &operator<<(const T &x) { 62 Out << x; 63 return Out; 64 } 65 66 bool ignoreResults() const { return IgnoreResults; } 67 68 // Visitation methods from generating USRs from AST elements. 69 void VisitDeclContext(const DeclContext *D); 70 void VisitFieldDecl(const FieldDecl *D); 71 void VisitFunctionDecl(const FunctionDecl *D); 72 void VisitNamedDecl(const NamedDecl *D); 73 void VisitNamespaceDecl(const NamespaceDecl *D); 74 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D); 75 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D); 76 void VisitClassTemplateDecl(const ClassTemplateDecl *D); 77 void VisitObjCContainerDecl(const ObjCContainerDecl *CD); 78 void VisitObjCMethodDecl(const ObjCMethodDecl *MD); 79 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D); 80 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D); 81 void VisitTagDecl(const TagDecl *D); 82 void VisitTypedefDecl(const TypedefDecl *D); 83 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D); 84 void VisitVarDecl(const VarDecl *D); 85 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D); 86 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D); 87 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) { 88 IgnoreResults = true; 89 } 90 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { 91 IgnoreResults = true; 92 } 93 void VisitUsingDecl(const UsingDecl *D) { 94 IgnoreResults = true; 95 } 96 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) { 97 IgnoreResults = true; 98 } 99 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) { 100 IgnoreResults = true; 101 } 102 103 /// Generate the string component containing the location of the 104 /// declaration. 105 bool GenLoc(const Decl *D); 106 107 /// String generation methods used both by the visitation methods 108 /// and from other clients that want to directly generate USRs. These 109 /// methods do not construct complete USRs (which incorporate the parents 110 /// of an AST element), but only the fragments concerning the AST element 111 /// itself. 112 113 /// Generate a USR for an Objective-C class. 114 void GenObjCClass(StringRef cls); 115 /// Generate a USR for an Objective-C class category. 116 void GenObjCCategory(StringRef cls, StringRef cat); 117 /// Generate a USR fragment for an Objective-C instance variable. The 118 /// complete USR can be created by concatenating the USR for the 119 /// encompassing class with this USR fragment. 120 void GenObjCIvar(StringRef ivar); 121 /// Generate a USR fragment for an Objective-C method. 122 void GenObjCMethod(StringRef sel, bool isInstanceMethod); 123 /// Generate a USR fragment for an Objective-C property. 124 void GenObjCProperty(StringRef prop); 125 /// Generate a USR for an Objective-C protocol. 126 void GenObjCProtocol(StringRef prot); 127 128 void VisitType(QualType T); 129 void VisitTemplateParameterList(const TemplateParameterList *Params); 130 void VisitTemplateName(TemplateName Name); 131 void VisitTemplateArgument(const TemplateArgument &Arg); 132 133 /// Emit a Decl's name using NamedDecl::printName() and return true if 134 /// the decl had no name. 135 bool EmitDeclName(const NamedDecl *D); 136 }; 137 138 } // end anonymous namespace 139 140 //===----------------------------------------------------------------------===// 141 // Generating USRs from ASTS. 142 //===----------------------------------------------------------------------===// 143 144 bool USRGenerator::EmitDeclName(const NamedDecl *D) { 145 Out.flush(); 146 const unsigned startSize = Buf.size(); 147 D->printName(Out); 148 Out.flush(); 149 const unsigned endSize = Buf.size(); 150 return startSize == endSize; 151 } 152 153 static inline bool ShouldGenerateLocation(const NamedDecl *D) { 154 return D->getLinkage() != ExternalLinkage; 155 } 156 157 void USRGenerator::VisitDeclContext(const DeclContext *DC) { 158 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC)) 159 Visit(D); 160 } 161 162 void USRGenerator::VisitFieldDecl(const FieldDecl *D) { 163 // The USR for an ivar declared in a class extension is based on the 164 // ObjCInterfaceDecl, not the ObjCCategoryDecl. 165 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D)) 166 Visit(ID); 167 else 168 VisitDeclContext(D->getDeclContext()); 169 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@"); 170 if (EmitDeclName(D)) { 171 // Bit fields can be anonymous. 172 IgnoreResults = true; 173 return; 174 } 175 } 176 177 void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) { 178 if (ShouldGenerateLocation(D) && GenLoc(D)) 179 return; 180 181 VisitDeclContext(D->getDeclContext()); 182 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) { 183 Out << "@FT@"; 184 VisitTemplateParameterList(FunTmpl->getTemplateParameters()); 185 } else 186 Out << "@F@"; 187 D->printName(Out); 188 189 ASTContext &Ctx = *Context; 190 if (!Ctx.getLangOpts().CPlusPlus || D->isExternC()) 191 return; 192 193 if (const TemplateArgumentList * 194 SpecArgs = D->getTemplateSpecializationArgs()) { 195 Out << '<'; 196 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) { 197 Out << '#'; 198 VisitTemplateArgument(SpecArgs->get(I)); 199 } 200 Out << '>'; 201 } 202 203 // Mangle in type information for the arguments. 204 for (FunctionDecl::param_const_iterator I = D->param_begin(), 205 E = D->param_end(); 206 I != E; ++I) { 207 Out << '#'; 208 if (ParmVarDecl *PD = *I) 209 VisitType(PD->getType()); 210 } 211 if (D->isVariadic()) 212 Out << '.'; 213 Out << '#'; 214 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 215 if (MD->isStatic()) 216 Out << 'S'; 217 if (unsigned quals = MD->getTypeQualifiers()) 218 Out << (char)('0' + quals); 219 } 220 } 221 222 void USRGenerator::VisitNamedDecl(const NamedDecl *D) { 223 VisitDeclContext(D->getDeclContext()); 224 Out << "@"; 225 226 if (EmitDeclName(D)) { 227 // The string can be empty if the declaration has no name; e.g., it is 228 // the ParmDecl with no name for declaration of a function pointer type, 229 // e.g.: void (*f)(void *); 230 // In this case, don't generate a USR. 231 IgnoreResults = true; 232 } 233 } 234 235 void USRGenerator::VisitVarDecl(const VarDecl *D) { 236 // VarDecls can be declared 'extern' within a function or method body, 237 // but their enclosing DeclContext is the function, not the TU. We need 238 // to check the storage class to correctly generate the USR. 239 if (ShouldGenerateLocation(D) && GenLoc(D)) 240 return; 241 242 VisitDeclContext(D->getDeclContext()); 243 244 // Variables always have simple names. 245 StringRef s = D->getName(); 246 247 // The string can be empty if the declaration has no name; e.g., it is 248 // the ParmDecl with no name for declaration of a function pointer type, e.g.: 249 // void (*f)(void *); 250 // In this case, don't generate a USR. 251 if (s.empty()) 252 IgnoreResults = true; 253 else 254 Out << '@' << s; 255 } 256 257 void USRGenerator::VisitNonTypeTemplateParmDecl( 258 const NonTypeTemplateParmDecl *D) { 259 GenLoc(D); 260 return; 261 } 262 263 void USRGenerator::VisitTemplateTemplateParmDecl( 264 const TemplateTemplateParmDecl *D) { 265 GenLoc(D); 266 return; 267 } 268 269 void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) { 270 if (D->isAnonymousNamespace()) { 271 Out << "@aN"; 272 return; 273 } 274 275 VisitDeclContext(D->getDeclContext()); 276 if (!IgnoreResults) 277 Out << "@N@" << D->getName(); 278 } 279 280 void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) { 281 VisitFunctionDecl(D->getTemplatedDecl()); 282 } 283 284 void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) { 285 VisitTagDecl(D->getTemplatedDecl()); 286 } 287 288 void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) { 289 VisitDeclContext(D->getDeclContext()); 290 if (!IgnoreResults) 291 Out << "@NA@" << D->getName(); 292 } 293 294 void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) { 295 const DeclContext *container = D->getDeclContext(); 296 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) { 297 Visit(pd); 298 } 299 else { 300 // The USR for a method declared in a class extension or category is based on 301 // the ObjCInterfaceDecl, not the ObjCCategoryDecl. 302 const ObjCInterfaceDecl *ID = D->getClassInterface(); 303 if (!ID) { 304 IgnoreResults = true; 305 return; 306 } 307 Visit(ID); 308 } 309 // Ideally we would use 'GenObjCMethod', but this is such a hot path 310 // for Objective-C code that we don't want to use 311 // DeclarationName::getAsString(). 312 Out << (D->isInstanceMethod() ? "(im)" : "(cm)"); 313 DeclarationName N(D->getSelector()); 314 N.printName(Out); 315 } 316 317 void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) { 318 switch (D->getKind()) { 319 default: 320 llvm_unreachable("Invalid ObjC container."); 321 case Decl::ObjCInterface: 322 case Decl::ObjCImplementation: 323 GenObjCClass(D->getName()); 324 break; 325 case Decl::ObjCCategory: { 326 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); 327 const ObjCInterfaceDecl *ID = CD->getClassInterface(); 328 if (!ID) { 329 // Handle invalid code where the @interface might not 330 // have been specified. 331 // FIXME: We should be able to generate this USR even if the 332 // @interface isn't available. 333 IgnoreResults = true; 334 return; 335 } 336 // Specially handle class extensions, which are anonymous categories. 337 // We want to mangle in the location to uniquely distinguish them. 338 if (CD->IsClassExtension()) { 339 Out << "objc(ext)" << ID->getName() << '@'; 340 GenLoc(CD); 341 } 342 else 343 GenObjCCategory(ID->getName(), CD->getName()); 344 345 break; 346 } 347 case Decl::ObjCCategoryImpl: { 348 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); 349 const ObjCInterfaceDecl *ID = CD->getClassInterface(); 350 if (!ID) { 351 // Handle invalid code where the @interface might not 352 // have been specified. 353 // FIXME: We should be able to generate this USR even if the 354 // @interface isn't available. 355 IgnoreResults = true; 356 return; 357 } 358 GenObjCCategory(ID->getName(), CD->getName()); 359 break; 360 } 361 case Decl::ObjCProtocol: 362 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName()); 363 break; 364 } 365 } 366 367 void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { 368 // The USR for a property declared in a class extension or category is based 369 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl. 370 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D)) 371 Visit(ID); 372 else 373 Visit(cast<Decl>(D->getDeclContext())); 374 GenObjCProperty(D->getName()); 375 } 376 377 void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { 378 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) { 379 VisitObjCPropertyDecl(PD); 380 return; 381 } 382 383 IgnoreResults = true; 384 } 385 386 void USRGenerator::VisitTagDecl(const TagDecl *D) { 387 // Add the location of the tag decl to handle resolution across 388 // translation units. 389 if (ShouldGenerateLocation(D) && GenLoc(D)) 390 return; 391 392 D = D->getCanonicalDecl(); 393 VisitDeclContext(D->getDeclContext()); 394 395 bool AlreadyStarted = false; 396 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) { 397 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) { 398 AlreadyStarted = true; 399 400 switch (D->getTagKind()) { 401 case TTK_Interface: 402 case TTK_Struct: Out << "@ST"; break; 403 case TTK_Class: Out << "@CT"; break; 404 case TTK_Union: Out << "@UT"; break; 405 case TTK_Enum: llvm_unreachable("enum template"); 406 } 407 VisitTemplateParameterList(ClassTmpl->getTemplateParameters()); 408 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec 409 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) { 410 AlreadyStarted = true; 411 412 switch (D->getTagKind()) { 413 case TTK_Interface: 414 case TTK_Struct: Out << "@SP"; break; 415 case TTK_Class: Out << "@CP"; break; 416 case TTK_Union: Out << "@UP"; break; 417 case TTK_Enum: llvm_unreachable("enum partial specialization"); 418 } 419 VisitTemplateParameterList(PartialSpec->getTemplateParameters()); 420 } 421 } 422 423 if (!AlreadyStarted) { 424 switch (D->getTagKind()) { 425 case TTK_Interface: 426 case TTK_Struct: Out << "@S"; break; 427 case TTK_Class: Out << "@C"; break; 428 case TTK_Union: Out << "@U"; break; 429 case TTK_Enum: Out << "@E"; break; 430 } 431 } 432 433 Out << '@'; 434 Out.flush(); 435 assert(Buf.size() > 0); 436 const unsigned off = Buf.size() - 1; 437 438 if (EmitDeclName(D)) { 439 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) { 440 Buf[off] = 'A'; 441 Out << '@' << *TD; 442 } 443 else 444 Buf[off] = 'a'; 445 } 446 447 // For a class template specialization, mangle the template arguments. 448 if (const ClassTemplateSpecializationDecl *Spec 449 = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 450 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs(); 451 Out << '>'; 452 for (unsigned I = 0, N = Args.size(); I != N; ++I) { 453 Out << '#'; 454 VisitTemplateArgument(Args.get(I)); 455 } 456 } 457 } 458 459 void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) { 460 if (ShouldGenerateLocation(D) && GenLoc(D)) 461 return; 462 const DeclContext *DC = D->getDeclContext(); 463 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) 464 Visit(DCN); 465 Out << "@T@"; 466 Out << D->getName(); 467 } 468 469 void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) { 470 GenLoc(D); 471 return; 472 } 473 474 bool USRGenerator::GenLoc(const Decl *D) { 475 if (generatedLoc) 476 return IgnoreResults; 477 generatedLoc = true; 478 479 // Guard against null declarations in invalid code. 480 if (!D) { 481 IgnoreResults = true; 482 return true; 483 } 484 485 // Use the location of canonical decl. 486 D = D->getCanonicalDecl(); 487 488 const SourceManager &SM = Context->getSourceManager(); 489 SourceLocation L = D->getLocStart(); 490 if (L.isInvalid()) { 491 IgnoreResults = true; 492 return true; 493 } 494 L = SM.getExpansionLoc(L); 495 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L); 496 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first); 497 if (FE) { 498 Out << llvm::sys::path::filename(FE->getName()); 499 } 500 else { 501 // This case really isn't interesting. 502 IgnoreResults = true; 503 return true; 504 } 505 // Use the offest into the FileID to represent the location. Using 506 // a line/column can cause us to look back at the original source file, 507 // which is expensive. 508 Out << '@' << Decomposed.second; 509 return IgnoreResults; 510 } 511 512 void USRGenerator::VisitType(QualType T) { 513 // This method mangles in USR information for types. It can possibly 514 // just reuse the naming-mangling logic used by codegen, although the 515 // requirements for USRs might not be the same. 516 ASTContext &Ctx = *Context; 517 518 do { 519 T = Ctx.getCanonicalType(T); 520 Qualifiers Q = T.getQualifiers(); 521 unsigned qVal = 0; 522 if (Q.hasConst()) 523 qVal |= 0x1; 524 if (Q.hasVolatile()) 525 qVal |= 0x2; 526 if (Q.hasRestrict()) 527 qVal |= 0x4; 528 if(qVal) 529 Out << ((char) ('0' + qVal)); 530 531 // Mangle in ObjC GC qualifiers? 532 533 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) { 534 Out << 'P'; 535 T = Expansion->getPattern(); 536 } 537 538 if (const BuiltinType *BT = T->getAs<BuiltinType>()) { 539 unsigned char c = '\0'; 540 switch (BT->getKind()) { 541 case BuiltinType::Void: 542 c = 'v'; break; 543 case BuiltinType::Bool: 544 c = 'b'; break; 545 case BuiltinType::Char_U: 546 case BuiltinType::UChar: 547 c = 'c'; break; 548 case BuiltinType::Char16: 549 c = 'q'; break; 550 case BuiltinType::Char32: 551 c = 'w'; break; 552 case BuiltinType::UShort: 553 c = 's'; break; 554 case BuiltinType::UInt: 555 c = 'i'; break; 556 case BuiltinType::ULong: 557 c = 'l'; break; 558 case BuiltinType::ULongLong: 559 c = 'k'; break; 560 case BuiltinType::UInt128: 561 c = 'j'; break; 562 case BuiltinType::Char_S: 563 case BuiltinType::SChar: 564 c = 'C'; break; 565 case BuiltinType::WChar_S: 566 case BuiltinType::WChar_U: 567 c = 'W'; break; 568 case BuiltinType::Short: 569 c = 'S'; break; 570 case BuiltinType::Int: 571 c = 'I'; break; 572 case BuiltinType::Long: 573 c = 'L'; break; 574 case BuiltinType::LongLong: 575 c = 'K'; break; 576 case BuiltinType::Int128: 577 c = 'J'; break; 578 case BuiltinType::Half: 579 c = 'h'; break; 580 case BuiltinType::Float: 581 c = 'f'; break; 582 case BuiltinType::Double: 583 c = 'd'; break; 584 case BuiltinType::LongDouble: 585 c = 'D'; break; 586 case BuiltinType::NullPtr: 587 c = 'n'; break; 588 #define BUILTIN_TYPE(Id, SingletonId) 589 #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: 590 #include "clang/AST/BuiltinTypes.def" 591 case BuiltinType::Dependent: 592 case BuiltinType::OCLImage1d: 593 case BuiltinType::OCLImage1dArray: 594 case BuiltinType::OCLImage1dBuffer: 595 case BuiltinType::OCLImage2d: 596 case BuiltinType::OCLImage2dArray: 597 case BuiltinType::OCLImage3d: 598 case BuiltinType::OCLEvent: 599 case BuiltinType::OCLSampler: 600 IgnoreResults = true; 601 return; 602 case BuiltinType::ObjCId: 603 c = 'o'; break; 604 case BuiltinType::ObjCClass: 605 c = 'O'; break; 606 case BuiltinType::ObjCSel: 607 c = 'e'; break; 608 } 609 Out << c; 610 return; 611 } 612 613 // If we have already seen this (non-built-in) type, use a substitution 614 // encoding. 615 llvm::DenseMap<const Type *, unsigned>::iterator Substitution 616 = TypeSubstitutions.find(T.getTypePtr()); 617 if (Substitution != TypeSubstitutions.end()) { 618 Out << 'S' << Substitution->second << '_'; 619 return; 620 } else { 621 // Record this as a substitution. 622 unsigned Number = TypeSubstitutions.size(); 623 TypeSubstitutions[T.getTypePtr()] = Number; 624 } 625 626 if (const PointerType *PT = T->getAs<PointerType>()) { 627 Out << '*'; 628 T = PT->getPointeeType(); 629 continue; 630 } 631 if (const ReferenceType *RT = T->getAs<ReferenceType>()) { 632 Out << '&'; 633 T = RT->getPointeeType(); 634 continue; 635 } 636 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) { 637 Out << 'F'; 638 VisitType(FT->getResultType()); 639 for (FunctionProtoType::arg_type_iterator 640 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) { 641 VisitType(*I); 642 } 643 if (FT->isVariadic()) 644 Out << '.'; 645 return; 646 } 647 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { 648 Out << 'B'; 649 T = BT->getPointeeType(); 650 continue; 651 } 652 if (const ComplexType *CT = T->getAs<ComplexType>()) { 653 Out << '<'; 654 T = CT->getElementType(); 655 continue; 656 } 657 if (const TagType *TT = T->getAs<TagType>()) { 658 Out << '$'; 659 VisitTagDecl(TT->getDecl()); 660 return; 661 } 662 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) { 663 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); 664 return; 665 } 666 if (const TemplateSpecializationType *Spec 667 = T->getAs<TemplateSpecializationType>()) { 668 Out << '>'; 669 VisitTemplateName(Spec->getTemplateName()); 670 Out << Spec->getNumArgs(); 671 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 672 VisitTemplateArgument(Spec->getArg(I)); 673 return; 674 } 675 676 // Unhandled type. 677 Out << ' '; 678 break; 679 } while (true); 680 } 681 682 void USRGenerator::VisitTemplateParameterList( 683 const TemplateParameterList *Params) { 684 if (!Params) 685 return; 686 Out << '>' << Params->size(); 687 for (TemplateParameterList::const_iterator P = Params->begin(), 688 PEnd = Params->end(); 689 P != PEnd; ++P) { 690 Out << '#'; 691 if (isa<TemplateTypeParmDecl>(*P)) { 692 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack()) 693 Out<< 'p'; 694 Out << 'T'; 695 continue; 696 } 697 698 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { 699 if (NTTP->isParameterPack()) 700 Out << 'p'; 701 Out << 'N'; 702 VisitType(NTTP->getType()); 703 continue; 704 } 705 706 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); 707 if (TTP->isParameterPack()) 708 Out << 'p'; 709 Out << 't'; 710 VisitTemplateParameterList(TTP->getTemplateParameters()); 711 } 712 } 713 714 void USRGenerator::VisitTemplateName(TemplateName Name) { 715 if (TemplateDecl *Template = Name.getAsTemplateDecl()) { 716 if (TemplateTemplateParmDecl *TTP 717 = dyn_cast<TemplateTemplateParmDecl>(Template)) { 718 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); 719 return; 720 } 721 722 Visit(Template); 723 return; 724 } 725 726 // FIXME: Visit dependent template names. 727 } 728 729 void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) { 730 switch (Arg.getKind()) { 731 case TemplateArgument::Null: 732 break; 733 734 case TemplateArgument::Declaration: 735 Visit(Arg.getAsDecl()); 736 break; 737 738 case TemplateArgument::NullPtr: 739 break; 740 741 case TemplateArgument::TemplateExpansion: 742 Out << 'P'; // pack expansion of... 743 // Fall through 744 case TemplateArgument::Template: 745 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern()); 746 break; 747 748 case TemplateArgument::Expression: 749 // FIXME: Visit expressions. 750 break; 751 752 case TemplateArgument::Pack: 753 Out << 'p' << Arg.pack_size(); 754 for (TemplateArgument::pack_iterator P = Arg.pack_begin(), PEnd = Arg.pack_end(); 755 P != PEnd; ++P) 756 VisitTemplateArgument(*P); 757 break; 758 759 case TemplateArgument::Type: 760 VisitType(Arg.getAsType()); 761 break; 762 763 case TemplateArgument::Integral: 764 Out << 'V'; 765 VisitType(Arg.getIntegralType()); 766 Out << Arg.getAsIntegral(); 767 break; 768 } 769 } 770 771 //===----------------------------------------------------------------------===// 772 // General purpose USR generation methods. 773 //===----------------------------------------------------------------------===// 774 775 void USRGenerator::GenObjCClass(StringRef cls) { 776 Out << "objc(cs)" << cls; 777 } 778 779 void USRGenerator::GenObjCCategory(StringRef cls, StringRef cat) { 780 Out << "objc(cy)" << cls << '@' << cat; 781 } 782 783 void USRGenerator::GenObjCIvar(StringRef ivar) { 784 Out << '@' << ivar; 785 } 786 787 void USRGenerator::GenObjCMethod(StringRef meth, bool isInstanceMethod) { 788 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth; 789 } 790 791 void USRGenerator::GenObjCProperty(StringRef prop) { 792 Out << "(py)" << prop; 793 } 794 795 void USRGenerator::GenObjCProtocol(StringRef prot) { 796 Out << "objc(pl)" << prot; 797 } 798 799 //===----------------------------------------------------------------------===// 800 // API hooks. 801 //===----------------------------------------------------------------------===// 802 803 static inline StringRef extractUSRSuffix(StringRef s) { 804 return s.startswith("c:") ? s.substr(2) : ""; 805 } 806 807 bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) { 808 // Don't generate USRs for things with invalid locations. 809 if (!D || D->getLocStart().isInvalid()) 810 return true; 811 812 USRGenerator UG(&D->getASTContext(), &Buf); 813 UG->Visit(D); 814 815 if (UG->ignoreResults()) 816 return true; 817 818 return false; 819 } 820 821 extern "C" { 822 823 CXString clang_getCursorUSR(CXCursor C) { 824 const CXCursorKind &K = clang_getCursorKind(C); 825 826 if (clang_isDeclaration(K)) { 827 const Decl *D = cxcursor::getCursorDecl(C); 828 if (!D) 829 return cxstring::createEmpty(); 830 831 CXTranslationUnit TU = cxcursor::getCursorTU(C); 832 if (!TU) 833 return cxstring::createEmpty(); 834 835 cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU); 836 if (!buf) 837 return cxstring::createEmpty(); 838 839 bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data); 840 if (Ignore) { 841 buf->dispose(); 842 return cxstring::createEmpty(); 843 } 844 845 // Return the C-string, but don't make a copy since it is already in 846 // the string buffer. 847 buf->Data.push_back('\0'); 848 return createCXString(buf); 849 } 850 851 if (K == CXCursor_MacroDefinition) { 852 CXTranslationUnit TU = cxcursor::getCursorTU(C); 853 if (!TU) 854 return cxstring::createEmpty(); 855 856 cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU); 857 if (!buf) 858 return cxstring::createEmpty(); 859 860 { 861 USRGenerator UG(&cxcursor::getCursorASTUnit(C)->getASTContext(), 862 &buf->Data); 863 UG << "macro@" 864 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart(); 865 } 866 buf->Data.push_back('\0'); 867 return createCXString(buf); 868 } 869 870 return cxstring::createEmpty(); 871 } 872 873 CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) { 874 USRGenerator UG; 875 UG << extractUSRSuffix(clang_getCString(classUSR)); 876 UG->GenObjCIvar(name); 877 return cxstring::createDup(UG.str()); 878 } 879 880 CXString clang_constructUSR_ObjCMethod(const char *name, 881 unsigned isInstanceMethod, 882 CXString classUSR) { 883 USRGenerator UG; 884 UG << extractUSRSuffix(clang_getCString(classUSR)); 885 UG->GenObjCMethod(name, isInstanceMethod); 886 return cxstring::createDup(UG.str()); 887 } 888 889 CXString clang_constructUSR_ObjCClass(const char *name) { 890 USRGenerator UG; 891 UG->GenObjCClass(name); 892 return cxstring::createDup(UG.str()); 893 } 894 895 CXString clang_constructUSR_ObjCProtocol(const char *name) { 896 USRGenerator UG; 897 UG->GenObjCProtocol(name); 898 return cxstring::createDup(UG.str()); 899 } 900 901 CXString clang_constructUSR_ObjCCategory(const char *class_name, 902 const char *category_name) { 903 USRGenerator UG; 904 UG->GenObjCCategory(class_name, category_name); 905 return cxstring::createDup(UG.str()); 906 } 907 908 CXString clang_constructUSR_ObjCProperty(const char *property, 909 CXString classUSR) { 910 USRGenerator UG; 911 UG << extractUSRSuffix(clang_getCString(classUSR)); 912 UG->GenObjCProperty(property); 913 return cxstring::createDup(UG.str()); 914 } 915 916 } // end extern "C" 917