Add a helper method
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 <map>
18 #include <set>
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   /// Memo - This is used to remember the name that we assign a value.
38   ///
39   std::map<const Value*, std::string> Memo;
40
41   /// Count - This simple counter is used to unique value names.
42   ///
43   unsigned Count;
44   
45   /// TypeMap - If the client wants us to unique types, this keeps track of the
46   /// current assignments and TypeCounter keeps track of the next id to assign.
47   std::map<const Type*, unsigned> TypeMap;
48   unsigned TypeCounter;
49
50   /// This keeps track of which global values have had their names
51   /// mangled in the current module.
52   ///
53   std::set<const GlobalValue*> MangledGlobals;
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 = "");
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   /// Acceptable Characters - This allows the target to specify which characters
69   /// are acceptable to the assembler without being mangled.  By default we
70   /// allow letters, numbers, '_', '$', and '.', which is what GAS accepts.
71   void markCharAcceptable(unsigned char X) {
72     AcceptableChars[X/32] |= 1 << (X&31);
73   }
74   void markCharUnacceptable(unsigned char X) {
75     AcceptableChars[X/32] &= ~(1 << (X&31));
76   }
77   bool isCharAcceptable(unsigned char X) const {
78     return (AcceptableChars[X/32] & (1 << (X&31))) != 0;
79   }
80   
81   /// getTypeID - Return a unique ID for the specified LLVM type.
82   ///
83   unsigned getTypeID(const Type *Ty);
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);
89   std::string getValueName(const Value *V);
90
91   /// makeNameProper - We don't want identifier names with ., space, or
92   /// - in them, so we mangle these characters into the strings "d_",
93   /// "s_", and "D_", respectively. This is a very simple mangling that
94   /// doesn't guarantee unique names for values. getValueName already
95   /// does this for you, so there's no point calling it on the result
96   /// from getValueName.
97   ///
98   std::string makeNameProper(const std::string &x, const char *Prefix = "");
99   
100 private:
101   void InsertName(GlobalValue *GV, std::map<std::string, GlobalValue*> &Names);
102 };
103
104 } // End llvm namespace
105
106 #endif // LLVM_SUPPORT_MANGLER_H