Anonymous namespaces, sema + codegen.  A lot of semantics are still broken,
apparently because using directives aren't quite working correctly.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83184 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index bc8cc26..f93c604 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -247,6 +247,11 @@
 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;
@@ -1000,7 +1005,9 @@
   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);
diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp
index 61555ad..7555ae5 100644
--- a/lib/CodeGen/Mangle.cpp
+++ b/lib/CodeGen/Mangle.cpp
@@ -254,7 +254,8 @@
     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 *
@@ -403,6 +404,14 @@
   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;
 
@@ -1204,8 +1213,7 @@
 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;
     }