| 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 | |
| 9 | #include "clang/AST/QualTypeNames.h" |
| 10 | #include "clang/AST/DeclTemplate.h" |
| 11 | #include "clang/AST/DeclarationName.h" |
| 12 | #include "clang/AST/Mangle.h" |
| 13 | #include "clang/AST/Type.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | |
| 17 | namespace TypeName { |
| 18 | |
| 19 | /// Create a NestedNameSpecifier for Namesp and its enclosing |
| 20 | /// scopes. |
| 21 | /// |
| 22 | /// \param[in] Ctx - the AST Context to be used. |
| 23 | /// \param[in] Namesp - the NamespaceDecl for which a NestedNameSpecifier |
| 24 | /// is requested. |
| 25 | /// \param[in] WithGlobalNsPrefix - Indicate whether the global namespace |
| 26 | /// specifier "::" should be prepended or not. |
| 27 | static NestedNameSpecifier *createNestedNameSpecifier( |
| 28 | const ASTContext &Ctx, |
| 29 | const NamespaceDecl *Namesp, |
| 30 | bool WithGlobalNsPrefix); |
| 31 | |
| 32 | /// 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 | |
| 46 | static NestedNameSpecifier *createNestedNameSpecifierForScopeOf( |
| 47 | const ASTContext &Ctx, const Decl *decl, |
| 48 | bool FullyQualified, bool WithGlobalNsPrefix); |
| 49 | |
| 50 | static NestedNameSpecifier *getFullyQualifiedNestedNameSpecifier( |
| 51 | const ASTContext &Ctx, NestedNameSpecifier *scope, bool WithGlobalNsPrefix); |
| 52 | |
| 53 | static bool getFullyQualifiedTemplateName(const ASTContext &Ctx, |
| 54 | TemplateName &TName, |
| 55 | bool WithGlobalNsPrefix) { |
| 56 | bool Changed = false; |
| 57 | NestedNameSpecifier *NNS = nullptr; |
| 58 | |
| 59 | TemplateDecl *ArgTDecl = TName.getAsTemplateDecl(); |
| 60 | // ArgTDecl won't be NULL because we asserted that this isn't a |
| 61 | // dependent context very early in the call chain. |
| 62 | assert(ArgTDecl != nullptr); |
| 63 | QualifiedTemplateName *QTName = TName.getAsQualifiedTemplateName(); |
| 64 | |
| 65 | if (QTName && |
| 66 | !QTName->hasTemplateKeyword() && |
| 67 | (NNS = QTName->getQualifier())) { |
| 68 | NestedNameSpecifier *QNNS = getFullyQualifiedNestedNameSpecifier( |
| 69 | Ctx, NNS, WithGlobalNsPrefix); |
| 70 | if (QNNS != NNS) { |
| 71 | Changed = true; |
| 72 | NNS = QNNS; |
| 73 | } else { |
| 74 | NNS = nullptr; |
| 75 | } |
| 76 | } else { |
| 77 | NNS = createNestedNameSpecifierForScopeOf( |
| 78 | Ctx, ArgTDecl, true, WithGlobalNsPrefix); |
| 79 | } |
| 80 | if (NNS) { |
| 81 | TemplateName UnderlyingTN(ArgTDecl); |
| 82 | if (UsingShadowDecl *USD = TName.getAsUsingShadowDecl()) |
| 83 | UnderlyingTN = TemplateName(USD); |
| 84 | TName = |
| 85 | Ctx.getQualifiedTemplateName(NNS, |
| 86 | /*TemplateKeyword=*/false, UnderlyingTN); |
| 87 | Changed = true; |
| 88 | } |
| 89 | return Changed; |
| 90 | } |
| 91 | |
| 92 | // AspectC++: made function visible by commenting out "static" |
| 93 | // static |
| 94 | // AspectC++ end |
| 95 | bool getFullyQualifiedTemplateArgument(const ASTContext &Ctx, |
| 96 | TemplateArgument &Arg, |
| 97 | bool WithGlobalNsPrefix) { |
| 98 | bool Changed = false; |
| 99 | |
| 100 | // Note: we do not handle TemplateArgument::Expression, to replace it |
| 101 | // we need the information for the template instance decl. |
| 102 | |
| 103 | if (Arg.getKind() == TemplateArgument::Template) { |
| 104 | TemplateName TName = Arg.getAsTemplate(); |
| 105 | Changed = getFullyQualifiedTemplateName(Ctx, TName, WithGlobalNsPrefix); |
| 106 | if (Changed) { |
| 107 | Arg = TemplateArgument(TName); |
| 108 | } |
| 109 | } else if (Arg.getKind() == TemplateArgument::Type) { |
| 110 | QualType SubTy = Arg.getAsType(); |
| 111 | // Check if the type needs more desugaring and recurse. |
| 112 | QualType QTFQ = getFullyQualifiedType(SubTy, Ctx, WithGlobalNsPrefix); |
| 113 | if (QTFQ != SubTy) { |
| 114 | Arg = TemplateArgument(QTFQ); |
| 115 | Changed = true; |
| 116 | } |
| 117 | } |
| 118 | return Changed; |
| 119 | } |
| 120 | |
| 121 | static const Type *getFullyQualifiedTemplateType(const ASTContext &Ctx, |
| 122 | const Type *TypePtr, |
| 123 | bool WithGlobalNsPrefix) { |
| 124 | // DependentTemplateTypes exist within template declarations and |
| 125 | // definitions. Therefore we shouldn't encounter them at the end of |
| 126 | // a translation unit. If we do, the caller has made an error. |
| 127 | assert(!isa<DependentTemplateSpecializationType>(TypePtr)); |
| 128 | |
| 129 | // AspectC++: getIsDefaulted can only be used with the underlying RecordType. |
| 130 | // => "::std::basic_ostream<char>" instead of "::std::basic_ostream<char, ::std::type_traits<char>>" |
| 131 | if (const auto *TST = dyn_cast<const TemplateSpecializationType>(TypePtr)) { |
| 132 | TypePtr = TST->desugar().getTypePtr(); |
| 133 | } |
| 134 | // AspectC++ end |
| 135 | |
| 136 | // In case of template specializations, iterate over the arguments |
| 137 | // and fully qualify them as well. |
| 138 | if (const auto *TST = dyn_cast<const TemplateSpecializationType>(TypePtr)) { |
| 139 | bool MightHaveChanged = false; |
| 140 | SmallVector<TemplateArgument, 4> FQArgs; |
| 141 | // Cheap to copy and potentially modified by |
| 142 | // getFullyQualifedTemplateArgument. |
| 143 | for (TemplateArgument Arg : TST->template_arguments()) { |
| 144 | MightHaveChanged |= getFullyQualifiedTemplateArgument( |
| 145 | Ctx, Arg, WithGlobalNsPrefix); |
| 146 | FQArgs.push_back(Arg); |
| 147 | } |
| 148 | |
| 149 | // If a fully qualified arg is different from the unqualified arg, |
| 150 | // allocate new type in the AST. |
| 151 | if (MightHaveChanged) { |
| 152 | QualType QT = Ctx.getTemplateSpecializationType( |
| 153 | TST->getTemplateName(), FQArgs, |
| 154 | /*CanonicalArgs=*/{}, TST->desugar()); |
| 155 | // getTemplateSpecializationType returns a fully qualified |
| 156 | // version of the specialization itself, so no need to qualify |
| 157 | // it. |
| 158 | return QT.getTypePtr(); |
| 159 | } |
| 160 | } else if (const auto *TSTRecord = dyn_cast<const RecordType>(TypePtr)) { |
| 161 | // We are asked to fully qualify and we have a Record Type, |
| 162 | // which can point to a template instantiation with no sugar in any of |
| 163 | // its template argument, however we still need to fully qualify them. |
| 164 | |
| 165 | if (const auto *TSTDecl = |
| 166 | dyn_cast<ClassTemplateSpecializationDecl>(TSTRecord->getDecl())) { |
| 167 | const TemplateArgumentList &TemplateArgs = TSTDecl->getTemplateArgs(); |
| 168 | |
| 169 | bool MightHaveChanged = false; |
| 170 | SmallVector<TemplateArgument, 4> FQArgs; |
| 171 | for (unsigned int I = 0, E = TemplateArgs.size(); I != E; ++I) { |
| 172 | // cheap to copy and potentially modified by |
| 173 | // getFullyQualifedTemplateArgument |
| 174 | TemplateArgument Arg(TemplateArgs[I]); |
| 175 | MightHaveChanged |= getFullyQualifiedTemplateArgument( |
| 176 | Ctx, Arg, WithGlobalNsPrefix); |
| 177 | FQArgs.push_back(Arg); |
| 178 | } |
| 179 | |
| 180 | // AspectC++: Remove template arguments from the end of the argument list |
| 181 | // if they are identical to the default argument |
| 182 | unsigned int defaulted = 0; |
| 183 | for (int I = TemplateArgs.size() - 1; I >= 0; I--) { |
| 184 | if (!TemplateArgs[I].getIsDefaulted()) |
| 185 | break; |
| 186 | defaulted++; |
| 187 | } |
| 188 | FQArgs.pop_back_n(defaulted); |
| 189 | // AspectC++ end |
| 190 | |
| 191 | // If a fully qualified arg is different from the unqualified arg, |
| 192 | // allocate new type in the AST. |
| 193 | if (MightHaveChanged) { |
| 194 | TemplateName TN(TSTDecl->getSpecializedTemplate()); |
| 195 | QualType QT = Ctx.getTemplateSpecializationType( |
| 196 | TN, FQArgs, |
| 197 | /*CanonicalArgs=*/{}, TSTRecord->getCanonicalTypeInternal()); |
| 198 | // getTemplateSpecializationType returns a fully qualified |
| 199 | // version of the specialization itself, so no need to qualify |
| 200 | // it. |
| 201 | return QT.getTypePtr(); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | return TypePtr; |
| 206 | } |
| 207 | |
| 208 | static NestedNameSpecifier *createOuterNNS(const ASTContext &Ctx, const Decl *D, |
| 209 | bool FullyQualify, |
| 210 | bool WithGlobalNsPrefix) { |
| 211 | const DeclContext *DC = D->getDeclContext(); |
| 212 | if (const auto *NS = dyn_cast<NamespaceDecl>(DC)) { |
| 213 | while (NS && NS->isInline()) { |
| 214 | // Ignore inline namespace; |
| 215 | NS = dyn_cast<NamespaceDecl>(NS->getDeclContext()); |
| 216 | } |
| 217 | if (NS && NS->getDeclName()) { |
| 218 | return createNestedNameSpecifier(Ctx, NS, WithGlobalNsPrefix); |
| 219 | } |
| 220 | return nullptr; // no starting '::', no anonymous |
| 221 | } else if (const auto *TD = dyn_cast<TagDecl>(DC)) { |
| 222 | return createNestedNameSpecifier(Ctx, TD, FullyQualify, WithGlobalNsPrefix); |
| 223 | } else if (const auto *TDD = dyn_cast<TypedefNameDecl>(DC)) { |
| 224 | return createNestedNameSpecifier( |
| 225 | Ctx, TDD, FullyQualify, WithGlobalNsPrefix); |
| 226 | } else if (WithGlobalNsPrefix && DC->isTranslationUnit()) { |
| 227 | return NestedNameSpecifier::GlobalSpecifier(Ctx); |
| 228 | } |
| 229 | return nullptr; // no starting '::' if |WithGlobalNsPrefix| is false |
| 230 | } |
| 231 | |
| 232 | /// Return a fully qualified version of this name specifier. |
| 233 | static NestedNameSpecifier *getFullyQualifiedNestedNameSpecifier( |
| 234 | const ASTContext &Ctx, NestedNameSpecifier *Scope, |
| 235 | bool WithGlobalNsPrefix) { |
| 236 | switch (Scope->getKind()) { |
| 237 | case NestedNameSpecifier::Global: |
| 238 | case NestedNameSpecifier::Super: |
| 239 | // Already fully qualified |
| 240 | return Scope; |
| 241 | case NestedNameSpecifier::Namespace: |
| 242 | return TypeName::createNestedNameSpecifier( |
| 243 | Ctx, Scope->getAsNamespace(), WithGlobalNsPrefix); |
| 244 | case NestedNameSpecifier::NamespaceAlias: |
| 245 | // Namespace aliases are only valid for the duration of the |
| 246 | // scope where they were introduced, and therefore are often |
| 247 | // invalid at the end of the TU. So use the namespace name more |
| 248 | // likely to be valid at the end of the TU. |
| 249 | return TypeName::createNestedNameSpecifier( |
| 250 | Ctx, |
| 251 | Scope->getAsNamespaceAlias()->getNamespace()->getCanonicalDecl(), |
| 252 | WithGlobalNsPrefix); |
| 253 | case NestedNameSpecifier::Identifier: |
| 254 | // A function or some other construct that makes it un-namable |
| 255 | // at the end of the TU. Skip the current component of the name, |
| 256 | // but use the name of it's prefix. |
| 257 | return getFullyQualifiedNestedNameSpecifier( |
| 258 | Ctx, Scope->getPrefix(), WithGlobalNsPrefix); |
| 259 | case NestedNameSpecifier::TypeSpec: { |
| 260 | const Type *Type = Scope->getAsType(); |
| 261 | // Find decl context. |
| 262 | const TagDecl *TD = nullptr; |
| 263 | if (const TagType *TagDeclType = Type->getAs<TagType>()) { |
| 264 | TD = TagDeclType->getDecl(); |
| 265 | } else { |
| 266 | TD = Type->getAsCXXRecordDecl(); |
| 267 | } |
| 268 | if (TD) { |
| 269 | return TypeName::createNestedNameSpecifier(Ctx, TD, |
| 270 | true /*FullyQualified*/, |
| 271 | WithGlobalNsPrefix); |
| 272 | } else if (const auto *TDD = dyn_cast<TypedefType>(Type)) { |
| 273 | return TypeName::createNestedNameSpecifier(Ctx, TDD->getDecl(), |
| 274 | true /*FullyQualified*/, |
| 275 | WithGlobalNsPrefix); |
| 276 | } |
| 277 | return Scope; |
| 278 | } |
| 279 | } |
| 280 | llvm_unreachable("bad NNS kind"); |
| 281 | } |
| 282 | |
| 283 | /// Create a nested name specifier for the declaring context of |
| 284 | /// the type. |
| 285 | static NestedNameSpecifier *createNestedNameSpecifierForScopeOf( |
| 286 | const ASTContext &Ctx, const Decl *Decl, |
| 287 | bool FullyQualified, bool WithGlobalNsPrefix) { |
| 288 | assert(Decl); |
| 289 | |
| 290 | const DeclContext *DC = Decl->getDeclContext()->getRedeclContext(); |
| 291 | const auto *Outer = dyn_cast<NamedDecl>(DC); |
| 292 | const auto *OuterNS = dyn_cast<NamespaceDecl>(DC); |
| 293 | if (Outer && !(OuterNS && OuterNS->isAnonymousNamespace())) { |
| 294 | if (const auto *CxxDecl = dyn_cast<CXXRecordDecl>(DC)) { |
| 295 | if (ClassTemplateDecl *ClassTempl = |
| 296 | CxxDecl->getDescribedClassTemplate()) { |
| 297 | // We are in the case of a type(def) that was declared in a |
| 298 | // class template but is *not* type dependent. In clang, it |
| 299 | // gets attached to the class template declaration rather than |
| 300 | // any specific class template instantiation. This result in |
| 301 | // 'odd' fully qualified typename: |
| 302 | // |
| 303 | // vector<_Tp,_Alloc>::size_type |
| 304 | // |
| 305 | // Make the situation is 'useable' but looking a bit odd by |
| 306 | // picking a random instance as the declaring context. |
| 307 | if (ClassTempl->spec_begin() != ClassTempl->spec_end()) { |
| 308 | Decl = *(ClassTempl->spec_begin()); |
| 309 | Outer = dyn_cast<NamedDecl>(Decl); |
| 310 | OuterNS = dyn_cast<NamespaceDecl>(Decl); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (OuterNS) { |
| 316 | return createNestedNameSpecifier(Ctx, OuterNS, WithGlobalNsPrefix); |
| 317 | } else if (const auto *TD = dyn_cast<TagDecl>(Outer)) { |
| 318 | return createNestedNameSpecifier( |
| 319 | Ctx, TD, FullyQualified, WithGlobalNsPrefix); |
| 320 | } else if (isa<TranslationUnitDecl>(Outer)) { |
| 321 | // Context is the TU. Nothing needs to be done. |
| 322 | return nullptr; |
| 323 | } else { |
| 324 | // Decl's context was neither the TU, a namespace, nor a |
| 325 | // TagDecl, which means it is a type local to a scope, and not |
| 326 | // accessible at the end of the TU. |
| 327 | return nullptr; |
| 328 | } |
| 329 | } else if (WithGlobalNsPrefix && DC->isTranslationUnit()) { |
| 330 | return NestedNameSpecifier::GlobalSpecifier(Ctx); |
| 331 | } |
| 332 | return nullptr; |
| 333 | } |
| 334 | |
| 335 | /// Create a nested name specifier for the declaring context of |
| 336 | /// the type. |
| 337 | static NestedNameSpecifier *createNestedNameSpecifierForScopeOf( |
| 338 | const ASTContext &Ctx, const Type *TypePtr, |
| 339 | bool FullyQualified, bool WithGlobalNsPrefix) { |
| 340 | if (!TypePtr) return nullptr; |
| 341 | |
| 342 | Decl *Decl = nullptr; |
| 343 | // There are probably other cases ... |
| 344 | if (const auto *TDT = dyn_cast<TypedefType>(TypePtr)) { |
| 345 | Decl = TDT->getDecl(); |
| 346 | } else if (const auto *TagDeclType = dyn_cast<TagType>(TypePtr)) { |
| 347 | Decl = TagDeclType->getDecl(); |
| 348 | } else if (const auto *TST = dyn_cast<TemplateSpecializationType>(TypePtr)) { |
| 349 | Decl = TST->getTemplateName().getAsTemplateDecl(); |
| 350 | } else { |
| 351 | Decl = TypePtr->getAsCXXRecordDecl(); |
| 352 | } |
| 353 | |
| 354 | if (!Decl) return nullptr; |
| 355 | |
| 356 | return createNestedNameSpecifierForScopeOf( |
| 357 | Ctx, Decl, FullyQualified, WithGlobalNsPrefix); |
| 358 | } |
| 359 | |
| 360 | NestedNameSpecifier *createNestedNameSpecifier(const ASTContext &Ctx, |
| 361 | const NamespaceDecl *Namespace, |
| 362 | bool WithGlobalNsPrefix) { |
| 363 | while (Namespace && Namespace->isInline()) { |
| 364 | // Ignore inline namespace; |
| 365 | Namespace = dyn_cast<NamespaceDecl>(Namespace->getDeclContext()); |
| 366 | } |
| 367 | if (!Namespace) return nullptr; |
| 368 | |
| 369 | bool FullyQualified = true; // doesn't matter, DeclContexts are namespaces |
| 370 | return NestedNameSpecifier::Create( |
| 371 | Ctx, |
| 372 | createOuterNNS(Ctx, Namespace, FullyQualified, WithGlobalNsPrefix), |
| 373 | Namespace); |
| 374 | } |
| 375 | |
| 376 | NestedNameSpecifier *createNestedNameSpecifier(const ASTContext &Ctx, |
| 377 | const TypeDecl *TD, |
| 378 | bool FullyQualify, |
| 379 | bool WithGlobalNsPrefix) { |
| 380 | const Type *TypePtr = TD->getTypeForDecl(); |
| 381 | if (isa<const TemplateSpecializationType>(TypePtr) || |
| 382 | isa<const RecordType>(TypePtr)) { |
| 383 | // We are asked to fully qualify and we have a Record Type (which |
| 384 | // may point to a template specialization) or Template |
| 385 | // Specialization Type. We need to fully qualify their arguments. |
| 386 | |
| 387 | TypePtr = getFullyQualifiedTemplateType(Ctx, TypePtr, WithGlobalNsPrefix); |
| 388 | } |
| 389 | |
| 390 | return NestedNameSpecifier::Create( |
| 391 | Ctx, createOuterNNS(Ctx, TD, FullyQualify, WithGlobalNsPrefix), TypePtr); |
| 392 | } |
| 393 | |
| 394 | /// Return the fully qualified type, including fully-qualified |
| 395 | /// versions of any template parameters. |
| 396 | QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx, |
| 397 | bool WithGlobalNsPrefix) { |
| 398 | |
| 399 | // AspectC++: More types must be handled recursively! |
| 400 | // => now we also recurse into ParenTypes and FunctionProtoTypes |
| 401 | // (arguments and return type) |
| 402 | if (auto *PT = dyn_cast<ParenType>(QT.getTypePtr())) { |
| 403 | Qualifiers Quals = QT.getQualifiers(); |
| 404 | QT = getFullyQualifiedType(PT->getInnerType(), Ctx, WithGlobalNsPrefix); |
| 405 | QT = Ctx.getParenType(QT); |
| 406 | // Add back the qualifiers. |
| 407 | QT = Ctx.getQualifiedType(QT, Quals); |
| 408 | return QT; |
| 409 | } |
| 410 | |
| 411 | if (auto *FPT = dyn_cast<FunctionProtoType>(QT.getTypePtr())) { |
| 412 | Qualifiers Quals = QT.getQualifiers(); |
| 413 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 414 | SmallVector<QualType> new_param_types; |
| 415 | for (const auto PT : FPT->param_types()) { |
| 416 | QualType QPT = getFullyQualifiedType(PT, Ctx, WithGlobalNsPrefix); |
| 417 | new_param_types.push_back(QPT); |
| 418 | } |
| 419 | QualType QRT = getFullyQualifiedType(FPT->getReturnType(), Ctx, WithGlobalNsPrefix); |
| 420 | QT = Ctx.getFunctionType(QRT, new_param_types, EPI); |
| 421 | // Add back the qualifiers. |
| 422 | QT = Ctx.getQualifiedType(QT, Quals); |
| 423 | return QT; |
| 424 | } |
| 425 | // AspectC++ end |
| 426 | |
| 427 | // In case of myType* we need to strip the pointer first, fully |
| 428 | // qualify and attach the pointer once again. |
| 429 | if (isa<PointerType>(QT.getTypePtr())) { |
| 430 | // Get the qualifiers. |
| 431 | Qualifiers Quals = QT.getQualifiers(); |
| 432 | QT = getFullyQualifiedType(QT->getPointeeType(), Ctx, WithGlobalNsPrefix); |
| 433 | QT = Ctx.getPointerType(QT); |
| 434 | // Add back the qualifiers. |
| 435 | QT = Ctx.getQualifiedType(QT, Quals); |
| 436 | return QT; |
| 437 | } |
| 438 | |
| 439 | if (auto *MPT = dyn_cast<MemberPointerType>(QT.getTypePtr())) { |
| 440 | // Get the qualifiers. |
| 441 | Qualifiers Quals = QT.getQualifiers(); |
| 442 | // Fully qualify the pointee and class types. |
| 443 | QT = getFullyQualifiedType(QT->getPointeeType(), Ctx, WithGlobalNsPrefix); |
| 444 | NestedNameSpecifier *Qualifier = getFullyQualifiedNestedNameSpecifier( |
| 445 | Ctx, MPT->getQualifier(), WithGlobalNsPrefix); |
| 446 | QT = Ctx.getMemberPointerType(QT, Qualifier, |
| 447 | MPT->getMostRecentCXXRecordDecl()); |
| 448 | // Add back the qualifiers. |
| 449 | QT = Ctx.getQualifiedType(QT, Quals); |
| 450 | return QT; |
| 451 | } |
| 452 | |
| 453 | // In case of myType& we need to strip the reference first, fully |
| 454 | // qualify and attach the reference once again. |
| 455 | if (isa<ReferenceType>(QT.getTypePtr())) { |
| 456 | // Get the qualifiers. |
| 457 | bool IsLValueRefTy = isa<LValueReferenceType>(QT.getTypePtr()); |
| 458 | Qualifiers Quals = QT.getQualifiers(); |
| 459 | QT = getFullyQualifiedType(QT->getPointeeType(), Ctx, WithGlobalNsPrefix); |
| 460 | // Add the r- or l-value reference type back to the fully |
| 461 | // qualified one. |
| 462 | if (IsLValueRefTy) |
| 463 | QT = Ctx.getLValueReferenceType(QT); |
| 464 | else |
| 465 | QT = Ctx.getRValueReferenceType(QT); |
| 466 | // Add back the qualifiers. |
| 467 | QT = Ctx.getQualifiedType(QT, Quals); |
| 468 | return QT; |
| 469 | } |
| 470 | |
| 471 | // Handle types with attributes such as `unique_ptr<int> _Nonnull`. |
| 472 | if (auto *AT = dyn_cast<AttributedType>(QT.getTypePtr())) { |
| 473 | QualType NewModified = |
| 474 | getFullyQualifiedType(AT->getModifiedType(), Ctx, WithGlobalNsPrefix); |
| 475 | QualType NewEquivalent = |
| 476 | getFullyQualifiedType(AT->getEquivalentType(), Ctx, WithGlobalNsPrefix); |
| 477 | Qualifiers Qualifiers = QT.getLocalQualifiers(); |
| 478 | return Ctx.getQualifiedType( |
| 479 | Ctx.getAttributedType(AT->getAttrKind(), NewModified, NewEquivalent), |
| 480 | Qualifiers); |
| 481 | } |
| 482 | |
| 483 | // Remove the part of the type related to the type being a template |
| 484 | // parameter (we won't report it as part of the 'type name' and it |
| 485 | // is actually make the code below to be more complex (to handle |
| 486 | // those) |
| 487 | while (isa<SubstTemplateTypeParmType>(QT.getTypePtr())) { |
| 488 | // Get the qualifiers. |
| 489 | Qualifiers Quals = QT.getQualifiers(); |
| 490 | |
| 491 | QT = cast<SubstTemplateTypeParmType>(QT.getTypePtr())->desugar(); |
| 492 | |
| 493 | // Add back the qualifiers. |
| 494 | QT = Ctx.getQualifiedType(QT, Quals); |
| 495 | } |
| 496 | |
| 497 | NestedNameSpecifier *Prefix = nullptr; |
| 498 | // Local qualifiers are attached to the QualType outside of the |
| 499 | // elaborated type. Retrieve them before descending into the |
| 500 | // elaborated type. |
| 501 | Qualifiers PrefixQualifiers = QT.getLocalQualifiers(); |
| 502 | QT = QualType(QT.getTypePtr(), 0); |
| 503 | ElaboratedTypeKeyword Keyword = ElaboratedTypeKeyword::None; |
| 504 | if (const auto *ETypeInput = dyn_cast<ElaboratedType>(QT.getTypePtr())) { |
| 505 | QT = ETypeInput->getNamedType(); |
| 506 | assert(!QT.hasLocalQualifiers()); |
| 507 | Keyword = ETypeInput->getKeyword(); |
| 508 | } |
| 509 | |
| 510 | // We don't consider the alias introduced by `using a::X` as a new type. |
| 511 | // The qualified name is still a::X. |
| 512 | if (const auto *UT = QT->getAs<UsingType>()) { |
| 513 | QT = Ctx.getQualifiedType(UT->getUnderlyingType(), PrefixQualifiers); |
| 514 | return getFullyQualifiedType(QT, Ctx, WithGlobalNsPrefix); |
| 515 | } |
| 516 | |
| 517 | // Create a nested name specifier if needed. |
| 518 | Prefix = createNestedNameSpecifierForScopeOf(Ctx, QT.getTypePtr(), |
| 519 | true /*FullyQualified*/, |
| 520 | WithGlobalNsPrefix); |
| 521 | |
| 522 | // In case of template specializations iterate over the arguments and |
| 523 | // fully qualify them as well. |
| 524 | if (isa<const TemplateSpecializationType>(QT.getTypePtr()) || |
| 525 | isa<const RecordType>(QT.getTypePtr())) { |
| 526 | // We are asked to fully qualify and we have a Record Type (which |
| 527 | // may point to a template specialization) or Template |
| 528 | // Specialization Type. We need to fully qualify their arguments. |
| 529 | |
| 530 | const Type *TypePtr = getFullyQualifiedTemplateType( |
| 531 | Ctx, QT.getTypePtr(), WithGlobalNsPrefix); |
| 532 | QT = QualType(TypePtr, 0); |
| 533 | } |
| 534 | if (Prefix || Keyword != ElaboratedTypeKeyword::None) { |
| 535 | QT = Ctx.getElaboratedType(Keyword, Prefix, QT); |
| 536 | } |
| 537 | QT = Ctx.getQualifiedType(QT, PrefixQualifiers); |
| 538 | return QT; |
| 539 | } |
| 540 | |
| 541 | std::string getFullyQualifiedName(QualType QT, |
| 542 | const ASTContext &Ctx, |
| 543 | const PrintingPolicy &Policy, |
| 544 | bool WithGlobalNsPrefix) { |
| 545 | QualType FQQT = getFullyQualifiedType(QT, Ctx, WithGlobalNsPrefix); |
| 546 | return FQQT.getAsString(Policy); |
| 547 | } |
| 548 | |
| 549 | } // end namespace TypeName |
| 550 | } // end namespace clang |