LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | List of all members
llvm::Twine Class Reference

#include <Twine.h>

Public Member Functions

Predicate Operations
bool isTriviallyEmpty () const
 
bool isSingleStringRef () const
 
String Operations
Twine concat (const Twine &Suffix) const
 
Output & Conversion.
std::string str () const
 str - Return the twine contents as a std::string. More...
 
void toVector (SmallVectorImpl< char > &Out) const
 
StringRef getSingleStringRef () const
 
StringRef toStringRef (SmallVectorImpl< char > &Out) const
 
StringRef toNullTerminatedStringRef (SmallVectorImpl< char > &Out) const
 
void print (raw_ostream &OS) const
 
void dump () const
 Dump the concatenated string represented by this twine to stderr. More...
 
void printRepr (raw_ostream &OS) const
 Write the representation of this twine to the stream OS. More...
 
void dumpRepr () const
 Dump the representation of this twine to stderr. More...
 

Static Public Member Functions

Numeric Conversions
static Twine utohexstr (const uint64_t &Val)
 

Constructors

 Twine ()
 Construct from an empty string. More...
 
 Twine (const char *Str)
 
 Twine (const std::string &Str)
 Construct from an std::string. More...
 
 Twine (const StringRef &Str)
 Construct from a StringRef. More...
 
 Twine (char Val)
 Construct from a char. More...
 
 Twine (signed char Val)
 Construct from a signed char. More...
 
 Twine (unsigned char Val)
 Construct from an unsigned char. More...
 
 Twine (unsigned Val)
 Construct a twine to print Val as an unsigned decimal integer. More...
 
 Twine (int Val)
 Construct a twine to print Val as a signed decimal integer. More...
 
 Twine (const unsigned long &Val)
 Construct a twine to print Val as an unsigned decimal integer. More...
 
 Twine (const long &Val)
 Construct a twine to print Val as a signed decimal integer. More...
 
 Twine (const unsigned long long &Val)
 Construct a twine to print Val as an unsigned decimal integer. More...
 
 Twine (const long long &Val)
 Construct a twine to print Val as a signed decimal integer. More...
 
 Twine (const char *_LHS, const StringRef &_RHS)
 Construct as the concatenation of a C string and a StringRef. More...
 
 Twine (const StringRef &_LHS, const char *_RHS)
 Construct as the concatenation of a StringRef and a C string. More...
 
static Twine createNull ()
 

Detailed Description

Twine - A lightweight data structure for efficiently representing the concatenation of temporary values as strings.

A Twine is a kind of rope, it represents a concatenated string using a binary-tree, where the string is the preorder of the nodes. Since the Twine can be efficiently rendered into a buffer when its result is used, it avoids the cost of generating temporary values for intermediate string results – particularly in cases when the Twine result is never required. By explicitly tracking the type of leaf nodes, we can also avoid the creation of temporary strings for conversions operations (such as appending an integer to a string).

A Twine is not intended for use directly and should not be stored, its implementation relies on the ability to store pointers to temporary stack objects which may be deallocated at the end of a statement. Twines should only be used accepted as const references in arguments, when an API wishes to accept possibly-concatenated strings.

Twines support a special 'null' value, which always concatenates to form itself, and renders as an empty string. This can be returned from APIs to effectively nullify any concatenations performed on the result.

Implementation

Given the nature of a Twine, it is not possible for the Twine's concatenation method to construct interior nodes; the result must be represented inside the returned value. For this reason a Twine object actually holds two values, the left- and right-hand sides of a concatenation. We also have nullary Twine objects, which are effectively sentinel values that represent empty strings.

Thus, a Twine can effectively have zero, one, or two children. The

See Also
isNullary(),
isUnary(), and
isBinary() predicates exist for testing the number of children.

We maintain a number of invariants on Twine objects (FIXME: Why):

These invariants are check by

See Also
isValid().

Efficiency Considerations

The Twine is designed to yield efficient and small code for common situations. For this reason, the concat() method is inlined so that concatenations of leaf nodes can be optimized into stores directly into a single stack allocated object.

In practice, not all compilers can be trusted to optimize concat() fully, so we provide two additional methods (and accompanying operator+ overloads) to guarantee that particularly important cases (cstring plus StringRef) codegen as desired.

Definition at line 81 of file Twine.h.

Constructor & Destructor Documentation

llvm::Twine::Twine ( )
inline

Construct from an empty string.

Definition at line 254 of file Twine.h.

Referenced by concat(), createNull(), and utohexstr().

llvm::Twine::Twine ( const char *  Str)
inline

Construct from a C string.

We take care here to optimize "" into the empty twine – this will be optimized out for string constants. This allows Twine arguments have default "" values, without introducing unnecessary string constants.

Definition at line 263 of file Twine.h.

llvm::Twine::Twine ( const std::string &  Str)
inline

Construct from an std::string.

Definition at line 275 of file Twine.h.

llvm::Twine::Twine ( const StringRef Str)
inline

Construct from a StringRef.

Definition at line 282 of file Twine.h.

llvm::Twine::Twine ( char  Val)
inlineexplicit

