QualTypeNames.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. //===------- QualTypeNames.cpp - Generate Complete QualType Names ---------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. //===----------------------------------------------------------------------===//
  6. //
  7. // This file is distributed under the University of Illinois Open Source
  8. // License. See LICENSE.TXT for details.
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "clang/AST/DeclTemplate.h"
  12. #include "clang/AST/DeclarationName.h"
  13. #include "clang/AST/GlobalDecl.h"
  14. #include "clang/AST/Mangle.h"
  15. #include "clang/AST/QualTypeNames.h"
  16. #include <stdio.h>
  17. #include <memory>
  18. namespace clang {
  19. namespace TypeName {
  20. /// \brief Create a NestedNameSpecifier for Namesp and its enclosing
  21. /// scopes.
  22. ///
  23. /// \param[in] Ctx - the AST Context to be used.
  24. /// \param[in] Namesp - the NamespaceDecl for which a NestedNameSpecifier
  25. /// is requested.
  26. /// \param[in] WithGlobalNsPrefix - Indicate whether the global namespace
  27. /// specifier "::" should be prepended or not.
  28. static NestedNameSpecifier *createNestedNameSpecifier(
  29. const ASTContext &Ctx,
  30. const NamespaceDecl *Namesp,
  31. bool WithGlobalNsPrefix);
  32. /// \brief Create a NestedNameSpecifier for TagDecl and its enclosing
  33. /// scopes.
  34. ///
  35. /// \param[in] Ctx - the AST Context to be used.
  36. /// \param[in] TD - the TagDecl for which a NestedNameSpecifier is
  37. /// requested.
  38. /// \param[in] FullyQualify - Convert all template arguments into fully
  39. /// qualified names.
  40. /// \param[in] WithGlobalNsPrefix - Indicate whether the global namespace
  41. /// specifier "::" should be prepended or not.
  42. static NestedNameSpecifier *createNestedNameSpecifier(
  43. const ASTContext &Ctx, const TypeDecl *TD,
  44. bool FullyQualify, bool WithGlobalNsPrefix);
  45. static NestedNameSpecifier *createNestedNameSpecifierForScopeOf(
  46. const ASTContext &Ctx, const Decl *decl,
  47. bool FullyQualified, bool WithGlobalNsPrefix);
  48. static NestedNameSpecifier *getFullyQualifiedNestedNameSpecifier(
  49. const ASTContext &Ctx, NestedNameSpecifier *scope, bool WithGlobalNsPrefix);
  50. static bool getFullyQualifiedTemplateName(const ASTContext &Ctx,
  51. TemplateName &TName,
  52. bool WithGlobalNsPrefix) {
  53. bool Changed = false;
  54. NestedNameSpecifier *NNS = nullptr;
  55. TemplateDecl *ArgTDecl = TName.getAsTemplateDecl();
  56. // ArgTDecl won't be NULL because we asserted that this isn't a
  57. // dependent context very early in the call chain.
  58. assert(ArgTDecl != nullptr);
  59. QualifiedTemplateName *QTName = TName.getAsQualifiedTemplateName();
  60. if (QTName && !QTName->hasTemplateKeyword()) {
  61. NNS = QTName->getQualifier();
  62. NestedNameSpecifier *QNNS = getFullyQualifiedNestedNameSpecifier(
  63. Ctx, NNS, WithGlobalNsPrefix);
  64. if (QNNS != NNS) {
  65. Changed = true;
  66. NNS = QNNS;
  67. } else {
  68. NNS = nullptr;
  69. }
  70. } else {
  71. NNS = createNestedNameSpecifierForScopeOf(
  72. Ctx, ArgTDecl, true, WithGlobalNsPrefix);
  73. }
  74. if (NNS) {
  75. TName = Ctx.getQualifiedTemplateName(NNS,
  76. /*TemplateKeyword=*/false, ArgTDecl);
  77. Changed = true;
  78. }
  79. return Changed;
  80. }
  81. static bool getFullyQualifiedTemplateArgument(const ASTContext &Ctx,
  82. TemplateArgument &Arg,
  83. bool WithGlobalNsPrefix) {
  84. bool Changed = false;
  85. // Note: we do not handle TemplateArgument::Expression, to replace it
  86. // we need the information for the template instance decl.
  87. if (Arg.getKind() == TemplateArgument::Template) {
  88. TemplateName TName = Arg.getAsTemplate();
  89. Changed = getFullyQualifiedTemplateName(Ctx, TName, WithGlobalNsPrefix);
  90. if (Changed) {
  91. Arg = TemplateArgument(TName);
  92. }
  93. } else if (Arg.getKind() == TemplateArgument::Type) {
  94. QualType SubTy = Arg.getAsType();
  95. // Check if the type needs more desugaring and recurse.
  96. QualType QTFQ = getFullyQualifiedType(SubTy, Ctx, WithGlobalNsPrefix);
  97. if (QTFQ != SubTy) {
  98. Arg = TemplateArgument(QTFQ);
  99. Changed = true;
  100. }
  101. }
  102. return Changed;
  103. }
  104. static const Type *getFullyQualifiedTemplateType(const ASTContext &Ctx,
  105. const Type *TypePtr,
  106. bool WithGlobalNsPrefix) {
  107. // DependentTemplateTypes exist within template declarations and
  108. // definitions. Therefore we shouldn't encounter them at the end of
  109. // a translation unit. If we do, the caller has made an error.
  110. assert(!isa<DependentTemplateSpecializationType>(TypePtr));
  111. // In case of template specializations, iterate over the arguments
  112. // and fully qualify them as well.
  113. if (const auto *TST = dyn_cast<const TemplateSpecializationType>(TypePtr)) {
  114. bool MightHaveChanged = false;
  115. SmallVector<TemplateArgument, 4> FQArgs;
  116. for (TemplateSpecializationType::iterator I = TST->begin(), E = TST->end();
  117. I != E; ++I) {
  118. // Cheap to copy and potentially modified by
  119. // getFullyQualifedTemplateArgument.
  120. TemplateArgument Arg(*I);
  121. MightHaveChanged |= getFullyQualifiedTemplateArgument(
  122. Ctx, Arg, WithGlobalNsPrefix);
  123. FQArgs.push_back(Arg);
  124. }
  125. // If a fully qualified arg is different from the unqualified arg,
  126. // allocate new type in the AST.
  127. if (MightHaveChanged) {
  128. QualType QT = Ctx.getTemplateSpecializationType(
  129. TST->getTemplateName(), FQArgs,
  130. TST->getCanonicalTypeInternal());
  131. // getTemplateSpecializationType returns a fully qualified
  132. // version of the specialization itself, so no need to qualify
  133. // it.
  134. return QT.getTypePtr();
  135. }
  136. } else if (const auto *TSTRecord = dyn_cast<const RecordType>(TypePtr)) {
  137. // We are asked to fully qualify and we have a Record Type,
  138. // which can point to a template instantiation with no sugar in any of
  139. // its template argument, however we still need to fully qualify them.
  140. if (const auto *TSTDecl =
  141. dyn_cast<ClassTemplateSpecializationDecl>(TSTRecord->getDecl())) {
  142. const TemplateArgumentList &TemplateArgs = TSTDecl->getTemplateArgs();
  143. bool MightHaveChanged = false;
  144. SmallVector<TemplateArgument, 4> FQArgs;
  145. for (unsigned int I = 0, E = TemplateArgs.size(); I != E; ++I) {
  146. // cheap to copy and potentially modified by
  147. // getFullyQualifedTemplateArgument
  148. TemplateArgument Arg(TemplateArgs[I]);
  149. MightHaveChanged |= getFullyQualifiedTemplateArgument(
  150. Ctx, Arg, WithGlobalNsPrefix);
  151. FQArgs.push_back(Arg);
  152. }
  153. // If a fully qualified arg is different from the unqualified arg,
  154. // allocate new type in the AST.
  155. if (MightHaveChanged) {
  156. TemplateName TN(TSTDecl->getSpecializedTemplate());
  157. QualType QT = Ctx.getTemplateSpecializationType(
  158. TN, FQArgs,
  159. TSTRecord->getCanonicalTypeInternal());
  160. // getTemplateSpecializationType returns a fully qualified
  161. // version of the specialization itself, so no need to qualify
  162. // it.
  163. return QT.getTypePtr();
  164. }
  165. }
  166. }
  167. return TypePtr;
  168. }
  169. static NestedNameSpecifier *createOuterNNS(const ASTContext &Ctx, const Decl *D,
  170. bool FullyQualify,
  171. bool WithGlobalNsPrefix) {
  172. const DeclContext *DC = D->getDeclContext();
  173. if (const auto *NS = dyn_cast<NamespaceDecl>(DC)) {
  174. while (NS && NS->isInline()) {
  175. // Ignore inline namespace;
  176. NS = dyn_cast<NamespaceDecl>(NS->getDeclContext());
  177. }
  178. if (NS->getDeclName()) {
  179. return createNestedNameSpecifier(Ctx, NS, WithGlobalNsPrefix);
  180. }
  181. return nullptr; // no starting '::', no anonymous
  182. } else if (const auto *TD = dyn_cast<TagDecl>(DC)) {
  183. return createNestedNameSpecifier(Ctx, TD, FullyQualify, WithGlobalNsPrefix);
  184. } else if (const auto *TDD = dyn_cast<TypedefNameDecl>(DC)) {
  185. return createNestedNameSpecifier(
  186. Ctx, TDD, FullyQualify, WithGlobalNsPrefix);
  187. } else if (WithGlobalNsPrefix && DC->isTranslationUnit()) {
  188. return NestedNameSpecifier::GlobalSpecifier(Ctx);
  189. }
  190. return nullptr; // no starting '::' if |WithGlobalNsPrefix| is false
  191. }
  192. /// \brief Return a fully qualified version of this name specifier.
  193. static NestedNameSpecifier *getFullyQualifiedNestedNameSpecifier(
  194. const ASTContext &Ctx, NestedNameSpecifier *Scope,
  195. bool WithGlobalNsPrefix) {
  196. switch (Scope->getKind()) {
  197. case NestedNameSpecifier::Global:
  198. // Already fully qualified
  199. return Scope;
  200. case NestedNameSpecifier::Namespace:
  201. return TypeName::createNestedNameSpecifier(
  202. Ctx, Scope->getAsNamespace(), WithGlobalNsPrefix);
  203. case NestedNameSpecifier::NamespaceAlias:
  204. // Namespace aliases are only valid for the duration of the
  205. // scope where they were introduced, and therefore are often
  206. // invalid at the end of the TU. So use the namespace name more
  207. // likely to be valid at the end of the TU.
  208. return TypeName::createNestedNameSpecifier(
  209. Ctx,
  210. Scope->getAsNamespaceAlias()->getNamespace()->getCanonicalDecl(),
  211. WithGlobalNsPrefix);
  212. case NestedNameSpecifier::Identifier:
  213. // A function or some other construct that makes it un-namable
  214. // at the end of the TU. Skip the current component of the name,
  215. // but use the name of it's prefix.
  216. return getFullyQualifiedNestedNameSpecifier(
  217. Ctx, Scope->getPrefix(), WithGlobalNsPrefix);
  218. case NestedNameSpecifier::Super:
  219. case NestedNameSpecifier::TypeSpec:
  220. case NestedNameSpecifier::TypeSpecWithTemplate: {
  221. const Type *Type = Scope->getAsType();
  222. // Find decl context.
  223. const TagDecl *TD = nullptr;
  224. if (const TagType *TagDeclType = Type->getAs<TagType>()) {
  225. TD = TagDeclType->getDecl();
  226. } else {
  227. TD = Type->getAsCXXRecordDecl();
  228. }
  229. if (TD) {
  230. return TypeName::createNestedNameSpecifier(Ctx, TD,
  231. true /*FullyQualified*/,
  232. WithGlobalNsPrefix);
  233. } else if (const auto *TDD = dyn_cast<TypedefType>(Type)) {
  234. return TypeName::createNestedNameSpecifier(Ctx, TDD->getDecl(),
  235. true /*FullyQualified*/,
  236. WithGlobalNsPrefix);
  237. }
  238. return Scope;
  239. }
  240. }
  241. llvm_unreachable("bad NNS kind");
  242. }
  243. /// \brief Create a nested name specifier for the declaring context of
  244. /// the type.
  245. static NestedNameSpecifier *createNestedNameSpecifierForScopeOf(
  246. const ASTContext &Ctx, const Decl *Decl,
  247. bool FullyQualified, bool WithGlobalNsPrefix) {
  248. assert(Decl);
  249. const DeclContext *DC = Decl->getDeclContext()->getRedeclContext();
  250. const auto *Outer = dyn_cast_or_null<NamedDecl>(DC);
  251. const auto *OuterNS = dyn_cast_or_null<NamespaceDecl>(DC);
  252. if (Outer && !(OuterNS && OuterNS->isAnonymousNamespace())) {
  253. if (const auto *CxxDecl = dyn_cast<CXXRecordDecl>(DC)) {
  254. if (ClassTemplateDecl *ClassTempl =
  255. CxxDecl->getDescribedClassTemplate()) {
  256. // We are in the case of a type(def) that was declared in a
  257. // class template but is *not* type dependent. In clang, it
  258. // gets attached to the class template declaration rather than
  259. // any specific class template instantiation. This result in
  260. // 'odd' fully qualified typename:
  261. //
  262. // vector<_Tp,_Alloc>::size_type
  263. //
  264. // Make the situation is 'useable' but looking a bit odd by
  265. // picking a random instance as the declaring context.
  266. if (ClassTempl->spec_begin() != ClassTempl->spec_end()) {
  267. Decl = *(ClassTempl->spec_begin());
  268. Outer = dyn_cast<NamedDecl>(Decl);
  269. OuterNS = dyn_cast<NamespaceDecl>(Decl);
  270. }
  271. }
  272. }
  273. if (OuterNS) {
  274. return createNestedNameSpecifier(Ctx, OuterNS, WithGlobalNsPrefix);
  275. } else if (const auto *TD = dyn_cast<TagDecl>(Outer)) {
  276. return createNestedNameSpecifier(
  277. Ctx, TD, FullyQualified, WithGlobalNsPrefix);
  278. } else if (dyn_cast<TranslationUnitDecl>(Outer)) {
  279. // Context is the TU. Nothing needs to be done.
  280. return nullptr;
  281. } else {
  282. // Decl's context was neither the TU, a namespace, nor a
  283. // TagDecl, which means it is a type local to a scope, and not
  284. // accessible at the end of the TU.
  285. return nullptr;
  286. }
  287. } else if (WithGlobalNsPrefix && DC->isTranslationUnit()) {
  288. return NestedNameSpecifier::GlobalSpecifier(Ctx);
  289. }
  290. return nullptr;
  291. }
  292. /// \brief Create a nested name specifier for the declaring context of
  293. /// the type.
  294. static NestedNameSpecifier *createNestedNameSpecifierForScopeOf(
  295. const ASTContext &Ctx, const Type *TypePtr,
  296. bool FullyQualified, bool WithGlobalNsPrefix) {
  297. if (!TypePtr) return nullptr;
  298. Decl *Decl = nullptr;
  299. // There are probably other cases ...
  300. if (const auto *TDT = dyn_cast<TypedefType>(TypePtr)) {
  301. Decl = TDT->getDecl();
  302. } else if (const auto *TagDeclType = dyn_cast<TagType>(TypePtr)) {
  303. Decl = TagDeclType->getDecl();
  304. } else if (const auto *TST = dyn_cast<TemplateSpecializationType>(TypePtr)) {
  305. Decl = TST->getTemplateName().getAsTemplateDecl();
  306. } else {
  307. Decl = TypePtr->getAsCXXRecordDecl();
  308. }
  309. if (!Decl) return nullptr;
  310. return createNestedNameSpecifierForScopeOf(
  311. Ctx, Decl, FullyQualified, WithGlobalNsPrefix);
  312. }
  313. NestedNameSpecifier *createNestedNameSpecifier(const ASTContext &Ctx,
  314. const NamespaceDecl *Namespace,
  315. bool WithGlobalNsPrefix) {
  316. while (Namespace && Namespace->isInline()) {
  317. // Ignore inline namespace;
  318. Namespace = dyn_cast<NamespaceDecl>(Namespace->getDeclContext());
  319. }
  320. if (!Namespace) return nullptr;
  321. bool FullyQualified = true; // doesn't matter, DeclContexts are namespaces
  322. return NestedNameSpecifier::Create(
  323. Ctx,
  324. createOuterNNS(Ctx, Namespace, FullyQualified, WithGlobalNsPrefix),
  325. Namespace);
  326. }
  327. NestedNameSpecifier *createNestedNameSpecifier(const ASTContext &Ctx,
  328. const TypeDecl *TD,
  329. bool FullyQualify,
  330. bool WithGlobalNsPrefix) {
  331. return NestedNameSpecifier::Create(
  332. Ctx,
  333. createOuterNNS(Ctx, TD, FullyQualify, WithGlobalNsPrefix),
  334. false /*No TemplateKeyword*/,
  335. TD->getTypeForDecl());
  336. }
  337. /// \brief Return the fully qualified type, including fully-qualified
  338. /// versions of any template parameters.
  339. QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
  340. bool WithGlobalNsPrefix) {
  341. // In case of myType* we need to strip the pointer first, fully
  342. // qualify and attach the pointer once again.
  343. if (isa<PointerType>(QT.getTypePtr())) {
  344. // Get the qualifiers.
  345. Qualifiers Quals = QT.getQualifiers();
  346. QT = getFullyQualifiedType(QT->getPointeeType(), Ctx, WithGlobalNsPrefix);
  347. QT = Ctx.getPointerType(QT);
  348. // Add back the qualifiers.
  349. QT = Ctx.getQualifiedType(QT, Quals);
  350. return QT;
  351. }
  352. // In case of myType& we need to strip the reference first, fully
  353. // qualify and attach the reference once again.
  354. if (isa<ReferenceType>(QT.getTypePtr())) {
  355. // Get the qualifiers.
  356. bool IsLValueRefTy = isa<LValueReferenceType>(QT.getTypePtr());
  357. Qualifiers Quals = QT.getQualifiers();
  358. QT = getFullyQualifiedType(QT->getPointeeType(), Ctx, WithGlobalNsPrefix);
  359. // Add the r- or l-value reference type back to the fully
  360. // qualified one.
  361. if (IsLValueRefTy)
  362. QT = Ctx.getLValueReferenceType(QT);
  363. else
  364. QT = Ctx.getRValueReferenceType(QT);
  365. // Add back the qualifiers.
  366. QT = Ctx.getQualifiedType(QT, Quals);
  367. return QT;
  368. }
  369. // Remove the part of the type related to the type being a template
  370. // parameter (we won't report it as part of the 'type name' and it
  371. // is actually make the code below to be more complex (to handle
  372. // those)
  373. while (isa<SubstTemplateTypeParmType>(QT.getTypePtr())) {
  374. // Get the qualifiers.
  375. Qualifiers Quals = QT.getQualifiers();
  376. QT = dyn_cast<SubstTemplateTypeParmType>(QT.getTypePtr())->desugar();
  377. // Add back the qualifiers.
  378. QT = Ctx.getQualifiedType(QT, Quals);
  379. }
  380. NestedNameSpecifier *Prefix = nullptr;
  381. // Local qualifiers are attached to the QualType outside of the
  382. // elaborated type. Retrieve them before descending into the
  383. // elaborated type.
  384. Qualifiers PrefixQualifiers = QT.getLocalQualifiers();
  385. QT = QualType(QT.getTypePtr(), 0);
  386. ElaboratedTypeKeyword Keyword = ETK_None;
  387. if (const auto *ETypeInput = dyn_cast<ElaboratedType>(QT.getTypePtr())) {
  388. QT = ETypeInput->getNamedType();
  389. assert(!QT.hasLocalQualifiers());
  390. Keyword = ETypeInput->getKeyword();
  391. }
  392. // Create a nested name specifier if needed.
  393. Prefix = createNestedNameSpecifierForScopeOf(Ctx, QT.getTypePtr(),
  394. true /*FullyQualified*/,
  395. WithGlobalNsPrefix);
  396. // In case of template specializations iterate over the arguments and
  397. // fully qualify them as well.
  398. if (isa<const TemplateSpecializationType>(QT.getTypePtr()) ||
  399. isa<const RecordType>(QT.getTypePtr())) {
  400. // We are asked to fully qualify and we have a Record Type (which
  401. // may point to a template specialization) or Template
  402. // Specialization Type. We need to fully qualify their arguments.
  403. const Type *TypePtr = getFullyQualifiedTemplateType(
  404. Ctx, QT.getTypePtr(), WithGlobalNsPrefix);
  405. QT = QualType(TypePtr, 0);
  406. }
  407. if (Prefix || Keyword != ETK_None) {
  408. QT = Ctx.getElaboratedType(Keyword, Prefix, QT);
  409. }
  410. QT = Ctx.getQualifiedType(QT, PrefixQualifiers);
  411. return QT;
  412. }
  413. std::string getFullyQualifiedName(QualType QT,
  414. const ASTContext &Ctx,
  415. bool WithGlobalNsPrefix) {
  416. PrintingPolicy Policy(Ctx.getPrintingPolicy());
  417. Policy.SuppressScope = false;
  418. Policy.AnonymousTagLocations = false;
  419. Policy.PolishForDeclaration = true;
  420. Policy.SuppressUnwrittenScope = true;
  421. QualType FQQT = getFullyQualifiedType(QT, Ctx, WithGlobalNsPrefix);
  422. return FQQT.getAsString(Policy);
  423. }
  424. } // end namespace TypeName
  425. } // end namespace clang