Add Twine support for characters, and switch twine to use a union internally

to eliminate some casting.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135888 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2011-07-24 20:44:30 +00:00
parent 81d686edbe
commit 3f25ee080c
3 changed files with 122 additions and 54 deletions
+33 -27
View File
@@ -16,7 +16,7 @@ using namespace llvm;
std::string Twine::str() const {
// If we're storing only a std::string, just return it.
if (LHSKind == StdStringKind && RHSKind == EmptyKind)
return *static_cast<const std::string*>(LHS);
return *LHS.stdString;
// Otherwise, flatten and copy the contents first.
SmallString<256> Vec;
@@ -40,9 +40,9 @@ StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
switch (getLHSKind()) {
case CStringKind:
// Already null terminated, yay!
return StringRef(static_cast<const char*>(LHS));
return StringRef(LHS.cString);
case StdStringKind: {
const std::string *str = static_cast<const std::string*>(LHS);
const std::string *str = LHS.stdString;
return StringRef(str->c_str(), str->size());
}
default:
@@ -55,48 +55,51 @@ StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
return StringRef(Out.data(), Out.size());
}
void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
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<const Twine*>(Ptr)->print(OS);
Ptr.twine->print(OS);
break;
case Twine::CStringKind:
OS << static_cast<const char*>(Ptr);
OS << Ptr.cString;
break;
case Twine::StdStringKind:
OS << *static_cast<const std::string*>(Ptr);
OS << *Ptr.stdString;
break;
case Twine::StringRefKind:
OS << *static_cast<const StringRef*>(Ptr);
OS << *Ptr.stringRef;
break;
case Twine::CharKind:
OS << Ptr.character;
break;
case Twine::DecUIKind:
OS << (unsigned)(uintptr_t)Ptr;
OS << Ptr.decUI;
break;
case Twine::DecIKind:
OS << (int)(intptr_t)Ptr;
OS << Ptr.decI;
break;
case Twine::DecULKind:
OS << *static_cast<const unsigned long*>(Ptr);
OS << *Ptr.decUL;
break;
case Twine::DecLKind:
OS << *static_cast<const long*>(Ptr);
OS << *Ptr.decL;
break;
case Twine::DecULLKind:
OS << *static_cast<const unsigned long long*>(Ptr);
OS << *Ptr.decULL;
break;
case Twine::DecLLKind:
OS << *static_cast<const long long*>(Ptr);
OS << *Ptr.decLL;
break;
case Twine::UHexKind:
OS.write_hex(*static_cast<const uint64_t*>(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:
@@ -105,40 +108,43 @@ void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
OS << "empty"; break;
case Twine::TwineKind:
OS << "rope:";
static_cast<const Twine*>(Ptr)->printRepr(OS);
Ptr.twine->printRepr(OS);
break;
case Twine::CStringKind:
OS << "cstring:\""
<< static_cast<const char*>(Ptr) << "\"";
<< Ptr.cString << "\"";
break;
case Twine::StdStringKind:
OS << "std::string:\""
<< static_cast<const std::string*>(Ptr) << "\"";
<< Ptr.stdString << "\"";
break;
case Twine::StringRefKind:
OS << "stringref:\""
<< static_cast<const StringRef*>(Ptr) << "\"";
<< Ptr.stringRef << "\"";
break;
case Twine::CharKind:
OS << "char:\"" << Ptr.character << "\"";
break;
case Twine::DecUIKind:
OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\"";
OS << "decUI:\"" << Ptr.decUI << "\"";
break;
case Twine::DecIKind:
OS << "decI:\"" << (int)(intptr_t)Ptr << "\"";
OS << "decI:\"" << Ptr.decI << "\"";
break;
case Twine::DecULKind:
OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
OS << "decUL:\"" << *Ptr.decUL << "\"";
break;
case Twine::DecLKind:
OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
OS << "decL:\"" << *Ptr.decL << "\"";
break;
case Twine::DecULLKind:
OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
OS << "decULL:\"" << *Ptr.decULL << "\"";
break;
case Twine::DecLLKind:
OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
OS << "decLL:\"" << *Ptr.decLL << "\"";
break;
case Twine::UHexKind:
OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
OS << "uhex:\"" << Ptr.uHex << "\"";
break;
}
}