Construct from a char.

Definition at line 289 of file Twine.h.

llvm::Twine::Twine ( signed char  Val)
inlineexplicit

Construct from a signed char.

Definition at line 295 of file Twine.h.

llvm::Twine::Twine ( unsigned char  Val)
inlineexplicit

Construct from an unsigned char.

Definition at line 301 of file Twine.h.

llvm::Twine::Twine ( unsigned  Val)
inlineexplicit

Construct a twine to print Val as an unsigned decimal integer.

Definition at line 307 of file Twine.h.

llvm::Twine::Twine ( int  Val)
inlineexplicit

Construct a twine to print Val as a signed decimal integer.

Definition at line 313 of file Twine.h.

llvm::Twine::Twine ( const unsigned long &  Val)
inlineexplicit

Construct a twine to print Val as an unsigned decimal integer.

Definition at line 319 of file Twine.h.

llvm::Twine::Twine ( const long &  Val)
inlineexplicit

Construct a twine to print Val as a signed decimal integer.

Definition at line 325 of file Twine.h.

llvm::Twine::Twine ( const unsigned long long &  Val)
inlineexplicit

Construct a twine to print Val as an unsigned decimal integer.

Definition at line 331 of file Twine.h.

llvm::Twine::Twine ( const long long &  Val)
inlineexplicit

Construct a twine to print Val as a signed decimal integer.

Definition at line 337 of file Twine.h.

llvm::Twine::Twine ( const char *  _LHS,
const StringRef _RHS 
)
inline

Construct as the concatenation of a C string and a StringRef.

Definition at line 348 of file Twine.h.

llvm::Twine::Twine ( const StringRef _LHS,
const char *  _RHS 
)
inline

Construct as the concatenation of a StringRef and a C string.

Definition at line 356 of file Twine.h.

Member Function Documentation

Twine llvm::Twine::concat ( const Twine Suffix) const
inline

Definition at line 469 of file Twine.h.

References Twine().

Referenced by llvm::operator+().

static Twine llvm::Twine::createNull ( )
inlinestatic

Create a 'null' string, which is an empty string that always concatenates to form another empty string.

Definition at line 365 of file Twine.h.

References Twine().

void Twine::dump ( ) const

Dump the concatenated string represented by this twine to stderr.

Definition at line 165 of file Twine.cpp.

References llvm::dbgs(), and print().

void Twine::dumpRepr ( ) const

Dump the representation of this twine to stderr.

Definition at line 169 of file Twine.cpp.

References llvm::dbgs(), and printRepr().

StringRef llvm::Twine::getSingleStringRef ( ) const
inline

getSingleStringRef - This returns the twine as a single StringRef. This method is only valid if isSingleStringRef() is true.

Definition at line 426 of file Twine.h.

References isSingleStringRef(), and llvm_unreachable.

Referenced by llvm::sys::path::native(), and toStringRef().

bool llvm::Twine::isSingleStringRef ( ) const
inline

isSingleStringRef - Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStringRef().

Definition at line 393 of file Twine.h.

Referenced by getSingleStringRef(), llvm::sys::path::native(), and toStringRef().

bool llvm::Twine::isTriviallyEmpty ( ) const
inline

isTriviallyEmpty - Check if this twine is trivially empty; a false return value does not necessarily mean the twine is empty.

Definition at line 387 of file Twine.h.

Referenced by llvm::sys::path::append(), and llvm::Value::setName().

void Twine::print ( raw_ostream OS) const

Write the concatenated string represented by this twine to the stream OS.

Definition at line 152 of file Twine.cpp.

Referenced by dump(), llvm::operator<<(), and toVector().

void Twine::printRepr ( raw_ostream OS) const

Write the representation of this twine to the stream OS.

Definition at line 157 of file Twine.cpp.

Referenced by dumpRepr().

std::string Twine::str ( ) const
StringRef Twine::toNullTerminatedStringRef ( SmallVectorImpl< char > &  Out) const

toNullTerminatedStringRef - This returns the twine as a single null terminated StringRef if it can be represented as such. Otherwise the twine is written into the given SmallVector and a StringRef to the SmallVector's data is returned.

The returned StringRef's size does not include the null terminator.

Definition at line 38 of file Twine.cpp.

References llvm::SmallVectorTemplateCommon< T, typename >::data(), llvm::SmallVectorTemplateBase< T, isPodLike >::pop_back(), llvm::SmallVectorTemplateBase< T, isPodLike >::push_back(), llvm::SmallVectorTemplateCommon< T, typename >::size(), str(), and toVector().

Referenced by llvm::sys::fs::createTemporaryFile(), and llvm::MemoryBuffer::getFile().

StringRef Twine::toStringRef ( SmallVectorImpl< char > &  Out) const
void Twine::toVector ( SmallVectorImpl< char > &  Out) const
static Twine llvm::Twine::utohexstr ( const uint64_t &  Val)
inlinestatic

Definition at line 374 of file Twine.h.

References Twine().

Referenced by llvm::DwarfDebug::emitDIE().


The documentation for this class was generated from the following files: