QualTypeNames.cpp 18 KB

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