Cleanups:
[oota-llvm.git] / lib / VMCore / Mangler.cpp
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2 //
3 // Unified name mangler for CWriter and assembly backends.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include <iostream>
8 #include <set>
9 #include <string>
10 #include "llvm/Value.h"
11 #include "llvm/Module.h"
12 #include "llvm/Type.h"
13 #include "Support/StringExtras.h"
14 #include "llvm/Support/Mangler.h"
15
16 /// makeNameProper - We don't want identifier names with ., space, or
17 /// - in them, so we mangle these characters into the strings "d_",
18 /// "s_", and "D_", respectively.
19 /// 
20 std::string Mangler::makeNameProper(const std::string &x) {
21   std::string tmp;
22   for (std::string::const_iterator sI = x.begin(), sEnd = x.end();
23        sI != sEnd; sI++)
24     switch (*sI) {
25     case '.': tmp += "d_"; break;
26     case ' ': tmp += "s_"; break;
27     case '-': tmp += "D_"; break;
28     default:  tmp += *sI;
29     }
30   return tmp;
31 }
32
33 std::string Mangler::getValueName(const Value *V) {
34   // Check to see whether we've already named V.
35   ValueMap::iterator VI = Memo.find(V);
36   if (VI != Memo.end()) {
37     return VI->second; // Return the old name for V.
38   }
39
40   std::string name;
41   if (V->hasName()) { // Print out the label if it exists...
42     // Name mangling occurs as follows:
43     // - If V is not a global, mangling always occurs.
44     // - Otherwise, mangling occurs when any of the following are true:
45     //   1) V has internal linkage
46     //   2) V's name would collide if it is not mangled.
47     //
48     const GlobalValue* gv = dyn_cast<GlobalValue>(V);
49     if(gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
50       name = makeNameProper(gv->getName());
51     } else {
52       // Non-global, or global with internal linkage / colliding name
53       // -> mangle.
54       name = "l" + utostr(V->getType()->getUniqueID()) + "_" +
55         makeNameProper(V->getName());      
56     }
57   } else {
58     name = "ltmp_" + utostr(Count++) + "_"
59       + utostr(V->getType()->getUniqueID());
60   }
61   Memo[V] = name;
62   return name;
63 }
64
65 Mangler::Mangler(Module &M_) : M(M_)
66 {
67   // Calculate which global values have names that will collide when we throw
68   // away type information.
69   std::set<std::string> FoundNames;
70   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
71     if (I->hasName())                      // If the global has a name...
72       if (FoundNames.count(I->getName()))  // And the name is already used
73         MangledGlobals.insert(I);          // Mangle the name
74       else
75         FoundNames.insert(I->getName());   // Otherwise, keep track of name
76
77   for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
78     if (I->hasName())                      // If the global has a name...
79       if (FoundNames.count(I->getName()))  // And the name is already used
80         MangledGlobals.insert(I);          // Mangle the name
81       else
82         FoundNames.insert(I->getName());   // Otherwise, keep track of name
83 }
84