DeclarationName.cpp 17 KB

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