0dc26edc2df78c57220b35d59a61cbf49b5c671a
[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 Type;
23 class Module;
24 class Value;
25 class GlobalValue;
26
27 class Mangler {
28   /// Prefix - This string is added to each symbol that is emitted, unless the
29   /// symbol is marked as not needing this prefix.
30   const char *Prefix;
31
32   /// PrivatePrefix - This string is emitted before each symbol with private
33   /// linkage.
34   const char *PrivatePrefix;
35
36   /// UseQuotes - If this is set, the target accepts global names in quotes,
37   /// e.g. "foo bar" is a legal name.  This syntax is used instead of escaping
38   /// the space character.  By default, this is false.
39   bool UseQuotes;
40
41   /// PreserveAsmNames - If this is set, the asm escape character is not removed
42   /// from names with 'asm' specifiers.
43   bool PreserveAsmNames;
44
45   /// AnonGlobalIDs - We need to give global values the same name every time
46   /// they are mangled.  This keeps track of the number we give to anonymous
47   /// ones.
48   ///
49   DenseMap<const GlobalValue*, unsigned> AnonGlobalIDs;
50
51   /// NextAnonGlobalID - This simple counter is used to unique value names.
52   ///
53   unsigned NextAnonGlobalID;
54
55   /// AcceptableChars - This bitfield contains a one for each character that is
56   /// allowed to be part of an unmangled name.
57   unsigned AcceptableChars[256/32];
58 public:
59
60   // Mangler ctor - if a prefix is specified, it will be prepended onto all
61   // symbols.
62   Mangler(Module &M, const char *Prefix = "", const char *privatePrefix = "");
63
64   /// setUseQuotes - If UseQuotes is set to true, this target accepts quoted
65   /// strings for assembler labels.
66   void setUseQuotes(bool Val) { UseQuotes = Val; }
67
68   /// setPreserveAsmNames - If the mangler should not strip off the asm name
69   /// @verbatim identifier (\001), this should be set. @endverbatim
70   void setPreserveAsmNames(bool Val) { PreserveAsmNames = Val; }
71
72   /// Acceptable Characters - This allows the target to specify which characters
73   /// are acceptable to the assembler without being mangled.  By default we
74   /// allow letters, numbers, '_', '$', and '.', which is what GAS accepts.
75   void markCharAcceptable(unsigned char X) {
76     AcceptableChars[X/32] |= 1 << (X&31);
77   }
78   void markCharUnacceptable(unsigned char X) {
79     AcceptableChars[X/32] &= ~(1 << (X&31));
80   }
81   bool isCharAcceptable(unsigned char X) const {
82     return (AcceptableChars[X/32] & (1 << (X&31))) != 0;
83   }
84
85   /// getValueName - Returns the mangled name of V, an LLVM Value,
86   /// in the current module.
87   ///
88   std::string getValueName(const GlobalValue *V, const char *Suffix = "");
89
90   /// makeNameProper - We don't want identifier names with ., space, or
91   /// - in them, so we mangle these characters into the strings "d_",
92   /// "s_", and "D_", respectively. This is a very simple mangling that
93   /// doesn't guarantee unique names for values. getValueName already
94   /// does this for you, so there's no point calling it on the result
95   /// from getValueName.
96   ///
97   std::string makeNameProper(const std::string &x,
98                              bool hasPrivateLinkage = false);
99 };
100
101 } // End llvm namespace
102
103 #endif // LLVM_SUPPORT_MANGLER_H