X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FTwine.cpp;h=3d04bc34f0eb8d63ad023af7bcbb5a3e4d38d8ff;hb=adf01b3f18442ae8db6b8948e70d82d9df415119;hp=83a3a6180f3f6e578860437131f434551018d501;hpb=0fffbafa9609e0e289ff3120ab9e23d244c1dbc0;p=oota-llvm.git diff --git a/lib/Support/Twine.cpp b/lib/Support/Twine.cpp index 83a3a6180f3..3d04bc34f0e 100644 --- a/lib/Support/Twine.cpp +++ b/lib/Support/Twine.cpp @@ -8,64 +8,98 @@ //===----------------------------------------------------------------------===// #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 { - // FIXME: This is very inefficient, since we are creating a large raw_ostream - // buffer -- hitting malloc, which we were supposed to avoid -- all when we - // have this pretty little small vector available. - // - // The best way to fix this is to make raw_svector_ostream do the right thing - // and be efficient, by augmenting the base raw_ostream with the ability to - // have the buffer managed by a concrete implementation. raw_svector_ostream OS(Out); 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::TwineKind: - static_cast(Ptr)->print(OS); + Ptr.twine->print(OS); break; - case Twine::CStringKind: - OS << static_cast(Ptr); + 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::CharKind: + OS << Ptr.character; + break; + case Twine::DecUIKind: + OS << Ptr.decUI; + break; + case Twine::DecIKind: + OS << Ptr.decI; break; - case Twine::UDec32Kind: - OS << *static_cast(Ptr); + case Twine::DecULKind: + OS << *Ptr.decUL; break; - case Twine::SDec32Kind: - OS << *static_cast(Ptr); + case Twine::DecLKind: + OS << *Ptr.decL; break; - case Twine::UDec64Kind: - OS << *static_cast(Ptr); + case Twine::DecULLKind: + OS << *Ptr.decULL; break; - case Twine::SDec64Kind: - OS << *static_cast(Ptr); + case Twine::DecLLKind: + OS << *Ptr.decLL; break; case Twine::UHexKind: - OS.write_hex(*static_cast(Ptr)); + 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: @@ -74,34 +108,43 @@ void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr, OS << "empty"; break; case Twine::TwineKind: OS << "rope:"; - static_cast(Ptr)->printRepr(OS); + Ptr.twine->printRepr(OS); break; case Twine::CStringKind: OS << "cstring:\"" - << static_cast(Ptr) << "\""; + << Ptr.cString << "\""; break; case Twine::StdStringKind: OS << "std::string:\"" - << static_cast(Ptr) << "\""; + << Ptr.stdString << "\""; break; case Twine::StringRefKind: OS << "stringref:\"" - << static_cast(Ptr) << "\""; + << Ptr.stringRef << "\""; + break; + 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::UDec32Kind: - OS << "udec32:" << static_cast(Ptr) << "\""; + case Twine::DecULKind: + OS << "decUL:\"" << *Ptr.decUL << "\""; break; - case Twine::SDec32Kind: - OS << "sdec32:" << static_cast(Ptr) << "\""; + case Twine::DecLKind: + OS << "decL:\"" << *Ptr.decL << "\""; break; - case Twine::UDec64Kind: - OS << "udec64:" << static_cast(Ptr) << "\""; + case Twine::DecULLKind: + OS << "decULL:\"" << *Ptr.decULL << "\""; break; - case Twine::SDec64Kind: - OS << "sdec64:" << static_cast(Ptr) << "\""; + case Twine::DecLLKind: + OS << "decLL:\"" << *Ptr.decLL << "\""; break; case Twine::UHexKind: - OS << "uhex:" << static_cast(Ptr) << "\""; + OS << "uhex:\"" << Ptr.uHex << "\""; break; } } @@ -120,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()); }