blob: 611af8c5ae025b07ede27aba089d1c3b57f5c6d7 [file] [log] [blame]
Daniel Dunbar2538f7a2009-07-24 07:04:27 +00001//===-- 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"
12using namespace llvm;
13
14std::string Twine::str() const {
Daniel Dunbar2b800df2009-08-02 04:12:28 +000015 // FIXME: This should probably use the toVector implementation, once that is
16 // efficient.
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000017 std::string Res;
18 raw_string_ostream OS(Res);
19 print(OS);
Daniel Dunbar2b800df2009-08-02 04:12:28 +000020 OS.flush();
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000021 return Res;
22}
23
24void Twine::toVector(SmallVectorImpl<char> &Out) const {
Daniel Dunbar763457e2009-07-29 07:08:44 +000025 // 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.
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000032 raw_svector_ostream OS(Out);
33 print(OS);
34}
35
36void 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;
Daniel Dunbar763457e2009-07-29 07:08:44 +000041 case Twine::TwineKind:
42 static_cast<const Twine*>(Ptr)->print(OS);
43 break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000044 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;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000053 case Twine::DecUIKind:
54 OS << *static_cast<const unsigned int*>(Ptr);
Daniel Dunbar0165a2c2009-07-30 03:47:15 +000055 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000056 case Twine::DecIKind:
57 OS << *static_cast<const int*>(Ptr);
Daniel Dunbar0165a2c2009-07-30 03:47:15 +000058 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000059 case Twine::DecULKind:
60 OS << *static_cast<const unsigned long*>(Ptr);
Daniel Dunbar763457e2009-07-29 07:08:44 +000061 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000062 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);
Daniel Dunbar763457e2009-07-29 07:08:44 +000070 break;
71 case Twine::UHexKind:
Daniel Dunbar0fffbaf2009-07-30 18:30:19 +000072 OS.write_hex(*static_cast<const uint64_t*>(Ptr));
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000073 break;
74 }
75}
76
77void 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;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000084 case Twine::TwineKind:
85 OS << "rope:";
86 static_cast<const Twine*>(Ptr)->printRepr(OS);
87 break;
Daniel Dunbar763457e2009-07-29 07:08:44 +000088 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;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000100 case Twine::DecUIKind:
101 OS << "decUI:\"" << *static_cast<const unsigned int*>(Ptr) << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +0000102 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000103 case Twine::DecIKind:
104 OS << "decI:\"" << *static_cast<const int*>(Ptr) << "\"";
Daniel Dunbar0165a2c2009-07-30 03:47:15 +0000105 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000106 case Twine::DecULKind:
107 OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
Daniel Dunbar0165a2c2009-07-30 03:47:15 +0000108 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000109 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) << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +0000117 break;
118 case Twine::UHexKind:
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000119 OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +0000120 break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +0000121 }
122}
123
124void Twine::print(raw_ostream &OS) const {
125 printOneChild(OS, LHS, getLHSKind());
126 printOneChild(OS, RHS, getRHSKind());
127}
128
129void 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
137void Twine::dump() const {
138 print(llvm::errs());
139}
140
141void Twine::dumpRepr() const {
142 printRepr(llvm::errs());
143}