llvm::Twine

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

Synopsis

Declared in <llvm/ADT/Twine.h>

class Twine;

Description

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 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

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

  • Nullary twines are always represented with their Kind on the left-hand side, and the Empty kind on the right-hand side.

  • Unary twines are always represented with the value on the left-hand side, and the Empty kind on the right-hand side.

  • If a Twine has another Twine as a child, that child should always be binary (otherwise it could have been folded into the parent).

These invariants are check by

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.

Member Functions

NameDescription
Twine [constructor]Constructors
operator= [deleted]Deleted assignment; Twines are temporary and must not be reassigned.
concat Return a twine that concatenates this twine with Suffix.
dump Dump the concatenated string represented by this twine to stderr.
dumpRepr Dump the representation of this twine to stderr.
getSingleStringRef This returns the twine as a single StringRef. This method is only valid if isSingleStringRef() is true.
isSingleStringLiteral Check if this twine is guaranteed to refer to single string literal.
isSingleStringRef Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStringRef().
isTriviallyEmpty Check if this twine is trivially empty; a false return value does not necessarily mean the twine is empty.
print Write the concatenated string represented by this twine to the stream OS.
printRepr Write the representation of this twine to the stream OS.
str Return the twine contents as a std::string.
toNullTerminatedStringRef Return this twine as a null-terminated StringRef, using Out if needed.
toStringRef Return this twine as a StringRef, using Out as scratch if needed.
toVector Append the concatenated string into the given SmallString or SmallVector.

Static Member Functions

NameDescription
createNull Create a 'null' string, which is an empty string that always concatenates to form another empty string.
utohexstr Construct a twine to print Val as an unsigned hexadecimal integer.

Non-Member Functions

