Reapply 53476 and 53480, with a fix so that it properly updates
[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   /// UseQuotes - If this is set, the target accepts global names in quotes, 
33   /// e.g. "foo bar" is a legal name.  This syntax is used instead of escaping
34   /// the space character.  By default, this is false.
35   bool UseQuotes;
36   
37   /// PreserveAsmNames - If this is set, the asm escape character is not removed
38   /// from names with 'asm' specifiers. 
39   bool PreserveAsmNames;
40   
41   /// Memo - This is used to remember the name that we assign a value.
42   ///
43   DenseMap<const Value*, std::string> Memo;
44
45   /// Count - This simple counter is used to unique value names.
46   ///
47   unsigned Count;
48   
49   /// TypeMap - If the client wants us to unique types, this keeps track of the
50   /// current assignments and TypeCounter keeps track of the next id to assign.
51   DenseMap<const Type*, unsigned> TypeMap;
52   unsigned TypeCounter;
53
54   /// This keeps track of which global values have had their names
55   /// mangled in the current module.
56   ///
57   SmallPtrSet<const GlobalValue*, 16> MangledGlobals;
58   
59   /// AcceptableChars - This bitfield contains a one for each character that is
60   /// allowed to be part of an unmangled name.
61   unsigned AcceptableChars[256/32];
62 public:
63
64   // Mangler ctor - if a prefix is specified, it will be prepended onto all
65   // symbols.
66   Mangler(Module &M, const char *Prefix = "");
67
68   /// setUseQuotes - If UseQuotes is set to true, this target accepts quoted
69   /// strings for assembler labels.
70   void setUseQuotes(bool Val) { UseQuotes = Val; }
71   
72   /// setPreserveAsmNames - If the mangler should not strip off the asm name
73   /// @verbatim identifier (\001), this should be set. @endverbatim
74   void setPreserveAsmNames(bool Val) { PreserveAsmNames = Val; }
75   
76   /// Acceptable Characters - This allows the target to specify which characters
77   /// are acceptable to the assembler without being mangled.  By default we
78   /// allow letters, numbers, '_', '$', and '.', which is what GAS accepts.
79   void markCharAcceptable(unsigned char X) {
80     AcceptableChars[X/32] |= 1 << (X&31);
81   }
82   void markCharUnacceptable(unsigned char X) {
83     AcceptableChars[X/32] &= ~(1 << (X&31));
84   }
85   bool isCharAcceptable(unsigned char X) const {
86     return (AcceptableChars[X/32] & (1 << (X&31))) != 0;
87   }
88   
89   /// getValueName - Returns the mangled name of V, an LLVM Value,
90   /// in the current module.
91   ///
92   std::string getValueName(const GlobalValue *V, const char *Suffix = "");
93   std::string getValueName(const Value *V);
94
95   /// makeNameProper - We don't want identifier names with ., space, or
96   /// - in them, so we mangle these characters into the strings "d_",
97   /// "s_", and "D_", respectively. This is a very simple mangling that
98   /// doesn't guarantee unique names for values. getValueName already
99   /// does this for you, so there's no point calling it on the result
100   /// from getValueName.
101   ///
102   std::string makeNameProper(const std::string &x, const char *Prefix = "");
103
104 private:
105   /// getTypeID - Return a unique ID for the specified LLVM type.
106   ///
107   unsigned getTypeID(const Type *Ty);
108 };
109
110 } // End llvm namespace
111
112 #endif // LLVM_SUPPORT_MANGLER_H