blob: 75cea2961a9dc68d8faccbbd4a6e99681c69283f [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"
Daniel Dunbarb7be0e82009-08-19 18:09:47 +000011#include "llvm/ADT/SmallString.h"
David Greene2b965b02010-01-05 01:28:40 +000012#include "llvm/Support/Debug.h"
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000013#include "llvm/Support/raw_ostream.h"
14using namespace llvm;
15
16std::string Twine::str() const {
Daniel Dunbarb7be0e82009-08-19 18:09:47 +000017 SmallString<256> Vec;
Benjamin Kramerb357e062010-01-13 12:45:23 +000018 return toStringRef(Vec).str();
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000019}
20
21void Twine::toVector(SmallVectorImpl<char> &Out) const {
22 raw_svector_ostream OS(Out);
23 print(OS);
24}
25
Benjamin Kramerb357e062010-01-13 12:45:23 +000026StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
27 if (isSingleStringRef())
28 return getSingleStringRef();
29 toVector(Out);
30 return StringRef(Out.data(), Out.size());
31}
32
Michael J. Spencer7dc7ac32010-12-01 20:37:30 +000033StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
Michael J. Spencer0dda5432010-12-03 05:42:25 +000034 if (isUnary()) {
35 switch (getLHSKind()) {
36 case CStringKind:
37 // Already null terminated, yay!
38 return StringRef(static_cast<const char*>(LHS));
39 case StdStringKind: {
40 const std::string *str = static_cast<const std::string*>(LHS);
41 return StringRef(str->c_str(), str->size());
42 }
43 default:
44 break;
45 }
Michael J. Spencer7dc7ac32010-12-01 20:37:30 +000046 }
47 toVector(Out);
48 Out.push_back(0);
49 Out.pop_back();
50 return StringRef(Out.data(), Out.size());
51}
52
Michael J. Spencer326990f2010-11-26 04:16:08 +000053void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000054 NodeKind Kind) const {
55 switch (Kind) {
56 case Twine::NullKind: break;
57 case Twine::EmptyKind: break;
Daniel Dunbar763457e2009-07-29 07:08:44 +000058 case Twine::TwineKind:
Michael J. Spencer326990f2010-11-26 04:16:08 +000059 static_cast<const Twine*>(Ptr)->print(OS);
Daniel Dunbar763457e2009-07-29 07:08:44 +000060 break;
Michael J. Spencer326990f2010-11-26 04:16:08 +000061 case Twine::CStringKind:
62 OS << static_cast<const char*>(Ptr);
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000063 break;
64 case Twine::StdStringKind:
Michael J. Spencer326990f2010-11-26 04:16:08 +000065 OS << *static_cast<const std::string*>(Ptr);
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000066 break;
67 case Twine::StringRefKind:
Michael J. Spencer326990f2010-11-26 04:16:08 +000068 OS << *static_cast<const StringRef*>(Ptr);
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000069 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000070 case Twine::DecUIKind:
Chris Lattnerea03e102010-05-05 18:40:33 +000071 OS << (unsigned)(uintptr_t)Ptr;
Daniel Dunbar0165a2c2009-07-30 03:47:15 +000072 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000073 case Twine::DecIKind:
Chris Lattnerea03e102010-05-05 18:40:33 +000074 OS << (int)(intptr_t)Ptr;
Daniel Dunbar0165a2c2009-07-30 03:47:15 +000075 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000076 case Twine::DecULKind:
77 OS << *static_cast<const unsigned long*>(Ptr);
Daniel Dunbar763457e2009-07-29 07:08:44 +000078 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +000079 case Twine::DecLKind:
80 OS << *static_cast<const long*>(Ptr);
81 break;
82 case Twine::DecULLKind:
83 OS << *static_cast<const unsigned long long*>(Ptr);
84 break;
85 case Twine::DecLLKind:
86 OS << *static_cast<const long long*>(Ptr);
Daniel Dunbar763457e2009-07-29 07:08:44 +000087 break;
88 case Twine::UHexKind:
Daniel Dunbar0fffbaf2009-07-30 18:30:19 +000089 OS.write_hex(*static_cast<const uint64_t*>(Ptr));
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000090 break;
91 }
92}
93
Michael J. Spencer326990f2010-11-26 04:16:08 +000094void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000095 NodeKind Kind) const {
96 switch (Kind) {
97 case Twine::NullKind:
98 OS << "null"; break;
99 case Twine::EmptyKind:
100 OS << "empty"; break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +0000101 case Twine::TwineKind:
102 OS << "rope:";
103 static_cast<const Twine*>(Ptr)->printRepr(OS);
104 break;
Daniel Dunbar763457e2009-07-29 07:08:44 +0000105 case Twine::CStringKind:
106 OS << "cstring:\""
107 << static_cast<const char*>(Ptr) << "\"";
108 break;
109 case Twine::StdStringKind:
110 OS << "std::string:\""
111 << static_cast<const std::string*>(Ptr) << "\"";
112 break;
113 case Twine::StringRefKind:
114 OS << "stringref:\""
115 << static_cast<const StringRef*>(Ptr) << "\"";
116 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000117 case Twine::DecUIKind:
Chris Lattnerea03e102010-05-05 18:40:33 +0000118 OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +0000119 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000120 case Twine::DecIKind:
Chris Lattnerea03e102010-05-05 18:40:33 +0000121 OS << "decI:\"" << (int)(intptr_t)Ptr << "\"";
Daniel Dunbar0165a2c2009-07-30 03:47:15 +0000122 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000123 case Twine::DecULKind:
124 OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
Daniel Dunbar0165a2c2009-07-30 03:47:15 +0000125 break;
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000126 case Twine::DecLKind:
127 OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
128 break;
129 case Twine::DecULLKind:
130 OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
131 break;
132 case Twine::DecLLKind:
133 OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +0000134 break;
135 case Twine::UHexKind:
Daniel Dunbar2d8bc0f2009-07-30 21:15:14 +0000136 OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
Daniel Dunbar763457e2009-07-29 07:08:44 +0000137 break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +0000138 }
139}
140
141void Twine::print(raw_ostream &OS) const {
142 printOneChild(OS, LHS, getLHSKind());
143 printOneChild(OS, RHS, getRHSKind());
144}
145
146void Twine::printRepr(raw_ostream &OS) const {
147 OS << "(Twine ";
148 printOneChildRepr(OS, LHS, getLHSKind());
149 OS << " ";
150 printOneChildRepr(OS, RHS, getRHSKind());
151 OS << ")";
152}
153
154void Twine::dump() const {
David Greene2b965b02010-01-05 01:28:40 +0000155 print(llvm::dbgs());
Daniel Dunbar2538f7a2009-07-24 07:04:27 +0000156}
157
158void Twine::dumpRepr() const {
David Greene2b965b02010-01-05 01:28:40 +0000159 printRepr(llvm::dbgs());
Daniel Dunbar2538f7a2009-07-24 07:04:27 +0000160}