TemplateName.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //===- TemplateName.cpp - C++ Template Name Representation ----------------===//
  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 defines the TemplateName interface and subclasses.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/TemplateName.h"
  13. #include "clang/AST/Decl.h"
  14. #include "clang/AST/DeclBase.h"
  15. #include "clang/AST/DeclCXX.h"
  16. #include "clang/AST/DeclTemplate.h"
  17. #include "clang/AST/DependenceFlags.h"
  18. #include "clang/AST/NestedNameSpecifier.h"
  19. #include "clang/AST/PrettyPrinter.h"
  20. #include "clang/AST/TemplateBase.h"
  21. #include "clang/Basic/Diagnostic.h"
  22. #include "clang/Basic/LLVM.h"
  23. #include "clang/Basic/LangOptions.h"
  24. #include "clang/Basic/OperatorKinds.h"
  25. #include "llvm/ADT/ArrayRef.h"
  26. #include "llvm/ADT/FoldingSet.h"
  27. #include "llvm/Support/Casting.h"
  28. #include "llvm/Support/Compiler.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. #include <cassert>
  31. #include <optional>
  32. #include <string>
  33. using namespace clang;
  34. TemplateArgument
  35. SubstTemplateTemplateParmPackStorage::getArgumentPack() const {
  36. return TemplateArgument(llvm::ArrayRef(Arguments, Bits.Data));
  37. }
  38. TemplateTemplateParmDecl *
  39. SubstTemplateTemplateParmPackStorage::getParameterPack() const {
  40. return cast<TemplateTemplateParmDecl>(
  41. getReplacedTemplateParameterList(getAssociatedDecl())
  42. ->asArray()[Bits.Index]);
  43. }
  44. TemplateTemplateParmDecl *
  45. SubstTemplateTemplateParmStorage::getParameter() const {
  46. return cast<TemplateTemplateParmDecl>(
  47. getReplacedTemplateParameterList(getAssociatedDecl())
  48. ->asArray()[Bits.Index]);
  49. }
  50. void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
  51. Profile(ID, Replacement, getAssociatedDecl(), getIndex(), getPackIndex());
  52. }
  53. void SubstTemplateTemplateParmStorage::Profile(
  54. llvm::FoldingSetNodeID &ID, TemplateName Replacement, Decl *AssociatedDecl,
  55. unsigned Index, std::optional<unsigned> PackIndex) {
  56. Replacement.Profile(ID);
  57. ID.AddPointer(AssociatedDecl);
  58. ID.AddInteger(Index);
  59. ID.AddInteger(PackIndex ? *PackIndex + 1 : 0);
  60. }
  61. SubstTemplateTemplateParmPackStorage::SubstTemplateTemplateParmPackStorage(
  62. ArrayRef<TemplateArgument> ArgPack, Decl *AssociatedDecl, unsigned Index,
  63. bool Final)
  64. : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Index,
  65. ArgPack.size()),
  66. Arguments(ArgPack.data()), AssociatedDeclAndFinal(AssociatedDecl, Final) {
  67. assert(AssociatedDecl != nullptr);
  68. }
  69. void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
  70. ASTContext &Context) {
  71. Profile(ID, Context, getArgumentPack(), getAssociatedDecl(), getIndex(),
  72. getFinal());
  73. }
  74. Decl *SubstTemplateTemplateParmPackStorage::getAssociatedDecl() const {
  75. return AssociatedDeclAndFinal.getPointer();
  76. }
  77. bool SubstTemplateTemplateParmPackStorage::getFinal() const {
  78. return AssociatedDeclAndFinal.getInt();
  79. }
  80. void SubstTemplateTemplateParmPackStorage::Profile(
  81. llvm::FoldingSetNodeID &ID, ASTContext &Context,
  82. const TemplateArgument &ArgPack, Decl *AssociatedDecl, unsigned Index,
  83. bool Final) {
  84. ArgPack.Profile(ID, Context);
  85. ID.AddPointer(AssociatedDecl);
  86. ID.AddInteger(Index);
  87. ID.AddBoolean(Final);
  88. }
  89. TemplateName::TemplateName(void *Ptr) {
  90. Storage = StorageType::getFromOpaqueValue(Ptr);
  91. }
  92. TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
  93. TemplateName::TemplateName(OverloadedTemplateStorage *Storage)
  94. : Storage(Storage) {}
  95. TemplateName::TemplateName(AssumedTemplateStorage *Storage)
  96. : Storage(Storage) {}
  97. TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
  98. : Storage(Storage) {}
  99. TemplateName::TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
  100. : Storage(Storage) {}
  101. TemplateName::TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) {}
  102. TemplateName::TemplateName(DependentTemplateName *Dep) : Storage(Dep) {}
  103. TemplateName::TemplateName(UsingShadowDecl *Using) : Storage(Using) {}
  104. bool TemplateName::isNull() const { return Storage.isNull(); }
  105. TemplateName::NameKind TemplateName::getKind() const {
  106. if (auto *ND = Storage.dyn_cast<Decl *>()) {
  107. if (isa<UsingShadowDecl>(ND))
  108. return UsingTemplate;
  109. assert(isa<TemplateDecl>(ND));
  110. return Template;
  111. }
  112. if (Storage.is<DependentTemplateName *>())
  113. return DependentTemplate;
  114. if (Storage.is<QualifiedTemplateName *>())
  115. return QualifiedTemplate;
  116. UncommonTemplateNameStorage *uncommon
  117. = Storage.get<UncommonTemplateNameStorage*>();
  118. if (uncommon->getAsOverloadedStorage())
  119. return OverloadedTemplate;
  120. if (uncommon->getAsAssumedTemplateName())
  121. return AssumedTemplate;
  122. if (uncommon->getAsSubstTemplateTemplateParm())
  123. return SubstTemplateTemplateParm;
  124. return SubstTemplateTemplateParmPack;
  125. }
  126. TemplateDecl *TemplateName::getAsTemplateDecl() const {
  127. if (Decl *TemplateOrUsing = Storage.dyn_cast<Decl *>()) {
  128. if (UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(TemplateOrUsing))
  129. return cast<TemplateDecl>(USD->getTargetDecl());
  130. assert(isa<TemplateDecl>(TemplateOrUsing));
  131. return cast<TemplateDecl>(TemplateOrUsing);
  132. }
  133. if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
  134. return QTN->getUnderlyingTemplate().getAsTemplateDecl();
  135. if (SubstTemplateTemplateParmStorage *sub = getAsSubstTemplateTemplateParm())
  136. return sub->getReplacement().getAsTemplateDecl();
  137. if (UsingShadowDecl *USD = getAsUsingShadowDecl())
  138. return cast<TemplateDecl>(USD->getTargetDecl());
  139. return nullptr;
  140. }
  141. OverloadedTemplateStorage *TemplateName::getAsOverloadedTemplate() const {
  142. if (UncommonTemplateNameStorage *Uncommon =
  143. Storage.dyn_cast<UncommonTemplateNameStorage *>())
  144. return Uncommon->getAsOverloadedStorage();
  145. return nullptr;
  146. }
  147. AssumedTemplateStorage *TemplateName::getAsAssumedTemplateName() const {
  148. if (UncommonTemplateNameStorage *Uncommon =
  149. Storage.dyn_cast<UncommonTemplateNameStorage *>())
  150. return Uncommon->getAsAssumedTemplateName();
  151. return nullptr;
  152. }
  153. SubstTemplateTemplateParmStorage *
  154. TemplateName::getAsSubstTemplateTemplateParm() const {
  155. if (UncommonTemplateNameStorage *uncommon =
  156. Storage.dyn_cast<UncommonTemplateNameStorage *>())
  157. return uncommon->getAsSubstTemplateTemplateParm();
  158. return nullptr;
  159. }
  160. SubstTemplateTemplateParmPackStorage *
  161. TemplateName::getAsSubstTemplateTemplateParmPack() const {
  162. if (UncommonTemplateNameStorage *Uncommon =
  163. Storage.dyn_cast<UncommonTemplateNameStorage *>())
  164. return Uncommon->getAsSubstTemplateTemplateParmPack();
  165. return nullptr;
  166. }
  167. QualifiedTemplateName *TemplateName::getAsQualifiedTemplateName() const {
  168. return Storage.dyn_cast<QualifiedTemplateName *>();
  169. }
  170. DependentTemplateName *TemplateName::getAsDependentTemplateName() const {
  171. return Storage.dyn_cast<DependentTemplateName *>();
  172. }
  173. UsingShadowDecl *TemplateName::getAsUsingShadowDecl() const {
  174. if (Decl *D = Storage.dyn_cast<Decl *>())
  175. if (UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D))
  176. return USD;
  177. if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
  178. return QTN->getUnderlyingTemplate().getAsUsingShadowDecl();
  179. return nullptr;
  180. }
  181. TemplateName TemplateName::getNameToSubstitute() const {
  182. TemplateDecl *Decl = getAsTemplateDecl();
  183. // Substituting a dependent template name: preserve it as written.
  184. if (!Decl)
  185. return *this;
  186. // If we have a template declaration, use the most recent non-friend
  187. // declaration of that template.
  188. Decl = cast<TemplateDecl>(Decl->getMostRecentDecl());
  189. while (Decl->getFriendObjectKind()) {
  190. Decl = cast<TemplateDecl>(Decl->getPreviousDecl());
  191. assert(Decl && "all declarations of template are friends");
  192. }
  193. return TemplateName(Decl);
  194. }
  195. TemplateNameDependence TemplateName::getDependence() const {
  196. auto D = TemplateNameDependence::None;
  197. switch (getKind()) {
  198. case TemplateName::NameKind::QualifiedTemplate:
  199. D |= toTemplateNameDependence(
  200. getAsQualifiedTemplateName()->getQualifier()->getDependence());
  201. break;
  202. case TemplateName::NameKind::DependentTemplate:
  203. D |= toTemplateNameDependence(
  204. getAsDependentTemplateName()->getQualifier()->getDependence());
  205. break;
  206. case TemplateName::NameKind::SubstTemplateTemplateParmPack:
  207. D |= TemplateNameDependence::UnexpandedPack;
  208. break;
  209. case TemplateName::NameKind::OverloadedTemplate:
  210. llvm_unreachable("overloaded templates shouldn't survive to here.");
  211. default:
  212. break;
  213. }
  214. if (TemplateDecl *Template = getAsTemplateDecl()) {
  215. if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Template)) {
  216. D |= TemplateNameDependence::DependentInstantiation;
  217. if (TTP->isParameterPack())
  218. D |= TemplateNameDependence::UnexpandedPack;
  219. }
  220. // FIXME: Hack, getDeclContext() can be null if Template is still
  221. // initializing due to PCH reading, so we check it before using it.
  222. // Should probably modify TemplateSpecializationType to allow constructing
  223. // it without the isDependent() checking.
  224. if (Template->getDeclContext() &&
  225. Template->getDeclContext()->isDependentContext())
  226. D |= TemplateNameDependence::DependentInstantiation;
  227. } else {
  228. D |= TemplateNameDependence::DependentInstantiation;
  229. }
  230. return D;
  231. }
  232. bool TemplateName::isDependent() const {
  233. return getDependence() & TemplateNameDependence::Dependent;
  234. }
  235. bool TemplateName::isInstantiationDependent() const {
  236. return getDependence() & TemplateNameDependence::Instantiation;
  237. }
  238. bool TemplateName::containsUnexpandedParameterPack() const {
  239. return getDependence() & TemplateNameDependence::UnexpandedPack;
  240. }
  241. void TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
  242. Qualified Qual) const {
  243. auto Kind = getKind();
  244. TemplateDecl *Template = nullptr;
  245. if (Kind == TemplateName::Template || Kind == TemplateName::UsingTemplate) {
  246. // After `namespace ns { using std::vector }`, what is the fully-qualified
  247. // name of the UsingTemplateName `vector` within ns?
  248. //
  249. // - ns::vector (the qualified name of the using-shadow decl)
  250. // - std::vector (the qualified name of the underlying template decl)
  251. //
  252. // Similar to the UsingType behavior, using declarations are used to import
  253. // names more often than to export them, thus using the original name is
  254. // most useful in this case.
  255. Template = getAsTemplateDecl();
  256. }
  257. if (Template)
  258. if (Policy.CleanUglifiedParameters &&
  259. isa<TemplateTemplateParmDecl>(Template) && Template->getIdentifier())
  260. OS << Template->getIdentifier()->deuglifiedName();
  261. else if (Qual == Qualified::Fully &&
  262. getDependence() !=
  263. TemplateNameDependenceScope::DependentInstantiation)
  264. Template->printQualifiedName(OS, Policy);
  265. else
  266. OS << *Template;
  267. else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
  268. if (Qual == Qualified::Fully &&
  269. getDependence() !=
  270. TemplateNameDependenceScope::DependentInstantiation) {
  271. QTN->getUnderlyingTemplate().getAsTemplateDecl()->printQualifiedName(
  272. OS, Policy);
  273. return;
  274. }
  275. if (Qual == Qualified::AsWritten)
  276. QTN->getQualifier()->print(OS, Policy);
  277. if (QTN->hasTemplateKeyword())
  278. OS << "template ";
  279. OS << *QTN->getUnderlyingTemplate().getAsTemplateDecl();
  280. } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
  281. if (Qual == Qualified::AsWritten && DTN->getQualifier())
  282. DTN->getQualifier()->print(OS, Policy);
  283. OS << "template ";
  284. if (DTN->isIdentifier())
  285. OS << DTN->getIdentifier()->getName();
  286. else
  287. OS << "operator " << getOperatorSpelling(DTN->getOperator());
  288. } else if (SubstTemplateTemplateParmStorage *subst
  289. = getAsSubstTemplateTemplateParm()) {
  290. subst->getReplacement().print(OS, Policy, Qual);
  291. } else if (SubstTemplateTemplateParmPackStorage *SubstPack
  292. = getAsSubstTemplateTemplateParmPack())
  293. OS << *SubstPack->getParameterPack();
  294. else if (AssumedTemplateStorage *Assumed = getAsAssumedTemplateName()) {
  295. Assumed->getDeclName().print(OS, Policy);
  296. } else {
  297. assert(getKind() == TemplateName::OverloadedTemplate);
  298. OverloadedTemplateStorage *OTS = getAsOverloadedTemplate();
  299. (*OTS->begin())->printName(OS, Policy);
  300. }
  301. }
  302. const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
  303. TemplateName N) {
  304. std::string NameStr;
  305. llvm::raw_string_ostream OS(NameStr);
  306. LangOptions LO;
  307. LO.CPlusPlus = true;
  308. LO.Bool = true;
  309. OS << '\'';
  310. N.print(OS, PrintingPolicy(LO));
  311. OS << '\'';
  312. OS.flush();
  313. return DB << NameStr;
  314. }
  315. void TemplateName::dump(raw_ostream &OS) const {
  316. LangOptions LO; // FIXME!
  317. LO.CPlusPlus = true;
  318. LO.Bool = true;
  319. print(OS, PrintingPolicy(LO));
  320. }
  321. LLVM_DUMP_METHOD void TemplateName::dump() const {
  322. dump(llvm::errs());
  323. }