aa230d48e86d391f19c08c748e3d341ce5e2459f
[oota-llvm.git] / include / llvm / Support / Mangler.h
1 //===-- llvm/Support/Mangler.h - Self-contained name mangler ----*- C++ -*-===//
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 various backends.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_MANGLER_H
15 #define LLVM_SUPPORT_MANGLER_H
16
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include <string>
20
21 namespace llvm {
22 class Twine;
23 class Type;
24 class Module;
25 class Value;
26 class GlobalValue;
27 template <typename T> class SmallVectorImpl; 
28
29 class Mangler {
30 public:
31   enum ManglerPrefixTy {
32     Default,               ///< Emit default string before each symbol.
33     Private,               ///< Emit "private" prefix before each symbol.
34     LinkerPrivate          ///< Emit "linker private" prefix before each symbol.
35   };
36
37 private:
38   /// Prefix - This string is added to each symbol that is emitted, unless the
39   /// symbol is marked as not needing this prefix.
40   const char *Prefix;
41
42   /// PrivatePrefix - This string is emitted before each symbol with private
43   /// linkage.
44   const char *PrivatePrefix;
45
46   /// LinkerPrivatePrefix - This string is emitted before each symbol with
47   /// "linker_private" linkage.
48   const char *LinkerPrivatePrefix;
49
50   /// UseQuotes - If this is set, the target accepts global names in quotes,
51   /// e.g. "foo bar" is a legal name.  This syntax is used instead of escaping
52   /// the space character.  By default, this is false.
53   bool UseQuotes;
54
55   /// SymbolsCanStartWithDigit - If this is set, the target allows symbols to
56   /// start with digits (e.g., "0x0021").  By default, this is false.
57   bool SymbolsCanStartWithDigit;
58
59   /// AnonGlobalIDs - We need to give global values the same name every time
60   /// they are mangled.  This keeps track of the number we give to anonymous
61   /// ones.
62   ///
63   DenseMap<const GlobalValue*, unsigned> AnonGlobalIDs;
64
65   /// NextAnonGlobalID - This simple counter is used to unique value names.
66   ///
67   unsigned NextAnonGlobalID;
68
69   /// AcceptableChars - This bitfield contains a one for each character that is
70   /// allowed to be part of an unmangled name.
71   unsigned AcceptableChars[256 / 32];
72
73 public:
74   // Mangler ctor - if a prefix is specified, it will be prepended onto all
75   // symbols.
76   Mangler(Module &M, const char *Prefix = "", const char *privatePrefix = "",
77           const char *linkerPrivatePrefix = "");
78
79   /// setUseQuotes - If UseQuotes is set to true, this target accepts quoted
80   /// strings for assembler labels.
81   void setUseQuotes(bool Val) { UseQuotes = Val; }
82
83   /// setSymbolsCanStartWithDigit - If SymbolsCanStartWithDigit is set to true,
84   /// this target allows symbols to start with digits.
85   void setSymbolsCanStartWithDigit(bool Val) { SymbolsCanStartWithDigit = Val; }
86
87   /// Acceptable Characters - This allows the target to specify which characters
88   /// are acceptable to the assembler without being mangled.  By default we
89   /// allow letters, numbers, '_', '$', '.', which is what GAS accepts, and '@'.
90   void markCharAcceptable(unsigned char X) {
91     AcceptableChars[X/32] |= 1 << (X&31);
92   }
93   void markCharUnacceptable(unsigned char X) {
94     AcceptableChars[X/32] &= ~(1 << (X&31));
95   }
96   bool isCharAcceptable(unsigned char X) const {
97     return (AcceptableChars[X/32] & (1 << (X&31))) != 0;
98   }
99
100   /// getMangledName - Returns the mangled name of V, an LLVM Value,
101   /// in the current module.  If 'Suffix' is specified, the name ends with the
102   /// specified suffix.  If 'ForcePrivate' is specified, the label is specified
103   /// to have a private label prefix.
104   ///
105   /// FIXME: This is deprecated, new code should use getNameWithPrefix and use
106   /// MCSymbol printing to handle quotes or not etc.
107   ///
108   std::string getMangledName(const GlobalValue *V, const char *Suffix = "",
109                              bool ForcePrivate = false);
110
111   /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
112   /// and the specified global variable's name.  If the global variable doesn't
113   /// have a name, this fills in a unique name for the global.
114   void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
115                          bool isImplicitlyPrivate);
116   
117   /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
118   /// and the specified name as the global variable name.  GVName must not be
119   /// empty.
120   void getNameWithPrefix(SmallVectorImpl<char> &OutName, const Twine &GVName,
121                          ManglerPrefixTy PrefixTy = Mangler::Default);
122
123 private:
124   /// makeNameProper - We don't want identifier names with ., space, or
125   /// - in them, so we mangle these characters into the strings "d_",
126   /// "s_", and "D_", respectively. This is a very simple mangling that
127   /// doesn't guarantee unique names for values. getValueName already
128   /// does this for you, so there's no point calling it on the result
129   /// from getValueName.
130   ///
131   /// FIXME: This is deprecated, new code should use getNameWithPrefix and use
132   /// MCSymbol printing to handle quotes or not etc.
133   ///
134   void makeNameProper(SmallVectorImpl<char> &OutName,
135                       const Twine &Name,
136                       ManglerPrefixTy PrefixTy = Mangler::Default);
137   
138 };
139
140 } // End llvm namespace
141
142 #endif // LLVM_SUPPORT_MANGLER_H