virtual void Destroy(ASTContext& C);
+ // \brief Returns true if this is an anonymous namespace declaration.
+ //
+ // For example:
+ // namespace {
+ // ...
+ // };
+ // q.v. C++ [namespace.unnamed]
+ bool isAnonymousNamespace() const {
+ return !getIdentifier();
+ }
+
NamespaceDecl *getNextNamespace() { return NextNamespace; }
const NamespaceDecl *getNextNamespace() const { return NextNamespace; }
void setNextNamespace(NamespaceDecl *ND) { NextNamespace = ND; }
return const_cast<Decl*>(this)->getTranslationUnitDecl();
}
+ bool isInAnonymousNamespace() const;
+
ASTContext &getASTContext() const;
void setAccess(AccessSpecifier AS) {
}
}
+bool Decl::isInAnonymousNamespace() const {
+ const DeclContext *DC = getDeclContext();
+ do {
+ if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
+ if (ND->isAnonymousNamespace())
+ return true;
+ } while ((DC = DC->getParent()));
+
+ return false;
+}
+
TranslationUnitDecl *Decl::getTranslationUnitDecl() {
if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
return TUD;
static CodeGenModule::GVALinkage
GetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD,
const LangOptions &Features) {
+ // Everything located semantically within an anonymous namespace is
+ // always internal.
+ if (FD->isInAnonymousNamespace())
+ return CodeGenModule::GVA_Internal;
+
// The kind of external linkage this function will have, if it is not
// inline or static.
CodeGenModule::GVALinkage External = CodeGenModule::GVA_StrongExternal;
GV->setAlignment(getContext().getDeclAlignInBytes(D));
// Set the llvm linkage type as appropriate.
- if (D->getStorageClass() == VarDecl::Static)
+ if (D->isInAnonymousNamespace())
+ GV->setLinkage(llvm::Function::InternalLinkage);
+ else if (D->getStorageClass() == VarDecl::Static)
GV->setLinkage(llvm::Function::InternalLinkage);
else if (D->hasAttr<DLLImportAttr>())
GV->setLinkage(llvm::Function::DLLImportLinkage);
return false;
const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
- return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
+ const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
+ return II && II->isStr("std");
}
static const TemplateDecl *
DeclarationName Name = ND->getDeclName();
switch (Name.getNameKind()) {
case DeclarationName::Identifier:
+ if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND))
+ if (NS->isAnonymousNamespace()) {
+ // This is how gcc mangles these names. It's apparently
+ // always '1', no matter how many different anonymous
+ // namespaces appear in a context.
+ Out << "12_GLOBAL__N_1";
+ break;
+ }
mangleSourceName(Name.getAsIdentifierInfo());
break;
bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
// <substitution> ::= St # ::std::
if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
- if (NS->getParent()->isTranslationUnit() &&
- NS->getOriginalNamespace()->getIdentifier()->isStr("std")) {
+ if (isStdNamespace(NS)) {
Out << "St";
return true;
}
PushOnScopeChains(Namespc, DeclRegionScope);
} else {
- // FIXME: Handle anonymous namespaces
+ // Anonymous namespaces.
+
+ // C++ [namespace.unnamed]p1. An unnamed-namespace-definition
+ // behaves as if it were replaced by
+ // namespace unique { /* empty body */ }
+ // using namespace unique;
+ // namespace unique { namespace-body }
+ // where all occurrences of 'unique' in a translation unit are
+ // replaced by the same identifier and this identifier differs
+ // from all other identifiers in the entire program.
+
+ // We just create the namespace with an empty name and then add an
+ // implicit using declaration, just like the standard suggests.
+ //
+ // CodeGen enforces the "universally unique" aspect by giving all
+ // declarations semantically contained within an anonymous
+ // namespace internal linkage.
+
+ assert(Namespc->isAnonymousNamespace());
+ CurContext->addDecl(Namespc);
+
+ UsingDirectiveDecl* UD
+ = UsingDirectiveDecl::Create(Context, CurContext,
+ /* 'using' */ LBrace,
+ /* 'namespace' */ SourceLocation(),
+ /* qualifier */ SourceRange(),
+ /* NNS */ NULL,
+ /* identifier */ SourceLocation(),
+ Namespc,
+ /* Ancestor */ CurContext);
+ UD->setImplicit();
+ CurContext->addDecl(UD);
}
// Although we could have an invalid decl (i.e. the namespace name is a
--- /dev/null
+// RUN: clang-cc -emit-llvm %s -o - | FileCheck %s
+
+namespace {
+ // CHECK: @_ZN12_GLOBAL__N_11aE = internal global i32 0
+ int a = 0;
+
+ // CHECK: define internal i32 @_ZN12_GLOBAL__N_13fooEv()
+ int foo() {
+ return 32;
+ }
+
+ // CHECK: define internal i32 @_ZN12_GLOBAL__N_11A3fooEv()
+ namespace A {
+ int foo() {
+ return 45;
+ }
+ }
+}
+
+int concrete() {
+ return a + foo() + A::foo();
+}