Index: clang/include/clang/AST/Decl.h =================================================================== --- clang/include/clang/AST/Decl.h +++ clang/include/clang/AST/Decl.h @@ -409,7 +409,7 @@ /// Determine whether this declaration can be redeclared in a /// different translation unit. bool isExternallyDeclarable() const { - return isExternallyVisible() && !getOwningModuleForLinkage(); + return isExternallyVisible() && !getModuleAttachment(); } /// Determines the visibility of this entity. Index: clang/include/clang/AST/DeclBase.h =================================================================== --- clang/include/clang/AST/DeclBase.h +++ clang/include/clang/AST/DeclBase.h @@ -782,12 +782,11 @@ return isFromASTFile() ? getImportedOwningModule() : getLocalOwningModule(); } - /// Get the module that owns this declaration for linkage purposes. - /// There only ever is such a module under the C++ Modules TS. + /// Get the module that this declaration is attached to. /// - /// \param IgnoreLinkage Ignore the linkage of the entity; assume that - /// all declarations in a global module fragment are unowned. - Module *getOwningModuleForLinkage(bool IgnoreLinkage = false) const; + /// \param IgnoreLinkage Ignore ModuleInternalLinkage of the entity; assume + /// that all declarations in a global module fragment attached to the GM. + Module *getModuleAttachment(bool IgnoreLinkage = false) const; /// Determine whether this declaration is definitely visible to name lookup, /// independent of whether the owning module is visible. Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -4075,8 +4075,7 @@ // anything that is not visible. We don't need to check linkage here; if // the context has internal linkage, redeclaration lookup won't find things // from other TUs, and we can't safely compute linkage yet in general. - if (cast(CurContext) - ->getOwningModuleForLinkage(/*IgnoreLinkage*/true)) + if (cast(CurContext)->getModuleAttachment(/*IgnoreLinkage*/true)) return ForVisibleRedeclaration; return ForExternalRedeclaration; } Index: clang/lib/AST/Decl.cpp =================================================================== --- clang/lib/AST/Decl.cpp +++ clang/lib/AST/Decl.cpp @@ -1539,10 +1539,9 @@ : CK); } -Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const { +Module *Decl::getModuleAttachment(bool IgnoreLinkage) const { if (isa(this)) - // Namespaces never have module linkage. It is the entities within them - // that [may] do. + // External namespaces are always attached to the global module. return nullptr; Module *M = getOwningModule(); Index: clang/lib/AST/ItaniumMangle.cpp =================================================================== --- clang/lib/AST/ItaniumMangle.cpp +++ clang/lib/AST/ItaniumMangle.cpp @@ -714,7 +714,7 @@ if (DC->isTranslationUnit() && D->getFormalLinkage() != InternalLinkage && !CXXNameMangler::shouldHaveAbiTags(*this, VD) && !isa(VD) && - !VD->getOwningModuleForLinkage()) + !VD->getModuleAttachment()) return false; } @@ -1003,7 +1003,7 @@ void CXXNameMangler::mangleModuleName(const NamedDecl *ND) { if (ND->isExternallyVisible()) - if (Module *M = ND->getOwningModuleForLinkage()) + if (Module *M = ND->getModuleAttachment()) mangleModuleNamePrefix(M->Name); } @@ -5991,7 +5991,7 @@ if (TemplateArgs[0].getAsType() != A) return false; - if (SD->getSpecializedTemplate()->getOwningModuleForLinkage()) + if (SD->getSpecializedTemplate()->getModuleAttachment()) return false; return true; @@ -6024,7 +6024,7 @@ !isSpecializedAs(TemplateArgs[2].getAsType(), "allocator", A)) return false; - if (SD->getSpecializedTemplate()->getOwningModuleForLinkage()) + if (SD->getSpecializedTemplate()->getModuleAttachment()) return false; return true; @@ -6044,7 +6044,7 @@ if (!isStdNamespace(getEffectiveDeclContext(TD))) return false; - if (TD->getOwningModuleForLinkage()) + if (TD->getModuleAttachment()) return false; // ::= Sa # ::std::allocator @@ -6066,7 +6066,7 @@ if (!isStdNamespace(getEffectiveDeclContext(SD))) return false; - if (SD->getSpecializedTemplate()->getOwningModuleForLinkage()) + if (SD->getSpecializedTemplate()->getModuleAttachment()) return false; // ::= Ss # ::std::basic_stringhasExternalFormalLinkage() && D->getOwningModuleForLinkage()) + // If the declaration is attached to a named module, we must mangle its name. + if (!D->hasExternalFormalLinkage() && D->getModuleAttachment()) return true; // C functions with internal linkage have to be mangled with option Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -1594,7 +1594,7 @@ // a nested-name-specifier nor a simple-template-id, it is attached to the // module to which the friend is attached ([basic.link]). if (New->getFriendObjectKind() && - Old->getOwningModuleForLinkage() != New->getOwningModuleForLinkage()) { + Old->getModuleAttachment() != New->getModuleAttachment()) { New->setLocalOwningModule(Old->getOwningModule()); makeMergedDefinitionVisible(New); return false;