NameDescription
PrintErrorPrint an error diagnostic with message Msg and no source location.
PrintFatalErrorPrint an error with message Msg and then exit the process.
PrintFatalNotePrint a note with message Msg and then exit the process.
PrintNotePrint a note diagnostic with message Msg and no source location.
PrintWarningPrint a warning diagnostic with message Msg and no source location.
createFileErrorCreate a StringError with the specified error code and prepend the file path to it.
createFileErrorConcatenate a source file path and/or name with line number and std::error_code to form an Error object.
createFileErrorDeleted overload: a successful Error cannot be wrapped as a FileError.
createFileErrorConcatenate a source file path and/or name with a std::error_code to form an Error object.
createFileErrorCreate a StringError with the specified error code and prepend the file path to it.
createFileErrorConcatenate a source file path and/or name with an Error. The resulting Error is unchecked.
createFileErrorConcatenate a source file path and/or name with line number and an Error. The resulting Error is unchecked.
createGraphFilenameCreate a temporary file name suitable for writing a DOT graph.
createStringErrorCreate a StringError with an inconvertible error code.
identify_magicGet and identify path's type based on its content.
localCacheCreate a local file system cache which uses the given cache name, temporary file prefix, cache directory and file callback.
needConversionQuery whether a file needs conversion to the UTF-8 codepage.
operator+Concatenate LHS and RHS into a new Twine.
operator+Concatenate a C string and a StringRef with simplified codegen.
operator+Concatenate a StringRef and a C string with simplified codegen.
reportFatalInternalErrorReport a fatal internal error with message reason.
reportFatalUsageErrorReport a fatal usage error with message reason.
report_fatal_errorReport a fatal error from a Twine reason.
set_thread_nameSet the name of the current thread.
to_floatParse T as a double into Num.
to_floatParse T as a long double into Num.
to_floatParse T as a float into Num.
MachO::shouldSkipSymLinkDetermine whether to skip over symlink due to either too many symlink levels or is cyclic.
cas::createOnDiskCASCreate a persistent on-disk CAS at Path.
cgdata::warnReport a codegen-data warning with message Message.
dwarf_linker::isPathAbsoluteOnWindowsOrPosixTrue if Path is absolute under POSIX or Windows path rules.
object::createErrorCreate a parse_failed StringError with message Err.
vfs::convertToOutputErrorReturn Error::success() or use OutputPath to create an OutputError, depending on EC.
vfs::convertToTempFileOutputErrorReturn Error::success() or use TempPath and OutputPath to create a TempFileOutputError, depending on EC.
sys::fs::accessCan the file be accessed?
sys::fs::can_executeCan we execute this file?
sys::fs::can_writeCan we write this file?
sys::fs::copy_fileCopy the contents of From to To.
sys::fs::copy_fileCopy the contents of From to To.
sys::fs::createTemporaryFileCreate an empty temporary file without returning an open descriptor.
sys::fs::createTemporaryFileCreate a file in the system temporary directory.
sys::fs::createUniqueDirectoryCreate a unique directory whose name starts with Prefix.
sys::fs::createUniqueFileCreate a uniquely named empty file without returning an open descriptor.
sys::fs::createUniqueFileCreate a uniquely named file.
sys::fs::createUniquePathCreate a potentially unique file name but does not create it.
sys::fs::create_directoriesCreate all the non-existent directories in path.
sys::fs::create_directoryCreate the directory in path.
sys::fs::create_hard_linkCreate a hard link from from to to, or return an error.
sys::fs::create_linkCreate a link from from to to.
sys::fs::create_symlinkCreate a symbolic link from from to to.
sys::fs::disk_spaceGet disk space usage information.
sys::fs::equivalentReturn whether paths A and B refer to the same entity, ignoring errors.
sys::fs::equivalentDo paths represent the same thing?
sys::fs::existsDoes file exist?
sys::fs::expand_tildeExpands ~ expressions to the user's home directory. On Unix ~user directories are resolved as well.
sys::fs::file_sizeGet file size.
sys::fs::getPermissionsGet file permissions.
sys::fs::getPotentiallyUniqueFileNameGet a unique name, not currently exisiting in the filesystem. Subject to race conditions, prefer to use createUniqueFile instead.
sys::fs::getPotentiallyUniqueTempFileNameGet a unique temporary file name, not currently exisiting in the filesystem. Subject to race conditions, prefer to use createTemporaryFile instead.
sys::fs::getUniqueIDFill Result with the unique ID of the filesystem object at Path.
sys::fs::get_file_typeDoes status represent a directory?
sys::fs::is_directoryIs path a directory?
sys::fs::is_directoryReturn whether Path is a directory, ignoring errors as false.
sys::fs::is_localReturn whether Path is on a local filesystem, ignoring errors as false.
sys::fs::is_localIs the file mounted on a local filesystem?
sys::fs::is_otherIs path something that exists but is not a directory, regular file, or symlink?
sys::fs::is_regular_fileIs path a regular file?
sys::fs::is_regular_fileReturn whether Path is a regular file, ignoring errors as false.
sys::fs::is_symlink_fileReturn whether Path is a symbolic link, ignoring errors as false.
sys::fs::is_symlink_fileIs path a symlink file?
sys::fs::md5_contentsCompute an MD5 hash of the file at Path.
sys::fs::openFileOpens a file with the specified creation disposition, access mode, and flags and returns a file descriptor.
sys::fs::openFileForReadOpens the file with the given name in a read-only mode, returning its open file descriptor.
sys::fs::openFileForReadWriteOpens the file for read-write, creating it if it does not exist.
sys::fs::openFileForWriteOpens the file for write, creating it if it does not exist.
sys::fs::openNativeFileOpens a file with the specified creation disposition, access mode, and flags and returns a platform-specific file object.
sys::fs::openNativeFileForReadOpens the file with the given name in a read-only mode, returning its open file descriptor.
sys::fs::openNativeFileForReadWriteOpens the file for read-write, creating it if it does not exist.
sys::fs::openNativeFileForWriteOpens the file for write, creating it if it does not exist.
sys::fs::readlinkRead the target of a symbolic link.
sys::fs::real_pathCollapse all . and .. patterns, resolve all symlinks, and optionally expand ~ expressions to the user's home directory.
sys::fs::removeRemove path. Equivalent to POSIX remove().
sys::fs::remove_directoriesRecursively delete a directory.
sys::fs::renameRename from to to.
sys::fs::setLastAccessAndModificationTimeSet the file modification and access time, by path.
sys::fs::setLastAccessAndModificationTimeSet both access and modification time by path to the same value.
sys::fs::setPermissionsSet file permissions.
sys::fs::set_current_pathSet the current path.
sys::fs::statusGet file status as if by POSIX stat().
sys::fs::status_knownIs status available?
sys::path::has_extensionHas extension?
sys::path::has_filenameHas filename?
sys::path::has_parent_pathHas parent path?
sys::path::has_relative_pathHas relative path?
sys::path::has_root_directoryHas root directory?
sys::path::has_root_nameHas root name?
sys::path::has_root_pathHas root path?
sys::path::has_stemHas stem?
sys::path::is_absoluteIs path absolute?
sys::path::is_absolute_gnuIs path absolute using GNU rules?
sys::path::is_relativeIs path relative?
sys::path::make_absoluteMake path an absolute path.
sys::path::nativeConvert path to the native form as a std::string.
sys::path::nativeConvert path to the native form.

See Also

isNullary(),

isUnary(), and

isBinary() predicates exist for testing the number of children.

isValid().