[cfe-commits] r64984 - in /cfe/trunk/lib/CodeGen: CGDecl.cpp CGExpr.cpp CodeGenFunction.cpp CodeGenModule.cpp CodeGenModule.h Mangle.cpp Mangle.h

Douglas Gregor dgregor at apple.com
Wed Feb 18 15:53:56 PST 2009


Author: dgregor
Date: Wed Feb 18 17:53:56 2009
New Revision: 64984

URL: http://llvm.org/viewvc/llvm-project?rev=64984&view=rev
Log:
Address Chris's comments regarding C++ name mangling.


Modified:
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h
    cfe/trunk/lib/CodeGen/Mangle.cpp
    cfe/trunk/lib/CodeGen/Mangle.h

Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=64984&r1=64983&r2=64984&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Wed Feb 18 17:53:56 2009
@@ -106,7 +106,7 @@
 
   std::string ContextName;
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
-    ContextName = CGM.getMangledName(FD)->getName();
+    ContextName = CGM.getMangledName(FD);
   else if (isa<ObjCMethodDecl>(CurFuncDecl))
     ContextName = std::string(CurFn->getNameStart(), 
                               CurFn->getNameStart() + CurFn->getNameLen());
@@ -171,7 +171,7 @@
       // A normal fixed sized variable becomes an alloca in the entry block.
       const llvm::Type *LTy = ConvertType(Ty);
       llvm::AllocaInst *Alloc =
