2009-07-24 07:04:27 +00:00
|
|
|
//===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/Twine.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Twine::toVector(SmallVectorImpl<char> &Out) const {
|
2009-07-29 07:08:44 +00:00
|
|
|
// 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.
|
2009-07-24 07:04:27 +00:00
|
|
|
raw_svector_ostream OS(Out);
|
|
|
|
print(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
|
|
|
|
NodeKind Kind) const {
|
|
|
|
switch (Kind) {
|
|
|
|
case Twine::NullKind: break;
|
|
|
|
case Twine::EmptyKind: break;
|
2009-07-29 07:08:44 +00:00
|
|
|
case Twine::TwineKind:
|
|
|
|
static_cast<const Twine*>(Ptr)->print(OS);
|
|
|
|
break;
|
2009-07-24 07:04:27 +00:00
|
|
|
case Twine::CStringKind:
|
|
|
|
OS << static_cast<const char*>(Ptr);
|
|
|
|
break;
|
|
|
|
case Twine::StdStringKind:
|
|
|
|
OS << *static_cast<const std::string*>(Ptr);
|
|
|
|
break;
|
|
|
|
case Twine::StringRefKind:
|
|
|
|
OS << *static_cast<const StringRef*>(Ptr);
|
|
|
|
break;
|
2009-07-30 03:47:15 +00:00
|
|
|
case Twine::UDec32Kind:
|
|
|
|
OS << *static_cast<const uint32_t*>(Ptr);
|
|
|
|
break;
|
|
|
|
case Twine::SDec32Kind:
|
|
|
|
OS << *static_cast<const int32_t*>(Ptr);
|
|
|
|
break;
|
|
|
|
case Twine::UDec64Kind:
|
2009-07-29 07:08:44 +00:00
|
|
|
OS << *static_cast<const uint64_t*>(Ptr);
|
|
|
|
break;
|
2009-07-30 03:47:15 +00:00
|
|
|
case Twine::SDec64Kind:
|
2009-07-29 07:08:44 +00:00
|
|
|
OS << *static_cast<const int64_t*>(Ptr);
|
|
|
|
break;
|
|
|
|
case Twine::UHexKind:
|
|
|
|
// FIXME: Add raw_ostream functionality for this.
|
|
|
|
OS << ::utohexstr(*static_cast<const uint64_t*>(Ptr));
|
2009-07-24 07:04:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
|
|
|
|
NodeKind Kind) const {
|
|
|
|
switch (Kind) {
|
|
|
|
case Twine::NullKind:
|
|
|
|
OS << "null"; break;
|
|
|
|
case Twine::EmptyKind:
|
|
|
|
OS << "empty"; break;
|
2009-07-29 07:08:44 +00:00
|
|
|
case Twine::TwineKind:
|
|
|
|
OS << "rope:";
|
|
|
|
static_cast<const Twine*>(Ptr)->printRepr(OS);
|
|
|
|
break;
|
2009-07-24 07:04:27 +00:00
|
|
|
case Twine::CStringKind:
|
2009-07-29 07:08:44 +00:00
|
|
|
OS << "cstring:\""
|
|
|
|
<< static_cast<const char*>(Ptr) << "\"";
|
2009-07-24 07:04:27 +00:00
|
|
|
break;
|
|
|
|
case Twine::StdStringKind:
|
2009-07-29 07:08:44 +00:00
|
|
|
OS << "std::string:\""
|
|
|
|
<< static_cast<const std::string*>(Ptr) << "\"";
|
2009-07-24 07:04:27 +00:00
|
|
|
break;
|
|
|
|
case Twine::StringRefKind:
|
2009-07-29 07:08:44 +00:00
|
|
|
OS << "stringref:\""
|
|
|
|
<< static_cast<const StringRef*>(Ptr) << "\"";
|
2009-07-24 07:04:27 +00:00
|
|
|
break;
|
2009-07-30 03:47:15 +00:00
|
|
|
case Twine::UDec32Kind:
|
|
|
|
OS << "udec32:" << static_cast<const uint64_t*>(Ptr) << "\"";
|
|
|
|
break;
|
|
|
|
case Twine::SDec32Kind:
|
|
|
|
OS << "sdec32:" << static_cast<const int64_t*>(Ptr) << "\"";
|
|
|
|
break;
|
|
|
|
case Twine::UDec64Kind:
|
|
|
|
OS << "udec64:" << static_cast<const uint64_t*>(Ptr) << "\"";
|
2009-07-29 07:08:44 +00:00
|
|
|
break;
|
2009-07-30 03:47:15 +00:00
|
|
|
case Twine::SDec64Kind:
|
|
|
|
OS << "sdec64:" << static_cast<const int64_t*>(Ptr) << "\"";
|
2009-07-29 07:08:44 +00:00
|
|
|
break;
|
|
|
|
case Twine::UHexKind:
|
|
|
|
OS << "uhex:" << static_cast<const uint64_t*>(Ptr) << "\"";
|
2009-07-24 07:04:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Twine::print(raw_ostream &OS) const {
|
|
|
|
printOneChild(OS, LHS, getLHSKind());
|
|
|
|
printOneChild(OS, RHS, getRHSKind());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Twine::printRepr(raw_ostream &OS) const {
|
|
|
|
OS << "(Twine ";
|
|
|
|
printOneChildRepr(OS, LHS, getLHSKind());
|
|
|
|
OS << " ";
|
|
|
|
printOneChildRepr(OS, RHS, getRHSKind());
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Twine::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Twine::dumpRepr() const {
|
|
|
|
printRepr(llvm::errs());
|
|
|
|
}
|