Get MCSymbol out of the mangling business, and move all the logic
[oota-llvm.git] / lib / MC / MCSymbol.cpp
1 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
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 #include "llvm/MC/MCSymbol.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
15
16 // Sentinel value for the absolute pseudo section.
17 const MCSection *MCSymbol::AbsolutePseudoSection =
18   reinterpret_cast<const MCSection *>(1);
19
20 static bool isAcceptableChar(char C) {
21   if ((C < 'a' || C > 'z') &&
22       (C < 'A' || C > 'Z') &&
23       (C < '0' || C > '9') &&
24       C != '_' && C != '$' && C != '.' && C != '@')
25     return false;
26   return true;
27 }
28
29 /// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
30 /// for this assembler.
31 static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
32   assert(!Str.empty() && "Cannot create an empty MCSymbol");
33   
34   // If the first character is a number and the target does not allow this, we
35   // need quotes.
36   if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
37     return true;
38   
39   // If any of the characters in the string is an unacceptable character, force
40   // quotes.
41   for (unsigned i = 0, e = Str.size(); i != e; ++i)
42     if (!isAcceptableChar(Str[i]))
43       return true;
44   return false;
45 }
46
47 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
48   // The name for this MCSymbol is required to be a valid target name.  However,
49   // some targets support quoting names with funny characters.  If the name
50   // contains a funny character, then print it quoted.
51   if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) {
52     OS << getName();
53     return;
54   }
55     
56   OS << '"' << getName() << '"';
57 }
58
59 void MCSymbol::dump() const {
60   print(dbgs(), 0);
61 }