LLVM API Documentation

Twine.cpp

Go to the documentation of this file.
00001 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/ADT/Twine.h"
00011 #include "llvm/ADT/SmallString.h"
00012 #include "llvm/Support/raw_ostream.h"
00013 using namespace llvm;
00014 
00015 std::string Twine::str() const {
00016   SmallString<256> Vec;
00017   toVector(Vec);
00018   return std::string(Vec.begin(), Vec.end());
00019 }
00020 
00021 void Twine::toVector(SmallVectorImpl<char> &Out) const {
00022   raw_svector_ostream OS(Out);
00023   print(OS);
00024 }
00025 
00026 void Twine::printOneChild(raw_ostream &OS, const void *Ptr, 
00027                           NodeKind Kind) const {
00028   switch (Kind) {
00029   case Twine::NullKind: break;
00030   case Twine::EmptyKind: break;
00031   case Twine::TwineKind:
00032     static_cast<const Twine*>(Ptr)->print(OS); 
00033     break;
00034   case Twine::CStringKind: 
00035     OS << static_cast<const char*>(Ptr); 
00036     break;
00037   case Twine::StdStringKind:
00038     OS << *static_cast<const std::string*>(Ptr); 
00039     break;
00040   case Twine::StringRefKind:
00041     OS << *static_cast<const StringRef*>(Ptr); 
00042     break;
00043   case Twine::DecUIKind:
00044     OS << *static_cast<const unsigned int*>(Ptr);
00045     break;
00046   case Twine::DecIKind:
00047     OS << *static_cast<const int*>(Ptr);
00048     break;
00049   case Twine::DecULKind:
00050     OS << *static_cast<const unsigned long*>(Ptr);
00051     break;
00052   case Twine::DecLKind:
00053     OS << *static_cast<const long*>(Ptr);
00054     break;
00055   case Twine::DecULLKind:
00056     OS << *static_cast<const unsigned long long*>(Ptr);
00057     break;
00058   case Twine::DecLLKind:
00059     OS << *static_cast<const long long*>(Ptr);
00060     break;
00061   case Twine::UHexKind:
00062     OS.write_hex(*static_cast<const uint64_t*>(Ptr));
00063     break;
00064   }
00065 }
00066 
00067 void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr, 
00068                               NodeKind Kind) const {
00069   switch (Kind) {
00070   case Twine::NullKind:
00071     OS << "null"; break;
00072   case Twine::EmptyKind:
00073     OS << "empty"; break;
00074   case Twine::TwineKind:
00075     OS << "rope:";
00076     static_cast<const Twine*>(Ptr)->printRepr(OS);
00077     break;
00078   case Twine::CStringKind:
00079     OS << "cstring:\""
00080        << static_cast<const char*>(Ptr) << "\"";
00081     break;
00082   case Twine::StdStringKind:
00083     OS << "std::string:\""
00084        << static_cast<const std::string*>(Ptr) << "\"";
00085     break;
00086   case Twine::StringRefKind:
00087     OS << "stringref:\""
00088        << static_cast<const StringRef*>(Ptr) << "\"";
00089     break;
00090   case Twine::DecUIKind:
00091     OS << "decUI:\"" << *static_cast<const unsigned int*>(Ptr) << "\"";
00092     break;
00093   case Twine::DecIKind:
00094     OS << "decI:\"" << *static_cast<const int*>(Ptr) << "\"";
00095     break;
00096   case Twine::DecULKind:
00097     OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
00098     break;
00099   case Twine::DecLKind:
00100     OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
00101     break;
00102   case Twine::DecULLKind:
00103     OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
00104     break;
00105   case Twine::DecLLKind:
00106     OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
00107     break;
00108   case Twine::UHexKind:
00109     OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
00110     break;
00111   }
00112 }
00113 
00114 void Twine::print(raw_ostream &OS) const {
00115   printOneChild(OS, LHS, getLHSKind());
00116   printOneChild(OS, RHS, getRHSKind());
00117 }
00118 
00119 void Twine::printRepr(raw_ostream &OS) const {
00120   OS << "(Twine ";
00121   printOneChildRepr(OS, LHS, getLHSKind());
00122   OS << " ";
00123   printOneChildRepr(OS, RHS, getRHSKind());
00124   OS << ")";
00125 }
00126 
00127 void Twine::dump() const {
00128   print(llvm::errs());
00129 }
00130 
00131 void Twine::dumpRepr() const {
00132   printRepr(llvm::errs());
00133 }