X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FTwine.cpp;h=3d04bc34f0eb8d63ad023af7bcbb5a3e4d38d8ff;hb=54e57f8cb79bdc23ed8289cf2a558fa7c9602972;hp=4c34d279e8fdd1a6be194d53f57db081e45d2f18;hpb=2538f7ab2ef39ab1a5e48744548d66b560d1fee6;p=oota-llvm.git diff --git a/lib/Support/Twine.cpp b/lib/Support/Twine.cpp index 4c34d279e8f..3d04bc34f0e 100644 --- a/lib/Support/Twine.cpp +++ b/lib/Support/Twine.cpp @@ -8,14 +8,19 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/Twine.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; std::string Twine::str() const { - std::string Res; - raw_string_ostream OS(Res); - print(OS); - return Res; + // If we're storing only a std::string, just return it. + if (LHSKind == StdStringKind && RHSKind == EmptyKind) + return *LHS.stdString; + + // Otherwise, flatten and copy the contents first. + SmallString<256> Vec; + return toStringRef(Vec).str(); } void Twine::toVector(SmallVectorImpl &Out) const { @@ -23,48 +28,123 @@ void Twine::toVector(SmallVectorImpl &Out) const { print(OS); } -void Twine::printOneChild(raw_ostream &OS, const void *Ptr, +StringRef Twine::toStringRef(SmallVectorImpl &Out) const { + if (isSingleStringRef()) + return getSingleStringRef(); + toVector(Out); + return StringRef(Out.data(), Out.size()); +} + +StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl &Out) const { + if (isUnary()) { + switch (getLHSKind()) { + case CStringKind: + // Already null terminated, yay! + return StringRef(LHS.cString); + case StdStringKind: { + const std::string *str = LHS.stdString; + return StringRef(str->c_str(), str->size()); + } + default: + break; + } + } + toVector(Out); + Out.push_back(0); + Out.pop_back(); + return StringRef(Out.data(), Out.size()); +} + +void Twine::printOneChild(raw_ostream &OS, Child Ptr, NodeKind Kind) const { switch (Kind) { case Twine::NullKind: break; case Twine::EmptyKind: break; - case Twine::CStringKind: - OS << static_cast(Ptr); + case Twine::TwineKind: + Ptr.twine->print(OS); + break; + case Twine::CStringKind: + OS << Ptr.cString; break; case Twine::StdStringKind: - OS << *static_cast(Ptr); + OS << *Ptr.stdString; break; case Twine::StringRefKind: - OS << *static_cast(Ptr); + OS << *Ptr.stringRef; break; - case Twine::TwineKind: - static_cast(Ptr)->print(OS); + case Twine::CharKind: + OS << Ptr.character; + break; + case Twine::DecUIKind: + OS << Ptr.decUI; + break; + case Twine::DecIKind: + OS << Ptr.decI; + break; + case Twine::DecULKind: + OS << *Ptr.decUL; + break; + case Twine::DecLKind: + OS << *Ptr.decL; + break; + case Twine::DecULLKind: + OS << *Ptr.decULL; + break; + case Twine::DecLLKind: + OS << *Ptr.decLL; + break; + case Twine::UHexKind: + OS.write_hex(*Ptr.uHex); break; } } -void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr, +void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr, NodeKind Kind) const { switch (Kind) { case Twine::NullKind: OS << "null"; break; case Twine::EmptyKind: OS << "empty"; break; + case Twine::TwineKind: + OS << "rope:"; + Ptr.twine->printRepr(OS); + break; case Twine::CStringKind: - OS << "cstring:\"" - << static_cast(Ptr) << "\""; + OS << "cstring:\"" + << Ptr.cString << "\""; break; case Twine::StdStringKind: - OS << "std::string:\"" - << *static_cast(Ptr) << "\""; + OS << "std::string:\"" + << Ptr.stdString << "\""; break; case Twine::StringRefKind: - OS << "stringref:\"" - << *static_cast(Ptr) << "\""; + OS << "stringref:\"" + << Ptr.stringRef << "\""; break; - case Twine::TwineKind: - OS << "rope:"; - static_cast(Ptr)->printRepr(OS); + case Twine::CharKind: + OS << "char:\"" << Ptr.character << "\""; + break; + case Twine::DecUIKind: + OS << "decUI:\"" << Ptr.decUI << "\""; + break; + case Twine::DecIKind: + OS << "decI:\"" << Ptr.decI << "\""; + break; + case Twine::DecULKind: + OS << "decUL:\"" << *Ptr.decUL << "\""; + break; + case Twine::DecLKind: + OS << "decL:\"" << *Ptr.decL << "\""; + break; + case Twine::DecULLKind: + OS << "decULL:\"" << *Ptr.decULL << "\""; + break; + case Twine::DecLLKind: + OS << "decLL:\"" << *Ptr.decLL << "\""; + break; + case Twine::UHexKind: + OS << "uhex:\"" << Ptr.uHex << "\""; break; } } @@ -83,9 +163,9 @@ void Twine::printRepr(raw_ostream &OS) const { } void Twine::dump() const { - print(llvm::errs()); + print(llvm::dbgs()); } void Twine::dumpRepr() const { - printRepr(llvm::errs()); + printRepr(llvm::dbgs()); }