DeclarationName.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. //===- DeclarationName.cpp - Declaration names implementation -------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the DeclarationName and DeclarationNameTable
  10. // classes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/DeclarationName.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclBase.h"
  17. #include "clang/AST/DeclCXX.h"
  18. #include "clang/AST/DeclTemplate.h"
  19. #include "clang/AST/OpenMPClause.h"
  20. #include "clang/AST/PrettyPrinter.h"
  21. #include "clang/AST/Type.h"
  22. #include "clang/AST/TypeLoc.h"
  23. #include "clang/AST/TypeOrdering.h"
  24. #include "clang/Basic/IdentifierTable.h"
  25. #include "clang/Basic/LLVM.h"
  26. #include "clang/Basic/LangOptions.h"
  27. #include "clang/Basic/OperatorKinds.h"
  28. #include "clang/Basic/SourceLocation.h"
  29. #include "llvm/ADT/FoldingSet.h"
  30. #include "llvm/Support/Casting.h"
  31. #include "llvm/Support/Compiler.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include <algorithm>
  35. #include <cassert>
  36. #include <cstdint>
  37. #include <string>
  38. using namespace clang;
  39. static int compareInt(unsigned A, unsigned B) {
  40. return (A < B ? -1 : (A > B ? 1 : 0));
  41. }
  42. int DeclarationName::compare(DeclarationName LHS, DeclarationName RHS) {
  43. if (LHS.getNameKind() != RHS.getNameKind())
  44. return (LHS.getNameKind() < RHS.getNameKind() ? -1 : 1);
  45. switch (LHS.getNameKind()) {
  46. case DeclarationName::Identifier: {
  47. IdentifierInfo *LII = LHS.castAsIdentifierInfo();
  48. IdentifierInfo *RII = RHS.castAsIdentifierInfo();
  49. if (!LII)
  50. return RII ? -1 : 0;
  51. if (!RII)
  52. return 1;
  53. return LII->getName().compare(RII->getName());
  54. }
  55. case DeclarationName::ObjCZeroArgSelector:
  56. case DeclarationName::ObjCOneArgSelector:
  57. case DeclarationName::ObjCMultiArgSelector: {
  58. Selector LHSSelector = LHS.getObjCSelector();
  59. Selector RHSSelector = RHS.getObjCSelector();
  60. // getNumArgs for ZeroArgSelector returns 0, but we still need to compare.
  61. if (LHS.getNameKind() == DeclarationName::ObjCZeroArgSelector &&
  62. RHS.getNameKind() == DeclarationName::ObjCZeroArgSelector) {
  63. return LHSSelector.getAsIdentifierInfo()->getName().compare(
  64. RHSSelector.getAsIdentifierInfo()->getName());
  65. }
  66. unsigned LN = LHSSelector.getNumArgs(), RN = RHSSelector.getNumArgs();
  67. for (unsigned I = 0, N = std::min(LN, RN); I != N; ++I) {
  68. switch (LHSSelector.getNameForSlot(I).compare(
  69. RHSSelector.getNameForSlot(I))) {
  70. case -1:
  71. return -1;
  72. case 1:
  73. return 1;
  74. default:
  75. break;
  76. }
  77. }
  78. return compareInt(LN, RN);
  79. }
  80. case DeclarationName::CXXConstructorName:
  81. case DeclarationName::CXXDestructorName:
  82. case DeclarationName::CXXConversionFunctionName:
  83. if (QualTypeOrdering()(LHS.getCXXNameType(), RHS.getCXXNameType()))
  84. return -1;
  85. if (QualTypeOrdering()(RHS.getCXXNameType(), LHS.getCXXNameType()))
  86. return 1;
  87. return 0;
  88. case DeclarationName::CXXDeductionGuideName:
  89. // We never want to compare deduction guide names for templates from
  90. // different scopes, so just compare the template-name.
  91. return compare(LHS.getCXXDeductionGuideTemplate()->getDeclName(),
  92. RHS.getCXXDeductionGuideTemplate()->getDeclName());
  93. case DeclarationName::CXXOperatorName:
  94. return compareInt(LHS.getCXXOverloadedOperator(),
  95. RHS.getCXXOverloadedOperator());
  96. case DeclarationName::CXXLiteralOperatorName:
  97. return LHS.getCXXLiteralIdentifier()->getName().compare(
  98. RHS.getCXXLiteralIdentifier()->getName());
  99. case DeclarationName::CXXUsingDirective:
  100. return 0;
  101. }
  102. llvm_unreachable("Invalid DeclarationName Kind!");
  103. }
  104. static void printCXXConstructorDestructorName(QualType ClassType,
  105. raw_ostream &OS,
  106. PrintingPolicy Policy) {
  107. // We know we're printing C++ here. Ensure we print types properly.
  108. Policy.adjustForCPlusPlus();
  109. if (const RecordType *ClassRec = ClassType->getAs<RecordType>()) {
  110. OS << *ClassRec->getDecl();
  111. return;
  112. }
  113. if (Policy.SuppressTemplateArgsInCXXConstructors) {
  114. if (auto *InjTy = ClassType->getAs<InjectedClassNameType>()) {
  115. OS << *InjTy->getDecl();
  116. return;
  117. }
  118. }
  119. ClassType.print(OS, Policy);
  120. }
  121. void DeclarationName::print(raw_ostream &OS,
  122. const PrintingPolicy &Policy) const {
  123. switch (getNameKind()) {
  124. case DeclarationName::Identifier:
  125. if (const IdentifierInfo *II = getAsIdentifierInfo()) {
  126. StringRef Name = II->getName();
  127. // If this is a mangled OpenMP variant name we strip off the mangling for
  128. // printing. It should not be visible to the user at all.
  129. if (II->isMangledOpenMPVariantName()) {
  130. std::pair<StringRef, StringRef> NameContextPair =
  131. Name.split(getOpenMPVariantManglingSeparatorStr());
  132. OS << NameContextPair.first << "["
  133. << OMPTraitInfo(NameContextPair.second) << "]";
  134. } else {
  135. OS << Name;
  136. }
  137. }
  138. return;
  139. case DeclarationName::ObjCZeroArgSelector:
  140. case DeclarationName::ObjCOneArgSelector:
  141. case DeclarationName::ObjCMultiArgSelector:
  142. getObjCSelector().print(OS);
  143. return;
  144. case DeclarationName::CXXConstructorName:
  145. return printCXXConstructorDestructorName(getCXXNameType(), OS, Policy);
  146. case DeclarationName::CXXDestructorName:
  147. OS << '~';
  148. return printCXXConstructorDestructorName(getCXXNameType(), OS, Policy);
  149. case DeclarationName::CXXDeductionGuideName:
  150. OS << "<deduction guide for ";
  151. getCXXDeductionGuideTemplate()->getDeclName().print(OS, Policy);
  152. OS << '>';
  153. return;
  154. case DeclarationName::CXXOperatorName: {
  155. const char *OpName = getOperatorSpelling(getCXXOverloadedOperator());
  156. assert(OpName && "not an overloaded operator");
  157. OS << "operator";
  158. if (OpName[0] >= 'a' && OpName[0] <= 'z')
  159. OS << ' ';
  160. OS << OpName;
  161. return;
  162. }
  163. case DeclarationName::CXXLiteralOperatorName:
  164. OS << "operator\"\"" << getCXXLiteralIdentifier()->getName();
  165. return;
  166. case DeclarationName::CXXConversionFunctionName: {
  167. OS << "operator ";
  168. QualType Type = getCXXNameType();
  169. if (const RecordType *Rec = Type->getAs<RecordType>()) {
  170. OS << *Rec->getDecl();
  171. return;
  172. }
  173. // We know we're printing C++ here, ensure we print 'bool' properly.
  174. PrintingPolicy CXXPolicy = Policy;
  175. CXXPolicy.adjustForCPlusPlus();
  176. Type.print(OS, CXXPolicy);
  177. return;
  178. }
  179. case DeclarationName::CXXUsingDirective:
  180. OS << "<using-directive>";
  181. return;
  182. }
  183. llvm_unreachable("Unexpected declaration name kind");
  184. }
  185. namespace clang {
  186. raw_ostream &operator<<(raw_ostream &OS, DeclarationName N) {
  187. LangOptions LO;
  188. N.print(OS, PrintingPolicy(LO));
  189. return OS;
  190. }
  191. } // namespace clang
  192. bool DeclarationName::isDependentName() const {
  193. QualType T = getCXXNameType();
  194. if (!T.isNull() && T->isDependentType())
  195. return true;
  196. // A class-scope deduction guide in a dependent context has a dependent name.
  197. auto *TD = getCXXDeductionGuideTemplate();
  198. if (TD && TD->getDeclContext()->isDependentContext())
  199. return true;
  200. return false;
  201. }
  202. std::string DeclarationName::getAsString() const {
  203. std::string Result;
  204. llvm::raw_string_ostream OS(Result);
  205. OS << *this;
  206. return Result;
  207. }
  208. void *DeclarationName::getFETokenInfoSlow() const {
  209. switch (getNameKind()) {
  210. case Identifier:
  211. llvm_unreachable("case Identifier already handled by getFETokenInfo!");
  212. case CXXConstructorName:
  213. case CXXDestructorName:
  214. case CXXConversionFunctionName:
  215. return castAsCXXSpecialNameExtra()->FETokenInfo;
  216. case CXXOperatorName:
  217. return castAsCXXOperatorIdName()->FETokenInfo;
  218. case CXXDeductionGuideName:
  219. return castAsCXXDeductionGuideNameExtra()->FETokenInfo;
  220. case CXXLiteralOperatorName:
  221. return castAsCXXLiteralOperatorIdName()->FETokenInfo;
  222. default:
  223. llvm_unreachable("DeclarationName has no FETokenInfo!");
  224. }
  225. }
  226. void DeclarationName::setFETokenInfoSlow(void *T) {
  227. switch (getNameKind()) {
  228. case Identifier:
  229. llvm_unreachable("case Identifier already handled by setFETokenInfo!");
  230. case CXXConstructorName:
  231. case CXXDestructorName:
  232. case CXXConversionFunctionName:
  233. castAsCXXSpecialNameExtra()->FETokenInfo = T;
  234. break;
  235. case CXXOperatorName:
  236. castAsCXXOperatorIdName()->FETokenInfo = T;
  237. break;
  238. case CXXDeductionGuideName:
  239. castAsCXXDeductionGuideNameExtra()->FETokenInfo = T;
  240. break;
  241. case CXXLiteralOperatorName:
  242. castAsCXXLiteralOperatorIdName()->FETokenInfo = T;
  243. break;
  244. default:
  245. llvm_unreachable("DeclarationName has no FETokenInfo!");
  246. }
  247. }
  248. LLVM_DUMP_METHOD void DeclarationName::dump() const {
  249. llvm::errs() << *this << '\n';
  250. }
  251. DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) {
  252. // Initialize the overloaded operator names.
  253. for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op)
  254. CXXOperatorNames[Op].Kind = static_cast<OverloadedOperatorKind>(Op);
  255. }
  256. DeclarationName
  257. DeclarationNameTable::getCXXDeductionGuideName(TemplateDecl *Template) {
  258. Template = cast<TemplateDecl>(Template->getCanonicalDecl());
  259. llvm::FoldingSetNodeID ID;
  260. ID.AddPointer(Template);
  261. void *InsertPos = nullptr;
  262. if (auto *Name = CXXDeductionGuideNames.FindNodeOrInsertPos(ID, InsertPos))
  263. return DeclarationName(Name);
  264. auto *Name = new (Ctx) detail::CXXDeductionGuideNameExtra(Template);
  265. CXXDeductionGuideNames.InsertNode(Name, InsertPos);
  266. return DeclarationName(Name);
  267. }
  268. DeclarationName DeclarationNameTable::getCXXConstructorName(CanQualType Ty) {
  269. // The type of constructors is unqualified.
  270. Ty = Ty.getUnqualifiedType();
  271. // Do we already have this C++ constructor name ?
  272. llvm::FoldingSetNodeID ID;
  273. ID.AddPointer(Ty.getAsOpaquePtr());
  274. void *InsertPos = nullptr;
  275. if (auto *Name = CXXConstructorNames.FindNodeOrInsertPos(ID, InsertPos))
  276. return {Name, DeclarationName::StoredCXXConstructorName};
  277. // We have to create it.
  278. auto *SpecialName = new (Ctx) detail::CXXSpecialNameExtra(Ty);
  279. CXXConstructorNames.InsertNode(SpecialName, InsertPos);
  280. return {SpecialName, DeclarationName::StoredCXXConstructorName};
  281. }
  282. DeclarationName DeclarationNameTable::getCXXDestructorName(CanQualType Ty) {
  283. // The type of destructors is unqualified.
  284. Ty = Ty.getUnqualifiedType();
  285. // Do we already have this C++ destructor name ?
  286. llvm::FoldingSetNodeID ID;
  287. ID.AddPointer(Ty.getAsOpaquePtr());
  288. void *InsertPos = nullptr;
  289. if (auto *Name = CXXDestructorNames.FindNodeOrInsertPos(ID, InsertPos))
  290. return {Name, DeclarationName::StoredCXXDestructorName};
  291. // We have to create it.
  292. auto *SpecialName = new (Ctx) detail::CXXSpecialNameExtra(Ty);
  293. CXXDestructorNames.InsertNode(SpecialName, InsertPos);
  294. return {SpecialName, DeclarationName::StoredCXXDestructorName};
  295. }
  296. DeclarationName
  297. DeclarationNameTable::getCXXConversionFunctionName(CanQualType Ty) {
  298. // Do we already have this C++ conversion function name ?
  299. llvm::FoldingSetNodeID ID;
  300. ID.AddPointer(Ty.getAsOpaquePtr());
  301. void *InsertPos = nullptr;
  302. if (auto *Name =
  303. CXXConversionFunctionNames.FindNodeOrInsertPos(ID, InsertPos))
  304. return {Name, DeclarationName::StoredCXXConversionFunctionName};
  305. // We have to create it.
  306. auto *SpecialName = new (Ctx) detail::CXXSpecialNameExtra(Ty);
  307. CXXConversionFunctionNames.InsertNode(SpecialName, InsertPos);
  308. return {SpecialName, DeclarationName::StoredCXXConversionFunctionName};
  309. }
  310. DeclarationName
  311. DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
  312. CanQualType Ty) {
  313. switch (Kind) {
  314. case DeclarationName::CXXConstructorName:
  315. return getCXXConstructorName(Ty);
  316. case DeclarationName::CXXDestructorName:
  317. return getCXXDestructorName(Ty);
  318. case DeclarationName::CXXConversionFunctionName:
  319. return getCXXConversionFunctionName(Ty);
  320. default:
  321. llvm_unreachable("Invalid kind in getCXXSpecialName!");
  322. }
  323. }
  324. DeclarationName
  325. DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) {
  326. llvm::FoldingSetNodeID ID;
  327. ID.AddPointer(II);
  328. void *InsertPos = nullptr;
  329. if (auto *Name = CXXLiteralOperatorNames.FindNodeOrInsertPos(ID, InsertPos))
  330. return DeclarationName(Name);
  331. auto *LiteralName = new (Ctx) detail::CXXLiteralOperatorIdName(II);
  332. CXXLiteralOperatorNames.InsertNode(LiteralName, InsertPos);
  333. return DeclarationName(LiteralName);
  334. }
  335. DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) {
  336. switch (Name.getNameKind()) {
  337. case DeclarationName::Identifier:
  338. case DeclarationName::CXXDeductionGuideName:
  339. break;
  340. case DeclarationName::CXXConstructorName:
  341. case DeclarationName::CXXDestructorName:
  342. case DeclarationName::CXXConversionFunctionName:
  343. setNamedTypeLoc(nullptr);
  344. break;
  345. case DeclarationName::CXXOperatorName:
  346. setCXXOperatorNameRange(SourceRange());
  347. break;
  348. case DeclarationName::CXXLiteralOperatorName:
  349. setCXXLiteralOperatorNameLoc(SourceLocation());
  350. break;
  351. case DeclarationName::ObjCZeroArgSelector:
  352. case DeclarationName::ObjCOneArgSelector:
  353. case DeclarationName::ObjCMultiArgSelector:
  354. // FIXME: ?
  355. break;
  356. case DeclarationName::CXXUsingDirective:
  357. break;
  358. }
  359. }
  360. bool DeclarationNameInfo::containsUnexpandedParameterPack() const {
  361. switch (Name.getNameKind()) {
  362. case DeclarationName::Identifier:
  363. case DeclarationName::ObjCZeroArgSelector:
  364. case DeclarationName::ObjCOneArgSelector:
  365. case DeclarationName::ObjCMultiArgSelector:
  366. case DeclarationName::CXXOperatorName:
  367. case DeclarationName::CXXLiteralOperatorName:
  368. case DeclarationName::CXXUsingDirective:
  369. case DeclarationName::CXXDeductionGuideName:
  370. return false;
  371. case DeclarationName::CXXConstructorName:
  372. case DeclarationName::CXXDestructorName:
  373. case DeclarationName::CXXConversionFunctionName:
  374. if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
  375. return TInfo->getType()->containsUnexpandedParameterPack();
  376. return Name.getCXXNameType()->containsUnexpandedParameterPack();
  377. }
  378. llvm_unreachable("All name kinds handled.");
  379. }
  380. bool DeclarationNameInfo::isInstantiationDependent() const {
  381. switch (Name.getNameKind()) {
  382. case DeclarationName::Identifier:
  383. case DeclarationName::ObjCZeroArgSelector:
  384. case DeclarationName::ObjCOneArgSelector:
  385. case DeclarationName::ObjCMultiArgSelector:
  386. case DeclarationName::CXXOperatorName:
  387. case DeclarationName::CXXLiteralOperatorName:
  388. case DeclarationName::CXXUsingDirective:
  389. case DeclarationName::CXXDeductionGuideName:
  390. return false;
  391. case DeclarationName::CXXConstructorName:
  392. case DeclarationName::CXXDestructorName:
  393. case DeclarationName::CXXConversionFunctionName:
  394. if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
  395. return TInfo->getType()->isInstantiationDependentType();
  396. return Name.getCXXNameType()->isInstantiationDependentType();
  397. }
  398. llvm_unreachable("All name kinds handled.");
  399. }
  400. std::string DeclarationNameInfo::getAsString() const {
  401. std::string Result;
  402. llvm::raw_string_ostream OS(Result);
  403. OS << *this;
  404. return Result;
  405. }
  406. raw_ostream &clang::operator<<(raw_ostream &OS, DeclarationNameInfo DNInfo) {
  407. LangOptions LO;
  408. DNInfo.printName(OS, PrintingPolicy(LangOptions()));
  409. return OS;
  410. }
  411. void DeclarationNameInfo::printName(raw_ostream &OS, PrintingPolicy Policy) const {
  412. switch (Name.getNameKind()) {
  413. case DeclarationName::Identifier:
  414. case DeclarationName::ObjCZeroArgSelector:
  415. case DeclarationName::ObjCOneArgSelector:
  416. case DeclarationName::ObjCMultiArgSelector:
  417. case DeclarationName::CXXOperatorName:
  418. case DeclarationName::CXXLiteralOperatorName:
  419. case DeclarationName::CXXUsingDirective:
  420. case DeclarationName::CXXDeductionGuideName:
  421. Name.print(OS, Policy);
  422. return;
  423. case DeclarationName::CXXConstructorName:
  424. case DeclarationName::CXXDestructorName:
  425. case DeclarationName::CXXConversionFunctionName:
  426. if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo()) {
  427. if (Name.getNameKind() == DeclarationName::CXXDestructorName)
  428. OS << '~';
  429. else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName)
  430. OS << "operator ";
  431. LangOptions LO;
  432. Policy.adjustForCPlusPlus();
  433. Policy.SuppressScope = true;
  434. OS << TInfo->getType().getAsString(Policy);
  435. } else
  436. Name.print(OS, Policy);
  437. return;
  438. }
  439. llvm_unreachable("Unexpected declaration name kind");
  440. }
  441. SourceLocation DeclarationNameInfo::getEndLocPrivate() const {
  442. switch (Name.getNameKind()) {
  443. case DeclarationName::Identifier:
  444. case DeclarationName::CXXDeductionGuideName:
  445. return NameLoc;
  446. case DeclarationName::CXXOperatorName:
  447. return LocInfo.getCXXOperatorNameEndLoc();
  448. case DeclarationName::CXXLiteralOperatorName:
  449. return LocInfo.getCXXLiteralOperatorNameLoc();
  450. case DeclarationName::CXXConstructorName:
  451. case DeclarationName::CXXDestructorName:
  452. case DeclarationName::CXXConversionFunctionName:
  453. if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
  454. return TInfo->getTypeLoc().getEndLoc();
  455. else
  456. return NameLoc;
  457. // DNInfo work in progress: FIXME.
  458. case DeclarationName::ObjCZeroArgSelector:
  459. case DeclarationName::ObjCOneArgSelector:
  460. case DeclarationName::ObjCMultiArgSelector:
  461. case DeclarationName::CXXUsingDirective:
  462. return NameLoc;
  463. }
  464. llvm_unreachable("Unexpected declaration name kind");
  465. }