blob: 8b8c0f0db80ce272413a0b455f7fc750c9a4cdd6 [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 {
15 std::string Res;
16 raw_string_ostream OS(Res);
17 print(OS);
18 return Res;
19}
20
21void Twine::toVector(SmallVectorImpl<char> &Out) const {
Daniel Dunbar763457e2009-07-29 07:08:44 +000022 // FIXME: This is very inefficient, since we are creating a large raw_ostream
23 // buffer -- hitting malloc, which we were supposed to avoid -- all when we
24 // have this pretty little small vector available.
25 //
26 // The best way to fix this is to make raw_svector_ostream do the right thing
27 // and be efficient, by augmenting the base raw_ostream with the ability to
28 // have the buffer managed by a concrete implementation.
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000029 raw_svector_ostream OS(Out);
30 print(OS);
31}
32
33void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
34 NodeKind Kind) const {
35 switch (Kind) {
36 case Twine::NullKind: break;
37 case Twine::EmptyKind: break;
Daniel Dunbar763457e2009-07-29 07:08:44 +000038 case Twine::TwineKind:
39 static_cast<const Twine*>(Ptr)->print(OS);
40 break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000041 case Twine::CStringKind:
42 OS << static_cast<const char*>(Ptr);
43 break;
44 case Twine::StdStringKind:
45 OS << *static_cast<const std::string*>(Ptr);
46 break;
47 case Twine::StringRefKind:
48 OS << *static_cast<const StringRef*>(Ptr);
49 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000050 case Twine::DecUIKind:
51 OS << *static_cast<const unsigned int*>(Ptr);
Daniel Dunbar0165a2c2009-07-30 03:47:15 +000052 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000053 case Twine::DecIKind:
54 OS << *static_cast<const int*>(Ptr);
Daniel Dunbar0165a2c2009-07-30 03:47:15 +000055 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000056 case Twine::DecULKind:
57 OS << *static_cast<const unsigned long*>(Ptr);
Daniel Dunbar763457e2009-07-29 07:08:44 +000058 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000059 case Twine::DecLKind:
60 OS << *static_cast<const long*>(Ptr);
61 break;
62 case Twine::DecULLKind:
63 OS << *static_cast<const unsigned long long*>(Ptr);
64 break;
65 case Twine::DecLLKind:
66 OS << *static_cast<const long long*>(Ptr);
Daniel Dunbar763457e2009-07-29 07:08:44 +000067 break;
68 case Twine::UHexKind:
Daniel Dunbar0fffbaf2009-07-30 18:30:19 +000069 OS.write_hex(*static_cast<const uint64_t*>(Ptr));
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000070 break;
71 }
72}
73
74void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
75 NodeKind Kind) const {
76 switch (Kind) {
77 case Twine::NullKind:
78 OS << "null"; break;
79 case Twine::EmptyKind:
80 OS << "empty"; break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000081 case Twine::TwineKind:
82 OS << "rope:";
83 static_cast<const Twine*>(Ptr)->printRepr(OS);
84 break;
Daniel Dunbar763457e2009-07-29 07:08:44 +000085 case Twine::CStringKind:
86 OS << "cstring:\""
87 << static_cast<const char*>(Ptr) << "\"";
88 break;
89 case Twine::StdStringKind:
90 OS << "std::string:\""
91 << static_cast<const std::string*>(Ptr) << "\"";
92 break;
93 case Twine::StringRefKind:
94 OS << "stringref:\""
95 << static_cast<const StringRef*>(Ptr) << "\"";
96 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000097 case Twine::DecUIKind:
98 OS << "decUI:\"" << *static_cast<const unsigned int*>(Ptr) << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +000099 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000100 case Twine::DecIKind:
101 OS << "decI:\"" << *static_cast<const int*>(Ptr) << "\"";
Daniel Dunbar0165a2c2009-07-30 03:47:15 +0000102 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000103 case Twine::DecULKind:
104 OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
Daniel Dunbar0165a2c2009-07-30 03:47:15 +0000105 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000106 case Twine::DecLKind:
107 OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
108 break;
109 case Twine::DecULLKind:
110 OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
111 break;
112 case Twine::DecLLKind:
113 OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +0000114 break;
115 case Twine::UHexKind:
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000116 OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +0000117 break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +0000118 }
119}
120
121void Twine::print(raw_ostream &OS) const {
122 printOneChild(OS, LHS, getLHSKind());
123 printOneChild(OS, RHS, getRHSKind());
124}
125
126void Twine::printRepr(raw_ostream &OS) const {
127 OS << "(Twine ";
128 printOneChildRepr(OS, LHS, getLHSKind());
129 OS << " ";
130 printOneChildRepr(OS, RHS, getRHSKind());
131 OS << ")";
132}
133
134void Twine::dump() const {
135 print(llvm::errs());
136}
137
138void Twine::dumpRepr() const {
139 printRepr(llvm::errs());
140}