blob: 83a3a6180f3f6e578860437131f434551018d501 [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 Dunbar0165a2c2009-07-30 03:47:15 +000050 case Twine::UDec32Kind:
51 OS << *static_cast<const uint32_t*>(Ptr);
52 break;
53 case Twine::SDec32Kind:
54 OS << *static_cast<const int32_t*>(Ptr);
55 break;
56 case Twine::UDec64Kind:
Daniel Dunbar763457e2009-07-29 07:08:44 +000057 OS << *static_cast<const uint64_t*>(Ptr);
58 break;
Daniel Dunbar0165a2c2009-07-30 03:47:15 +000059 case Twine::SDec64Kind:
Daniel Dunbar763457e2009-07-29 07:08:44 +000060 OS << *static_cast<const int64_t*>(Ptr);
61 break;
62 case Twine::UHexKind:
Daniel Dunbar0fffbaf2009-07-30 18:30:19 +000063 OS.write_hex(*static_cast<const uint64_t*>(Ptr));
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000064 break;
65 }
66}
67
68void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
69 NodeKind Kind) const {
70 switch (Kind) {
71 case Twine::NullKind:
72 OS << "null"; break;
73 case Twine::EmptyKind:
74 OS << "empty"; break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000075 case Twine::TwineKind:
76 OS << "rope:";
77 static_cast<const Twine*>(Ptr)->printRepr(OS);
78 break;
Daniel Dunbar763457e2009-07-29 07:08:44 +000079 case Twine::CStringKind:
80 OS << "cstring:\""
81 << static_cast<const char*>(Ptr) << "\"";
82 break;
83 case Twine::StdStringKind:
84 OS << "std::string:\""
85 << static_cast<const std::string*>(Ptr) << "\"";
86 break;
87 case Twine::StringRefKind:
88 OS << "stringref:\""
89 << static_cast<const StringRef*>(Ptr) << "\"";
90 break;
Daniel Dunbar0165a2c2009-07-30 03:47:15 +000091 case Twine::UDec32Kind:
92 OS << "udec32:" << static_cast<const uint64_t*>(Ptr) << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +000093 break;
Daniel Dunbar0165a2c2009-07-30 03:47:15 +000094 case Twine::SDec32Kind:
95 OS << "sdec32:" << static_cast<const int64_t*>(Ptr) << "\"";
96 break;
97 case Twine::UDec64Kind:
98 OS << "udec64:" << static_cast<const uint64_t*>(Ptr) << "\"";
99 break;
100 case Twine::SDec64Kind:
101 OS << "sdec64:" << static_cast<const int64_t*>(Ptr) << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +0000102 break;
103 case Twine::UHexKind:
104 OS << "uhex:" << static_cast<const uint64_t*>(Ptr) << "\"";
105 break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +0000106 }
107}
108
109void Twine::print(raw_ostream &OS) const {
110 printOneChild(OS, LHS, getLHSKind());
111 printOneChild(OS, RHS, getRHSKind());
112}
113
114void Twine::printRepr(raw_ostream &OS) const {
115 OS << "(Twine ";
116 printOneChildRepr(OS, LHS, getLHSKind());
117 OS << " ";
118 printOneChildRepr(OS, RHS, getRHSKind());
119 OS << ")";
120}
121
122void Twine::dump() const {
123 print(llvm::errs());
124}
125
126void Twine::dumpRepr() const {
127 printRepr(llvm::errs());
128}