Twine.cpp revision 2d8bc0fe70c55664b89605dbfa5c2f591446469c
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" 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 { 22 // 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. 29 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; 38 case Twine::TwineKind: 39 static_cast<const Twine*>(Ptr)->print(OS); 40 break; 41 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; 50 case Twine::DecUIKind: 51 OS << *static_cast<const unsigned int*>(Ptr); 52 break; 53 case Twine::DecIKind: 54 OS << *static_cast<const int*>(Ptr); 55 break; 56 case Twine::DecULKind: 57 OS << *static_cast<const unsigned long*>(Ptr); 58 break; 59 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); 67 break; 68 case Twine::UHexKind: 69 OS.write_hex(*static_cast<const uint64_t*>(Ptr)); 70 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; 81 case Twine::TwineKind: 82 OS << "rope:"; 83 static_cast<const Twine*>(Ptr)->printRepr(OS); 84 break; 85 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; 97 case Twine::DecUIKind: 98 OS << "decUI:\"" << *static_cast<const unsigned int*>(Ptr) << "\""; 99 break; 100 case Twine::DecIKind: 101 OS << "decI:\"" << *static_cast<const int*>(Ptr) << "\""; 102 break; 103 case Twine::DecULKind: 104 OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\""; 105 break; 106 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) << "\""; 114 break; 115 case Twine::UHexKind: 116 OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\""; 117 break; 118 } 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} 141