-        CreateTempAlloca(LTy, CGM.getMangledName(&D)->getName());
+        CreateTempAlloca(LTy, CGM.getMangledName(&D));
       unsigned align = getContext().getTypeAlign(Ty);
       if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
         align = std::max(align, AA->getAlignment());

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=64984&r1=64983&r2=64984&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Wed Feb 18 17:53:56 2009
@@ -702,7 +702,7 @@
 
   std::string FunctionName;
   if(const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) {
-    FunctionName = CGM.getMangledName(FD)->getName();
+    FunctionName = CGM.getMangledName(FD);
   } else {
     // Just get the mangled name.
     FunctionName = CurFn->getName();

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=64984&r1=64983&r2=64984&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Wed Feb 18 17:53:56 2009
@@ -171,8 +171,7 @@
   if (CGDebugInfo *DI = getDebugInfo()) {
     DI->setLocation(StartLoc);
     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
-      DI->EmitFunctionStart(CGM.getMangledName(FD)->getName(),
-                            RetTy, CurFn, Builder);
+      DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder);
     } else {
       // Just use LLVM function name.
       DI->EmitFunctionStart(Fn->getName().c_str(), 

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=64984&r1=64983&r2=64984&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Wed Feb 18 17:53:56 2009
@@ -170,13 +170,15 @@
 /// FIXME: Performance here is going to be terribly until we start
 /// caching mangled names. However, we should fix the problem above
 /// first.
-IdentifierInfo *CodeGenModule::getMangledName(const NamedDecl *ND) const {
-  std::string Name;
-  llvm::raw_string_ostream Out(Name);
+const char *CodeGenModule::getMangledName(const NamedDecl *ND) {
+  llvm::SmallString<256> Name;
+  llvm::raw_svector_ostream Out(Name);
   if (!mangleName(ND, Context, Out))
-    return ND->getIdentifier();
+    return ND->getIdentifier()->getName();
 
-  return &Context.Idents.get(Out.str());
+  Name += '\0';
+  return MangledNames.GetOrCreateValue(Name.begin(), Name.end())
+           .getKeyData();
 }
 
 /// AddGlobalCtor - Add a function to the list that will be called before
@@ -379,7 +381,7 @@
     llvm::GlobalValue *GA = 
       new llvm::GlobalAlias(aliasee->getType(),
                             llvm::Function::ExternalLinkage,
-                            getMangledName(D)->getName(), aliasee, 
+                            getMangledName(D), aliasee, 
                             &getModule());
     
     llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
@@ -582,7 +584,7 @@
     llvm::GlobalVariable *GV = 
       new llvm::GlobalVariable(Ty, false, 
                                llvm::GlobalValue::ExternalLinkage,
-                               0, getMangledName(D)->getName(), &getModule(), 
+                               0, getMangledName(D), &getModule(), 
                                0, ASTTy.getAddressSpace());
     Entry = GV;
 
@@ -630,7 +632,7 @@
   if (!GV) {
     GV = new llvm::GlobalVariable(InitType, false, 
                                   llvm::GlobalValue::ExternalLinkage,
-                                  0, getMangledName(D)->getName(), 
+                                  0, getMangledName(D), 
                                   &getModule(), 0, ASTTy.getAddressSpace());
   } else if (GV->getType() != 
              llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) {
@@ -654,7 +656,7 @@
     // Make a new global with the correct type
     GV = new llvm::GlobalVariable(InitType, false, 
                                   llvm::GlobalValue::ExternalLinkage,
-                                  0, getMangledName(D)->getName(), 
+                                  0, getMangledName(D), 
                                   &getModule(), 0, ASTTy.getAddressSpace());
     // Steal the name of the old global
     GV->takeName(OldGV);
@@ -753,7 +755,7 @@
   const llvm::Type *Ty = getTypes().ConvertType(D->getType());
   llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 
                                              llvm::Function::ExternalLinkage,
-                                             getMangledName(D)->getName(),
+                                             getMangledName(D),
                                              &getModule());
   SetFunctionAttributes(D, F);
   return F;

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=64984&r1=64983&r2=64984&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Wed Feb 18 17:53:56 2009
@@ -18,6 +18,7 @@
 #include "clang/AST/Attr.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringSet.h"
 
 #include "CGCall.h"
 
@@ -81,14 +82,23 @@
   /// will name them and patch up conflicts when we release the module.
   std::vector< std::pair<llvm::Function*, std::string> > RuntimeFunctions;
 
-  /// GlobalDeclMap - Mapping of decl names global variables we have already
-  /// emitted. Note that the entries in this map are the actual globals and
-  /// therefore may not be of the same type as the decl, they should be
-  /// bitcasted on retrieval. Also note that the globals are keyed on their
-  /// source name, not the global name (which may change with attributes such as
-  /// asm-labels).  This key to this map should be generated using
-  /// getMangledName().
-  llvm::DenseMap<IdentifierInfo*, llvm::GlobalValue*> GlobalDeclMap;
+  /// GlobalDeclMap - Mapping of decl names (represented as unique
+  /// character pointers from either the identifier table or the set
+  /// of mangled names) to global variables we have already
+  /// emitted. Note that the entries in this map are the actual
+  /// globals and therefore may not be of the same type as the decl,
+  /// they should be bitcasted on retrieval. Also note that the
+  /// globals are keyed on their source name, not the global name
+  /// (which may change with attributes such as asm-labels).  This key
+  /// to this map should be generated using getMangledName().
+  llvm::DenseMap<const char*, llvm::GlobalValue*> GlobalDeclMap;
+
+  /// \brief Contains the strings used for mangled names.
+  ///
+  /// FIXME: Eventually, this should map from the semantic/canonical
+  /// declaration for each global entity to its mangled name (if it
+  /// has one).
+  llvm::StringSet<> MangledNames;
 
   /// Aliases - List of aliases in module. These cannot be emitted until all the
   /// code has been seen, as they reference things by name instead of directly
@@ -285,7 +295,7 @@
                               const Decl *TargetDecl,
                               AttributeListType &PAL);
 
-  IdentifierInfo *getMangledName(const NamedDecl *ND) const;
+  const char *getMangledName(const NamedDecl *ND);
 
 
 private:

Modified: cfe/trunk/lib/CodeGen/Mangle.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/Mangle.cpp?rev=64984&r1=64983&r2=64984&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/Mangle.cpp (original)
+++ cfe/trunk/lib/CodeGen/Mangle.cpp Wed Feb 18 17:53:56 2009
@@ -1,4 +1,4 @@
-//===--------------------- Mangle.cpp - Mangle C++ Names ------------------===//
+//===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -18,6 +18,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclCXX.h"
+#include "clang/Basic/SourceManager.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace clang;
@@ -61,17 +62,28 @@
   // FIXME: Actually use a visitor to decode these?
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     bool RequiresMangling = false;
-    // Clang's "overloadable" attribute extension to C/C++
-    if (FD->getAttr<OverloadableAttr>())
+    // No mangled in an "implicit extern C" header.
+    if (Context.getSourceManager().getFileCharacteristic(FD->getLocation())
+          == SrcMgr::C_ExternCSystem)
+      RequiresMangling = false;
+    // Clang's "overloadable" attribute extension to C/C++ implies
+    // name mangling (always).
+    else if (FD->getAttr<OverloadableAttr>())
       RequiresMangling = true;
     else if (Context.getLangOptions().CPlusPlus) {
+      // C++ requires name mangling, unless we're in a C linkage
+      // specification.
       RequiresMangling = true;
-      if (isa<LinkageSpecDecl>(FD->getDeclContext()) &&
-          cast<LinkageSpecDecl>(FD->getDeclContext())->getLanguage() 
-            == LinkageSpecDecl::lang_c) {
-        // Entities with C linkage are not mangled.
-        RequiresMangling = false;
-      } 
+
+      for (const DeclContext *DC = FD->getDeclContext(); 
+           !DC->isTranslationUnit(); DC = DC->getParent()) {
+        if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
+          // extern "C" functions don't use name mangling
+          if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
+            RequiresMangling = false;
+          break;
+        }
+      }
     }
 
     if (RequiresMangling) {
@@ -95,15 +107,7 @@
     return false;
 
   const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
-  const IdentifierInfo *Name = NS->getIdentifier();
-  if (Name->getLength() != 3)
-    return false;
-
-  const char *Str = Name->getName();
-  if (Str[0] != 's' || Str[1] != 't' || Str[2] != 'd')
-    return false;
-      
-  return true;
+  return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
 }
 
 void CXXNameMangler::mangleName(const NamedDecl *ND) {
@@ -307,7 +311,7 @@
 
   case OO_None: 
   case NUM_OVERLOADED_OPERATORS:
-    assert(false && "Not an ovelroaded operator"); 
+    assert(false && "Not an overloaded operator"); 
     break;
   }
 }
@@ -516,7 +520,11 @@
   bool mangleName(const NamedDecl *D, ASTContext &Context, 
                   llvm::raw_ostream &os) {
     CXXNameMangler Mangler(Context, os);
-    return Mangler.mangle(D);
+    if (!Mangler.mangle(D))
+      return false;
+    
+    os.flush();
+    return true;
   }
 }
 

Modified: cfe/trunk/lib/CodeGen/Mangle.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/Mangle.h?rev=64984&r1=64983&r2=64984&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/Mangle.h (original)
+++ cfe/trunk/lib/CodeGen/Mangle.h Wed Feb 18 17:53:56 2009
@@ -1,4 +1,4 @@
-//===---------------------- Mangle.h - Mangle C++ Names -------------------===//
+//===--- Mangle.h - Mangle C++ Names ----------------------------*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //





More information about the cfe-commits mailing list