| Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 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" | 
|  | 12 | using namespace llvm; | 
|  | 13 |  | 
|  | 14 | std::string Twine::str() const { | 
| Daniel Dunbar | 2b800df | 2009-08-02 04:12:28 +0000 | [diff] [blame] | 15 | // FIXME: This should probably use the toVector implementation, once that is | 
|  | 16 | // efficient. | 
| Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 17 | std::string Res; | 
|  | 18 | raw_string_ostream OS(Res); | 
|  | 19 | print(OS); | 
| Daniel Dunbar | 2b800df | 2009-08-02 04:12:28 +0000 | [diff] [blame] | 20 | OS.flush(); | 
| Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 21 | return Res; | 
|  | 22 | } | 
|  | 23 |  | 
|  | 24 | void Twine::toVector(SmallVectorImpl<char> &Out) const { | 
| Daniel Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 25 | // 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 Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 32 | raw_svector_ostream OS(Out); | 
|  | 33 | print(OS); | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | void 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 Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 41 | case Twine::TwineKind: | 
|  | 42 | static_cast<const Twine*>(Ptr)->print(OS); | 
|  | 43 | break; | 
| Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 44 | 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 Dunbar | 2d8bc0f | 2009-07-30 21:15:14 +0000 | [diff] [blame] | 53 | case Twine::DecUIKind: | 
|  | 54 | OS << *static_cast<const unsigned int*>(Ptr); | 
| Daniel Dunbar | 0165a2c | 2009-07-30 03:47:15 +0000 | [diff] [blame] | 55 | break; | 
| Daniel Dunbar | 2d8bc0f | 2009-07-30 21:15:14 +0000 | [diff] [blame] | 56 | case Twine::DecIKind: | 
|  | 57 | OS << *static_cast<const int*>(Ptr); | 
| Daniel Dunbar | 0165a2c | 2009-07-30 03:47:15 +0000 | [diff] [blame] | 58 | break; | 
| Daniel Dunbar | 2d8bc0f | 2009-07-30 21:15:14 +0000 | [diff] [blame] | 59 | case Twine::DecULKind: | 
|  | 60 | OS << *static_cast<const unsigned long*>(Ptr); | 
| Daniel Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 61 | break; | 
| Daniel Dunbar | 2d8bc0f | 2009-07-30 21:15:14 +0000 | [diff] [blame] | 62 | 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 Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 70 | break; | 
|  | 71 | case Twine::UHexKind: | 
| Daniel Dunbar | 0fffbaf | 2009-07-30 18:30:19 +0000 | [diff] [blame] | 72 | OS.write_hex(*static_cast<const uint64_t*>(Ptr)); | 
| Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 73 | break; | 
|  | 74 | } | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | void 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 Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 84 | case Twine::TwineKind: | 
|  | 85 | OS << "rope:"; | 
|  | 86 | static_cast<const Twine*>(Ptr)->printRepr(OS); | 
|  | 87 | break; | 
| Daniel Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 88 | 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 Dunbar | 2d8bc0f | 2009-07-30 21:15:14 +0000 | [diff] [blame] | 100 | case Twine::DecUIKind: | 
|  | 101 | OS << "decUI:\"" << *static_cast<const unsigned int*>(Ptr) << "\""; | 
| Daniel Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 102 | break; | 
| Daniel Dunbar | 2d8bc0f | 2009-07-30 21:15:14 +0000 | [diff] [blame] | 103 | case Twine::DecIKind: | 
|  | 104 | OS << "decI:\"" << *static_cast<const int*>(Ptr) << "\""; | 
| Daniel Dunbar | 0165a2c | 2009-07-30 03:47:15 +0000 | [diff] [blame] | 105 | break; | 
| Daniel Dunbar | 2d8bc0f | 2009-07-30 21:15:14 +0000 | [diff] [blame] | 106 | case Twine::DecULKind: | 
|  | 107 | OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\""; | 
| Daniel Dunbar | 0165a2c | 2009-07-30 03:47:15 +0000 | [diff] [blame] | 108 | break; | 
| Daniel Dunbar | 2d8bc0f | 2009-07-30 21:15:14 +0000 | [diff] [blame] | 109 | 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 Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 117 | break; | 
|  | 118 | case Twine::UHexKind: | 
| Daniel Dunbar | 2d8bc0f | 2009-07-30 21:15:14 +0000 | [diff] [blame] | 119 | OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\""; | 
| Daniel Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 120 | break; | 
| Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 121 | } | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | void Twine::print(raw_ostream &OS) const { | 
|  | 125 | printOneChild(OS, LHS, getLHSKind()); | 
|  | 126 | printOneChild(OS, RHS, getRHSKind()); | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | void 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 |  | 
|  | 137 | void Twine::dump() const { | 
|  | 138 | print(llvm::errs()); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | void Twine::dumpRepr() const { | 
|  | 142 | printRepr(llvm::errs()); | 
|  | 143 | } |