eliminate the Mangler::PreserveAsmNames bit, the sole client of this
[oota-llvm.git] / lib / VMCore / Mangler.cpp
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Unified name mangler for CWriter and assembly backends.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Mangler.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Module.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringMap.h"
20 using namespace llvm;
21
22 static char HexDigit(int V) {
23   return V < 10 ? V+'0' : V+'A'-10;
24 }
25
26 static std::string MangleLetter(unsigned char C) {
27   char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
28   return Result;
29 }
30
31 /// makeNameProper - We don't want identifier names non-C-identifier characters
32 /// in them, so mangle them as appropriate.
33 ///
34 std::string Mangler::makeNameProper(const std::string &X,
35                                     bool hasPrivateLinkage) {
36   assert(!X.empty() && "Cannot mangle empty strings");
37   
38   if (!UseQuotes) {
39     std::string Result;
40
41     // If X does not start with (char)1, add the prefix.
42     bool NeedPrefix = true;
43     std::string::const_iterator I = X.begin();
44     if (*I == 1) {
45       NeedPrefix = false;
46       ++I;  // Skip over the marker.
47     }
48     
49     // Mangle the first letter specially, don't allow numbers.
50     if (*I >= '0' && *I <= '9')
51       Result += MangleLetter(*I++);
52
53     for (std::string::const_iterator E = X.end(); I != E; ++I) {
54       if (!isCharAcceptable(*I))
55         Result += MangleLetter(*I);
56       else
57         Result += *I;
58     }
59
60     if (NeedPrefix) {
61       Result = Prefix + Result;
62       if (hasPrivateLinkage)
63         Result = PrivatePrefix + Result;
64     }
65     return Result;
66   }
67
68   bool NeedPrefix = true;
69   bool NeedQuotes = false;
70   std::string Result;    
71   std::string::const_iterator I = X.begin();
72   if (*I == 1) {
73     NeedPrefix = false;
74     ++I;  // Skip over the marker.
75   }
76
77   // If the first character is a number, we need quotes.
78   if (*I >= '0' && *I <= '9')
79     NeedQuotes = true;
80     
81   // Do an initial scan of the string, checking to see if we need quotes or
82   // to escape a '"' or not.
83   if (!NeedQuotes)
84     for (std::string::const_iterator E = X.end(); I != E; ++I)
85       if (!isCharAcceptable(*I)) {
86         NeedQuotes = true;
87         break;
88       }
89     
90   // In the common case, we don't need quotes.  Handle this quickly.
91   if (!NeedQuotes) {
92     if (!NeedPrefix)
93       return X.substr(1);   // Strip off the \001.
94     
95     Result = Prefix + X;
96     if (hasPrivateLinkage)
97       Result = PrivatePrefix + Result;
98     return Result;
99   }
100   
101   Result = X.substr(0, I-X.begin());
102     
103   // Otherwise, construct the string the expensive way.
104   for (std::string::const_iterator E = X.end(); I != E; ++I) {
105     if (*I == '"')
106       Result += "_QQ_";
107     else if (*I == '\n')
108       Result += "_NL_";
109     else
110       Result += *I;
111   }
112
113   if (NeedPrefix) {
114     Result = Prefix + Result;
115     if (hasPrivateLinkage)
116       Result = PrivatePrefix + Result;
117   }
118   Result = '"' + Result + '"';
119   return Result;
120 }
121
122 /// getMangledName - Returns the mangled name of V, an LLVM Value,
123 /// in the current module.  If 'Suffix' is specified, the name ends with the
124 /// specified suffix.  If 'ForcePrivate' is specified, the label is specified
125 /// to have a private label prefix.
126 ///
127 std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
128                                     bool ForcePrivate) {
129   assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
130          "Intrinsic functions cannot be mangled by Mangler");
131   
132   if (GV->hasName())
133     return makeNameProper(GV->getName() + Suffix,
134                           GV->hasPrivateLinkage() | ForcePrivate);
135   
136   // Get the ID for the global, assigning a new one if we haven't got one
137   // already.
138   unsigned &ID = AnonGlobalIDs[GV];
139   if (ID == 0) ID = NextAnonGlobalID++;
140   
141   // Must mangle the global into a unique ID.
142   return makeNameProper("__unnamed_" + utostr(ID) + Suffix,
143                         GV->hasPrivateLinkage() | ForcePrivate);
144 }
145
146 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix)
147   : Prefix(prefix), PrivatePrefix (privatePrefix), UseQuotes(false),
148     NextAnonGlobalID(1) {
149   std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
150
151   // Letters and numbers are acceptable.
152   for (unsigned char X = 'a'; X <= 'z'; ++X)
153     markCharAcceptable(X);
154   for (unsigned char X = 'A'; X <= 'Z'; ++X)
155     markCharAcceptable(X);
156   for (unsigned char X = '0'; X <= '9'; ++X)
157     markCharAcceptable(X);
158   
159   // These chars are acceptable.
160   markCharAcceptable('_');
161   markCharAcceptable('$');
162   markCharAcceptable('.');
163 }