611af8c5ae025b07ede27aba089d1c3b57f5c6d7
[oota-llvm.git] / lib / Support / Twine.cpp
1 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
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/ADT/Twine.h"
11 #include "llvm/Support/raw_ostream.h"
12 using namespace llvm;
13
14 std::string Twine::str() const {
15   // FIXME: This should probably use the toVector implementation, once that is
16   // efficient.
17   std::string Res;
18   raw_string_ostream OS(Res);
19   print(OS);
20   OS.flush();
21   return Res;
22 }
23
24 void Twine::toVector(SmallVectorImpl<char> &Out) const {
25   // FIXME: This is very inefficient, since we are creating a large raw_ostream
26   // buffer -- hitting malloc, which we were supposed to avoid -- all when we
27   // have this pretty little small vector available.
28   //
29   // The best way to fix this is to make raw_svector_ostream do the right thing
30   // and be efficient, by augmenting the base raw_ostream with the ability to
31   // have the buffer managed by a concrete implementation.
32   raw_svector_ostream OS(Out);
33   print(OS);
34 }
35
36 void Twine::printOneChild(raw_ostream &OS, const void *Ptr, 
37                           NodeKind Kind) const {
38   switch (Kind) {
39   case Twine::NullKind: break;
40   case Twine::EmptyKind: break;
41   case Twine::TwineKind:
42     static_cast<const Twine*>(Ptr)->print(OS); 
43     break;
44   case Twine::CStringKind: 
45     OS << static_cast<const char*>(Ptr); 
46     break;
47   case Twine::StdStringKind:
48     OS << *static_cast<const std::string*>(Ptr); 
49     break;
50   case Twine::StringRefKind:
51     OS << *static_cast<const StringRef*>(Ptr); 
52     break;
53   case Twine::DecUIKind:
54     OS << *static_cast<const unsigned int*>(Ptr);
55     break;
56   case Twine::DecIKind:
57     OS << *static_cast<const int*>(Ptr);
58     break;
59   case Twine::DecULKind:
60     OS << *static_cast<const unsigned long*>(Ptr);
61     break;
62   case Twine::DecLKind:
63     OS << *static_cast<const long*>(Ptr);
64     break;
65   case Twine::DecULLKind:
66     OS << *static_cast<const unsigned long long*>(Ptr);
67     break;
68   case Twine::DecLLKind:
69     OS << *static_cast<const long long*>(Ptr);
70     break;
71   case Twine::UHexKind:
72     OS.write_hex(*static_cast<const uint64_t*>(Ptr));
73     break;
74   }
75 }
76
77 void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr, 
78                               NodeKind Kind) const {
79   switch (Kind) {
80   case Twine::NullKind:
81     OS << "null"; break;
82   case Twine::EmptyKind:
83     OS << "empty"; break;
84   case Twine::TwineKind:
85     OS << "rope:";
86     static_cast<const Twine*>(Ptr)->printRepr(OS);
87     break;
88   case Twine::CStringKind:
89     OS << "cstring:\""
90        << static_cast<const char*>(Ptr) << "\"";
91     break;
92   case Twine::StdStringKind:
93     OS << "std::string:\""
94        << static_cast<const std::string*>(Ptr) << "\"";
95     break;
96   case Twine::StringRefKind:
97     OS << "stringref:\""
98        << static_cast<const StringRef*>(Ptr) << "\"";
99     break;
100   case Twine::DecUIKind:
101     OS << "decUI:\"" << *static_cast<const unsigned int*>(Ptr) << "\"";
102     break;
103   case Twine::DecIKind:
104     OS << "decI:\"" << *static_cast<const int*>(Ptr) << "\"";
105     break;
106   case Twine::DecULKind:
107     OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
108     break;
109   case Twine::DecLKind:
110     OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
111     break;
112   case Twine::DecULLKind:
113     OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
114     break;
115   case Twine::DecLLKind:
116     OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
117     break;
118   case Twine::UHexKind:
119     OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
120     break;
121   }
122 }
123
124 void Twine::print(raw_ostream &OS) const {
125   printOneChild(OS, LHS, getLHSKind());
126   printOneChild(OS, RHS, getRHSKind());
127 }
128
129 void Twine::printRepr(raw_ostream &OS) const {
130   OS << "(Twine ";
131   printOneChildRepr(OS, LHS, getLHSKind());
132   OS << " ";
133   printOneChildRepr(OS, RHS, getRHSKind());
134   OS << ")";
135 }
136
137 void Twine::dump() const {
138   print(llvm::errs());
139 }
140
141 void Twine::dumpRepr() const {
142   printRepr(llvm::errs());
143 }