TemplateName.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/DeclTemplate.h"
  16. #include "clang/AST/DependenceFlags.h"
  17. #include "clang/AST/NestedNameSpecifier.h"
  18. #include "clang/AST/PrettyPrinter.h"
  19. #include "clang/AST/TemplateBase.h"
  20. #include "clang/Basic/Diagnostic.h"
  21. #include "clang/Basic/LLVM.h"
  22. #include "clang/Basic/LangOptions.h"
  23. #include "clang/Basic/OperatorKinds.h"
  24. #include "llvm/ADT/ArrayRef.h"
  25. #include "llvm/ADT/FoldingSet.h"
  26. #include "llvm/Support/Casting.h"
  27. #include "llvm/Support/Compiler.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include <cassert>
  30. #include <string>
  31. using namespace clang;
  32. TemplateArgument
  33. SubstTemplateTemplateParmPackStorage::getArgumentPack() const {
  34. return TemplateArgument(llvm::makeArrayRef(Arguments, size()));
  35. }
  36. void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
  37. Profile(ID, Parameter, Replacement);
  38. }
  39. void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID,
  40. TemplateTemplateParmDecl *parameter,
  41. TemplateName replacement) {
  42. ID.AddPointer(parameter);
  43. ID.AddPointer(replacement.getAsVoidPointer());
  44. }
  45. void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
  46. ASTContext &Context) {
  47. Profile(ID, Context, Parameter, getArgumentPack());
  48. }
  49. void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
  50. ASTContext &Context,
  51. TemplateTemplateParmDecl *Parameter,
  52. const TemplateArgument &ArgPack) {
  53. ID.AddPointer(Parameter);
  54. ArgPack.Profile(ID, Context);
  55. }
  56. TemplateName::TemplateName(void *Ptr) {
  57. Storage = StorageType::getFromOpaqueValue(Ptr);
  58. }
  59. TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
  60. TemplateName::TemplateName(OverloadedTemplateStorage *Storage)
  61. : Storage(Storage) {}
  62. TemplateName::TemplateName(AssumedTemplateStorage *Storage)
  63. : Storage(Storage) {}
  64. TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
  65. : Storage(Storage) {}
  66. TemplateName::TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
  67. : Storage(Storage) {}
  68. TemplateName::TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) {}
  69. TemplateName::TemplateName(DependentTemplateName *Dep) : Storage(Dep) {}
  70. bool TemplateName::isNull() const { return Storage.isNull(); }
  71. TemplateName::NameKind TemplateName::getKind() const {
  72. if (Storage.is<TemplateDecl *>())
  73. return Template;
  74. if (Storage.is<DependentTemplateName *>())
  75. return DependentTemplate;
  76. if (Storage.is<QualifiedTemplateName *>())
  77. return QualifiedTemplate;
  78. UncommonTemplateNameStorage *uncommon
  79. = Storage.get<UncommonTemplateNameStorage*>();
  80. if (uncommon->getAsOverloadedStorage())
  81. return OverloadedTemplate;
  82. if (uncommon->getAsAssumedTemplateName())
  83. return AssumedTemplate;
  84. if (uncommon->getAsSubstTemplateTemplateParm())
  85. return SubstTemplateTemplateParm;
  86. return SubstTemplateTemplateParmPack;
  87. }
  88. TemplateDecl *TemplateName::getAsTemplateDecl() const {
  89. if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
  90. return Template;
  91. if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
  92. return QTN->getTemplateDecl();
  93. if (SubstTemplateTemplateParmStorage *sub = getAsSubstTemplateTemplateParm())
  94. return sub->getReplacement().getAsTemplateDecl();
  95. return nullptr;
  96. }
  97. OverloadedTemplateStorage *TemplateName::getAsOverloadedTemplate() const {
  98. if (UncommonTemplateNameStorage *Uncommon =
  99. Storage.dyn_cast<UncommonTemplateNameStorage *>())
  100. return Uncommon->getAsOverloadedStorage();
  101. return nullptr;
  102. }
  103. AssumedTemplateStorage *TemplateName::getAsAssumedTemplateName() const {
  104. if (UncommonTemplateNameStorage *Uncommon =
  105. Storage.dyn_cast<UncommonTemplateNameStorage *>())
  106. return Uncommon->getAsAssumedTemplateName();
  107. return nullptr;
  108. }
  109. SubstTemplateTemplateParmStorage *
  110. TemplateName::getAsSubstTemplateTemplateParm() const {
  111. if (UncommonTemplateNameStorage *uncommon =
  112. Storage.dyn_cast<UncommonTemplateNameStorage *>())
  113. return uncommon->getAsSubstTemplateTemplateParm();
  114. return nullptr;
  115. }
  116. SubstTemplateTemplateParmPackStorage *
  117. TemplateName::getAsSubstTemplateTemplateParmPack() const {
  118. if (UncommonTemplateNameStorage *Uncommon =
  119. Storage.dyn_cast<UncommonTemplateNameStorage *>())
  120. return Uncommon->getAsSubstTemplateTemplateParmPack();
  121. return nullptr;
  122. }
  123. QualifiedTemplateName *TemplateName::getAsQualifiedTemplateName() const {
  124. return Storage.dyn_cast<QualifiedTemplateName *>();
  125. }
  126. DependentTemplateName *TemplateName::getAsDependentTemplateName() const {
  127. return Storage.dyn_cast<DependentTemplateName *>();
  128. }
  129. TemplateName TemplateName::getNameToSubstitute() const {
  130. TemplateDecl *Decl = getAsTemplateDecl();
  131. // Substituting a dependent template name: preserve it as written.
  132. if (!Decl)
  133. return *this;
  134. // If we have a template declaration, use the most recent non-friend
  135. // declaration of that template.
  136. Decl = cast<TemplateDecl>(Decl->getMostRecentDecl());
  137. while (Decl->getFriendObjectKind()) {
  138. Decl = cast<TemplateDecl>(Decl->getPreviousDecl());
  139. assert(Decl && "all declarations of template are friends");
  140. }
  141. return TemplateName(Decl);
  142. }
  143. TemplateNameDependence TemplateName::getDependence() const {
  144. auto D = TemplateNameDependence::None;
  145. switch (getKind()) {
  146. case TemplateName::NameKind::QualifiedTemplate:
  147. D |= toTemplateNameDependence(
  148. getAsQualifiedTemplateName()->getQualifier()->getDependence());
  149. break;
  150. case TemplateName::NameKind::DependentTemplate:
  151. D |= toTemplateNameDependence(
  152. getAsDependentTemplateName()->getQualifier()->getDependence());
  153. break;
  154. case TemplateName::NameKind::SubstTemplateTemplateParmPack:
  155. D |= TemplateNameDependence::UnexpandedPack;
  156. break;
  157. case TemplateName::NameKind::OverloadedTemplate:
  158. llvm_unreachable("overloaded templates shouldn't survive to here.");
  159. default:
  160. break;
  161. }
  162. if (TemplateDecl *Template = getAsTemplateDecl()) {
  163. if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Template)) {
  164. D |= TemplateNameDependence::DependentInstantiation;
  165. if (TTP->isParameterPack())
  166. D |= TemplateNameDependence::UnexpandedPack;
  167. }
  168. // FIXME: Hack, getDeclContext() can be null if Template is still
  169. // initializing due to PCH reading, so we check it before using it.
  170. // Should probably modify TemplateSpecializationType to allow constructing
  171. // it without the isDependent() checking.
  172. if (Template->getDeclContext() &&
  173. Template->getDeclContext()->isDependentContext())
  174. D |= TemplateNameDependence::DependentInstantiation;
  175. } else {
  176. D |= TemplateNameDependence::DependentInstantiation;
  177. }
  178. return D;
  179. }
  180. bool TemplateName::isDependent() const {
  181. return getDependence() & TemplateNameDependence::Dependent;
  182. }
  183. bool TemplateName::isInstantiationDependent() const {
  184. return getDependence() & TemplateNameDependence::Instantiation;
  185. }
  186. bool TemplateName::containsUnexpandedParameterPack() const {
  187. return getDependence() & TemplateNameDependence::UnexpandedPack;
  188. }
  189. void TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
  190. Qualified Qual) const {
  191. if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
  192. if (Policy.CleanUglifiedParameters &&
  193. isa<TemplateTemplateParmDecl>(Template) && Template->getIdentifier())
  194. OS << Template->getIdentifier()->deuglifiedName();
  195. else if (Qual == Qualified::Fully &&
  196. getDependence() !=
  197. TemplateNameDependenceScope::DependentInstantiation)
  198. Template->printQualifiedName(OS, Policy);
  199. else
  200. OS << *Template;
  201. else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
  202. if (Qual == Qualified::Fully &&
  203. getDependence() !=
  204. TemplateNameDependenceScope::DependentInstantiation) {
  205. QTN->getTemplateDecl()->printQualifiedName(OS, Policy);
  206. return;
  207. }
  208. if (Qual == Qualified::AsWritten)
  209. QTN->getQualifier()->print(OS, Policy);
  210. if (QTN->hasTemplateKeyword())
  211. OS << "template ";
  212. OS << *QTN->getDecl();
  213. } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
  214. if (Qual == Qualified::AsWritten && DTN->getQualifier())
  215. DTN->getQualifier()->print(OS, Policy);
  216. OS << "template ";
  217. if (DTN->isIdentifier())
  218. OS << DTN->getIdentifier()->getName();
  219. else
  220. OS << "operator " << getOperatorSpelling(DTN->getOperator());
  221. } else if (SubstTemplateTemplateParmStorage *subst
  222. = getAsSubstTemplateTemplateParm()) {
  223. subst->getReplacement().print(OS, Policy, Qual);
  224. } else if (SubstTemplateTemplateParmPackStorage *SubstPack
  225. = getAsSubstTemplateTemplateParmPack())
  226. OS << *SubstPack->getParameterPack();
  227. else if (AssumedTemplateStorage *Assumed = getAsAssumedTemplateName()) {
  228. Assumed->getDeclName().print(OS, Policy);
  229. } else {
  230. OverloadedTemplateStorage *OTS = getAsOverloadedTemplate();
  231. (*OTS->begin())->printName(OS);
  232. }
  233. }
  234. const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
  235. TemplateName N) {
  236. std::string NameStr;
  237. llvm::raw_string_ostream OS(NameStr);
  238. LangOptions LO;
  239. LO.CPlusPlus = true;
  240. LO.Bool = true;
  241. OS << '\'';
  242. N.print(OS, PrintingPolicy(LO));
  243. OS << '\'';
  244. OS.flush();
  245. return DB << NameStr;
  246. }
  247. void TemplateName::dump(raw_ostream &OS) const {
  248. LangOptions LO; // FIXME!
  249. LO.CPlusPlus = true;
  250. LO.Bool = true;
  251. print(OS, PrintingPolicy(LO));
  252. }
  253. LLVM_DUMP_METHOD void TemplateName::dump() const {
  254. dump(llvm::errs());
  255. }