Simplify from my last change. Assert1 is a macro that makes its caller return,
[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                                     ManglerPrefixTy PrefixTy) {
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
63       if (PrefixTy == Mangler::Private)
64         Result = PrivatePrefix + Result;
65       else if (PrefixTy == Mangler::LinkerPrivate)
66         Result = LinkerPrivatePrefix + Result;
67     }
68
69     return Result;
70   }
71
72   bool NeedPrefix = true;
73   bool NeedQuotes = false;
74   std::string Result;    
75   std::string::const_iterator I = X.begin();
76   if (*I == 1) {
77     NeedPrefix = false;
78     ++I;  // Skip over the marker.
79   }
80
81   // If the first character is a number, we need quotes.
82   if (*I >= '0' && *I <= '9')
83     NeedQuotes = true;
84     
85   // Do an initial scan of the string, checking to see if we need quotes or
86   // to escape a '"' or not.
87   if (!NeedQuotes)
88     for (std::string::const_iterator E = X.end(); I != E; ++I)
89       if (!isCharAcceptable(*I)) {
90         NeedQuotes = true;
91         break;
92       }
93     
94   // In the common case, we don't need quotes.  Handle this quickly.
95   if (!NeedQuotes) {
96     if (!NeedPrefix)
97       return X.substr(1);   // Strip off the \001.
98     
99     Result = Prefix + X;
100
101     if (PrefixTy == Mangler::Private)
102       Result = PrivatePrefix + Result;
103     else if (PrefixTy == Mangler::LinkerPrivate)
104       Result = LinkerPrivatePrefix + Result;
105
106     return Result;
107   }
108
109   if (NeedPrefix)
110     Result = X.substr(0, I-X.begin());
111     
112   // Otherwise, construct the string the expensive way.
113   for (std::string::const_iterator E = X.end(); I != E; ++I) {
114     if (*I == '"')
115       Result += "_QQ_";
116     else if (*I == '\n')
117       Result += "_NL_";
118     else
119       Result += *I;
120   }
121
122   if (NeedPrefix) {
123     Result = Prefix + Result;
124
125     if (PrefixTy == Mangler::Private)
126       Result = PrivatePrefix + Result;
127     else if (PrefixTy == Mangler::LinkerPrivate)
128       Result = LinkerPrivatePrefix + Result;
129   }
130
131   Result = '"' + Result + '"';
132   return Result;
133 }
134
135 /// getMangledName - Returns the mangled name of V, an LLVM Value,
136 /// in the current module.  If 'Suffix' is specified, the name ends with the
137 /// specified suffix.  If 'ForcePrivate' is specified, the label is specified
138 /// to have a private label prefix.
139 ///
140 std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
141                                     bool ForcePrivate) {
142   assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
143          "Intrinsic functions cannot be mangled by Mangler");
144
145   ManglerPrefixTy PrefixTy =
146     (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
147       GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
148
149   if (GV->hasName())
150     return makeNameProper(GV->getNameStr() + Suffix, PrefixTy);
151   
152   // Get the ID for the global, assigning a new one if we haven't got one
153   // already.
154   unsigned &ID = AnonGlobalIDs[GV];
155   if (ID == 0) ID = NextAnonGlobalID++;
156   
157   // Must mangle the global into a unique ID.
158   return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy);
159 }
160
161 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
162                  const char *linkerPrivatePrefix)
163   : Prefix(prefix), PrivatePrefix(privatePrefix),
164     LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
165     NextAnonGlobalID(1) {
166   std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
167
168   // Letters and numbers are acceptable.
169   for (unsigned char X = 'a'; X <= 'z'; ++X)
170     markCharAcceptable(X);
171   for (unsigned char X = 'A'; X <= 'Z'; ++X)
172     markCharAcceptable(X);
173   for (unsigned char X = '0'; X <= '9'; ++X)
174     markCharAcceptable(X);
175   
176   // These chars are acceptable.
177   markCharAcceptable('_');
178   markCharAcceptable('$');
179   markCharAcceptable('.');
180 }