USRGeneration.cpp 27 KB

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