mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
Add Path and Twine components from LLVM. (#10)
This commit is contained in:
457
src/main/native/include/llvm/Path.h
Normal file
457
src/main/native/include/llvm/Path.h
Normal file
@@ -0,0 +1,457 @@
|
||||
//===- llvm/Support/Path.h - Path Operating System Concept ------*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file declares the llvm::sys::path namespace. It is designed after
|
||||
// TR2/boost filesystem (v3), but modified to remove exception handling and the
|
||||
// path class.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_SUPPORT_PATH_H
|
||||
#define LLVM_SUPPORT_PATH_H
|
||||
|
||||
#include "llvm/Twine.h"
|
||||
#include <iterator>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace llvm {
|
||||
namespace sys {
|
||||
namespace path {
|
||||
|
||||
/// @name Lexical Component Iterator
|
||||
/// @{
|
||||
|
||||
/// @brief Path iterator.
|
||||
///
|
||||
/// This is an input iterator that iterates over the individual components in
|
||||
/// \a path. The traversal order is as follows:
|
||||
/// * The root-name element, if present.
|
||||
/// * The root-directory element, if present.
|
||||
/// * Each successive filename element, if present.
|
||||
/// * Dot, if one or more trailing non-root slash characters are present.
|
||||
/// Traversing backwards is possible with \a reverse_iterator
|
||||
///
|
||||
/// Iteration examples. Each component is separated by ',':
|
||||
/// @code
|
||||
/// / => /
|
||||
/// /foo => /,foo
|
||||
/// foo/ => foo,.
|
||||
/// /foo/bar => /,foo,bar
|
||||
/// ../ => ..,.
|
||||
/// C:\foo\bar => C:,/,foo,bar
|
||||
/// @endcode
|
||||
class const_iterator
|
||||
: public std::iterator<std::input_iterator_tag, const StringRef> {
|
||||
StringRef Path; ///< The entire path.
|
||||
StringRef Component; ///< The current component. Not necessarily in Path.
|
||||
size_t Position; ///< The iterators current position within Path.
|
||||
|
||||
// An end iterator has Position = Path.size() + 1.
|
||||
friend const_iterator begin(StringRef path);
|
||||
friend const_iterator end(StringRef path);
|
||||
|
||||
public:
|
||||
reference operator*() const { return Component; }
|
||||
pointer operator->() const { return &Component; }
|
||||
const_iterator &operator++(); // preincrement
|
||||
bool operator==(const const_iterator &RHS) const;
|
||||
bool operator!=(const const_iterator &RHS) const { return !(*this == RHS); }
|
||||
|
||||
/// @brief Difference in bytes between this and RHS.
|
||||
ptrdiff_t operator-(const const_iterator &RHS) const;
|
||||
};
|
||||
|
||||
/// @brief Reverse path iterator.
|
||||
///
|
||||
/// This is an input iterator that iterates over the individual components in
|
||||
/// \a path in reverse order. The traversal order is exactly reversed from that
|
||||
/// of \a const_iterator
|
||||
class reverse_iterator
|
||||
: public std::iterator<std::input_iterator_tag, const StringRef> {
|
||||
StringRef Path; ///< The entire path.
|
||||
StringRef Component; ///< The current component. Not necessarily in Path.
|
||||
size_t Position; ///< The iterators current position within Path.
|
||||
|
||||
friend reverse_iterator rbegin(StringRef path);
|
||||
friend reverse_iterator rend(StringRef path);
|
||||
|
||||
public:
|
||||
reference operator*() const { return Component; }
|
||||
pointer operator->() const { return &Component; }
|
||||
reverse_iterator &operator++(); // preincrement
|
||||
bool operator==(const reverse_iterator &RHS) const;
|
||||
bool operator!=(const reverse_iterator &RHS) const { return !(*this == RHS); }
|
||||
|
||||
/// @brief Difference in bytes between this and RHS.
|
||||
ptrdiff_t operator-(const reverse_iterator &RHS) const;
|
||||
};
|
||||
|
||||
/// @brief Get begin iterator over \a path.
|
||||
/// @param path Input path.
|
||||
/// @returns Iterator initialized with the first component of \a path.
|
||||
const_iterator begin(StringRef path);
|
||||
|
||||
/// @brief Get end iterator over \a path.
|
||||
/// @param path Input path.
|
||||
/// @returns Iterator initialized to the end of \a path.
|
||||
const_iterator end(StringRef path);
|
||||
|
||||
/// @brief Get reverse begin iterator over \a path.
|
||||
/// @param path Input path.
|
||||
/// @returns Iterator initialized with the first reverse component of \a path.
|
||||
reverse_iterator rbegin(StringRef path);
|
||||
|
||||
/// @brief Get reverse end iterator over \a path.
|
||||
/// @param path Input path.
|
||||
/// @returns Iterator initialized to the reverse end of \a path.
|
||||
reverse_iterator rend(StringRef path);
|
||||
|
||||
/// @}
|
||||
/// @name Lexical Modifiers
|
||||
/// @{
|
||||
|
||||
/// @brief Remove the last component from \a path unless it is the root dir.
|
||||
///
|
||||
/// @code
|
||||
/// directory/filename.cpp => directory/
|
||||
/// directory/ => directory
|
||||
/// filename.cpp => <empty>
|
||||
/// / => /
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path A path that is modified to not have a file component.
|
||||
void remove_filename(SmallVectorImpl<char> &path);
|
||||
|
||||
/// @brief Replace the file extension of \a path with \a extension.
|
||||
///
|
||||
/// @code
|
||||
/// ./filename.cpp => ./filename.extension
|
||||
/// ./filename => ./filename.extension
|
||||
/// ./ => ./.extension
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path A path that has its extension replaced with \a extension.
|
||||
/// @param extension The extension to be added. It may be empty. It may also
|
||||
/// optionally start with a '.', if it does not, one will be
|
||||
/// prepended.
|
||||
void replace_extension(SmallVectorImpl<char> &path, const Twine &extension);
|
||||
|
||||
/// @brief Replace matching path prefix with another path.
|
||||
///
|
||||
/// @code
|
||||
/// /foo, /old, /new => /foo
|
||||
/// /old/foo, /old, /new => /new/foo
|
||||
/// /foo, <empty>, /new => /new/foo
|
||||
/// /old/foo, /old, <empty> => /foo
|
||||
/// @endcode
|
||||
///
|
||||
/// @param Path If \a Path starts with \a OldPrefix modify to instead
|
||||
/// start with \a NewPrefix.
|
||||
/// @param OldPrefix The path prefix to strip from \a Path.
|
||||
/// @param NewPrefix The path prefix to replace \a NewPrefix with.
|
||||
void replace_path_prefix(SmallVectorImpl<char> &Path,
|
||||
const StringRef &OldPrefix,
|
||||
const StringRef &NewPrefix);
|
||||
|
||||
/// @brief Append to path.
|
||||
///
|
||||
/// @code
|
||||
/// /foo + bar/f => /foo/bar/f
|
||||
/// /foo/ + bar/f => /foo/bar/f
|
||||
/// foo + bar/f => foo/bar/f
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path Set to \a path + \a component.
|
||||
/// @param a The component to be appended to \a path.
|
||||
void append(SmallVectorImpl<char> &path, const Twine &a,
|
||||
const Twine &b = "",
|
||||
const Twine &c = "",
|
||||
const Twine &d = "");
|
||||
|
||||
/// @brief Append to path.
|
||||
///
|
||||
/// @code
|
||||
/// /foo + [bar,f] => /foo/bar/f
|
||||
/// /foo/ + [bar,f] => /foo/bar/f
|
||||
/// foo + [bar,f] => foo/bar/f
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path Set to \a path + [\a begin, \a end).
|
||||
/// @param begin Start of components to append.
|
||||
/// @param end One past the end of components to append.
|
||||
void append(SmallVectorImpl<char> &path,
|
||||
const_iterator begin, const_iterator end);
|
||||
|
||||
/// @}
|
||||
/// @name Transforms (or some other better name)
|
||||
/// @{
|
||||
|
||||
/// Convert path to the native form. This is used to give paths to users and
|
||||
/// operating system calls in the platform's normal way. For example, on Windows
|
||||
/// all '/' are converted to '\'.
|
||||
///
|
||||
/// @param path A path that is transformed to native format.
|
||||
/// @param result Holds the result of the transformation.
|
||||
void native(const Twine &path, SmallVectorImpl<char> &result);
|
||||
|
||||
/// Convert path to the native form in place. This is used to give paths to
|
||||
/// users and operating system calls in the platform's normal way. For example,
|
||||
/// on Windows all '/' are converted to '\'.
|
||||
///
|
||||
/// @param path A path that is transformed to native format.
|
||||
void native(SmallVectorImpl<char> &path);
|
||||
|
||||
/// @}
|
||||
/// @name Lexical Observers
|
||||
/// @{
|
||||
|
||||
/// @brief Get root name.
|
||||
///
|
||||
/// @code
|
||||
/// //net/hello => //net
|
||||
/// c:/hello => c: (on Windows, on other platforms nothing)
|
||||
/// /hello => <empty>
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result The root name of \a path if it has one, otherwise "".
|
||||
StringRef root_name(StringRef path);
|
||||
|
||||
/// @brief Get root directory.
|
||||
///
|
||||
/// @code
|
||||
/// /goo/hello => /
|
||||
/// c:/hello => /
|
||||
/// d/file.txt => <empty>
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result The root directory of \a path if it has one, otherwise
|
||||
/// "".
|
||||
StringRef root_directory(StringRef path);
|
||||
|
||||
/// @brief Get root path.
|
||||
///
|
||||
/// Equivalent to root_name + root_directory.
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result The root path of \a path if it has one, otherwise "".
|
||||
StringRef root_path(StringRef path);
|
||||
|
||||
/// @brief Get relative path.
|
||||
///
|
||||
/// @code
|
||||
/// C:\hello\world => hello\world
|
||||
/// foo/bar => foo/bar
|
||||
/// /foo/bar => foo/bar
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result The path starting after root_path if one exists, otherwise "".
|
||||
StringRef relative_path(StringRef path);
|
||||
|
||||
/// @brief Get parent path.
|
||||
///
|
||||
/// @code
|
||||
/// / => <empty>
|
||||
/// /foo => /
|
||||
/// foo/../bar => foo/..
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result The parent path of \a path if one exists, otherwise "".
|
||||
StringRef parent_path(StringRef path);
|
||||
|
||||
/// @brief Get filename.
|
||||
///
|
||||
/// @code
|
||||
/// /foo.txt => foo.txt
|
||||
/// . => .
|
||||
/// .. => ..
|
||||
/// / => /
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result The filename part of \a path. This is defined as the last component
|
||||
/// of \a path.
|
||||
StringRef filename(StringRef path);
|
||||
|
||||
/// @brief Get stem.
|
||||
///
|
||||
/// If filename contains a dot but not solely one or two dots, result is the
|
||||
/// substring of filename ending at (but not including) the last dot. Otherwise
|
||||
/// it is filename.
|
||||
///
|
||||
/// @code
|
||||
/// /foo/bar.txt => bar
|
||||
/// /foo/bar => bar
|
||||
/// /foo/.txt => <empty>
|
||||
/// /foo/. => .
|
||||
/// /foo/.. => ..
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result The stem of \a path.
|
||||
StringRef stem(StringRef path);
|
||||
|
||||
/// @brief Get extension.
|
||||
///
|
||||
/// If filename contains a dot but not solely one or two dots, result is the
|
||||
/// substring of filename starting at (and including) the last dot, and ending
|
||||
/// at the end of \a path. Otherwise "".
|
||||
///
|
||||
/// @code
|
||||
/// /foo/bar.txt => .txt
|
||||
/// /foo/bar => <empty>
|
||||
/// /foo/.txt => .txt
|
||||
/// @endcode
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result The extension of \a path.
|
||||
StringRef extension(StringRef path);
|
||||
|
||||
/// @brief Check whether the given char is a path separator on the host OS.
|
||||
///
|
||||
/// @param value a character
|
||||
/// @result true if \a value is a path separator character on the host OS
|
||||
bool is_separator(char value);
|
||||
|
||||
/// @brief Return the preferred separator for this platform.
|
||||
///
|
||||
/// @result StringRef of the preferred separator, null-terminated.
|
||||
StringRef get_separator();
|
||||
|
||||
/// @brief Get the typical temporary directory for the system, e.g.,
|
||||
/// "/var/tmp" or "C:/TEMP"
|
||||
///
|
||||
/// @param erasedOnReboot Whether to favor a path that is erased on reboot
|
||||
/// rather than one that potentially persists longer. This parameter will be
|
||||
/// ignored if the user or system has set the typical environment variable
|
||||
/// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory.
|
||||
///
|
||||
/// @param result Holds the resulting path name.
|
||||
void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
|
||||
|
||||
/// @brief Get the user's home directory.
|
||||
///
|
||||
/// @param result Holds the resulting path name.
|
||||
/// @result True if a home directory is set, false otherwise.
|
||||
bool home_directory(SmallVectorImpl<char> &result);
|
||||
|
||||
/// @brief Get the user's cache directory.
|
||||
///
|
||||
/// Expect the resulting path to be a directory shared with other
|
||||
/// applications/services used by the user. Params \p Path1 to \p Path3 can be
|
||||
/// used to append additional directory names to the resulting path. Recommended
|
||||
/// pattern is <user_cache_directory>/<vendor>/<application>.
|
||||
///
|
||||
/// @param Result Holds the resulting path.
|
||||
/// @param Path1 Additional path to be appended to the user's cache directory
|
||||
/// path. "" can be used to append nothing.
|
||||
/// @param Path2 Second additional path to be appended.
|
||||
/// @param Path3 Third additional path to be appended.
|
||||
/// @result True if a cache directory path is set, false otherwise.
|
||||
bool user_cache_directory(SmallVectorImpl<char> &Result, const Twine &Path1,
|
||||
const Twine &Path2 = "", const Twine &Path3 = "");
|
||||
|
||||
/// @brief Has root name?
|
||||
///
|
||||
/// root_name != ""
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result True if the path has a root name, false otherwise.
|
||||
bool has_root_name(const Twine &path);
|
||||
|
||||
/// @brief Has root directory?
|
||||
///
|
||||
/// root_directory != ""
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result True if the path has a root directory, false otherwise.
|
||||
bool has_root_directory(const Twine &path);
|
||||
|
||||
/// @brief Has root path?
|
||||
///
|
||||
/// root_path != ""
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result True if the path has a root path, false otherwise.
|
||||
bool has_root_path(const Twine &path);
|
||||
|
||||
/// @brief Has relative path?
|
||||
///
|
||||
/// relative_path != ""
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result True if the path has a relative path, false otherwise.
|
||||
bool has_relative_path(const Twine &path);
|
||||
|
||||
/// @brief Has parent path?
|
||||
///
|
||||
/// parent_path != ""
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result True if the path has a parent path, false otherwise.
|
||||
bool has_parent_path(const Twine &path);
|
||||
|
||||
/// @brief Has filename?
|
||||
///
|
||||
/// filename != ""
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result True if the path has a filename, false otherwise.
|
||||
bool has_filename(const Twine &path);
|
||||
|
||||
/// @brief Has stem?
|
||||
///
|
||||
/// stem != ""
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result True if the path has a stem, false otherwise.
|
||||
bool has_stem(const Twine &path);
|
||||
|
||||
/// @brief Has extension?
|
||||
///
|
||||
/// extension != ""
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result True if the path has a extension, false otherwise.
|
||||
bool has_extension(const Twine &path);
|
||||
|
||||
/// @brief Is path absolute?
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result True if the path is absolute, false if it is not.
|
||||
bool is_absolute(const Twine &path);
|
||||
|
||||
/// @brief Is path relative?
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result True if the path is relative, false if it is not.
|
||||
bool is_relative(const Twine &path);
|
||||
|
||||
/// @brief Remove redundant leading "./" pieces and consecutive separators.
|
||||
///
|
||||
/// @param path Input path.
|
||||
/// @result The cleaned-up \a path.
|
||||
StringRef remove_leading_dotslash(StringRef path);
|
||||
|
||||
/// @brief In-place remove any './' and optionally '../' components from a path.
|
||||
///
|
||||
/// @param path processed path
|
||||
/// @param remove_dot_dot specify if '../' should be removed
|
||||
/// @result True if path was changed
|
||||
bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot = false);
|
||||
|
||||
} // end namespace path
|
||||
} // end namespace sys
|
||||
} // end namespace llvm
|
||||
|
||||
#endif
|
||||
542
src/main/native/include/llvm/Twine.h
Normal file
542
src/main/native/include/llvm/Twine.h
Normal file
@@ -0,0 +1,542 @@
|
||||
//===-- Twine.h - Fast Temporary String Concatenation -----------*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_ADT_TWINE_H
|
||||
#define LLVM_ADT_TWINE_H
|
||||
|
||||
#include "llvm/SmallVector.h"
|
||||
#include "llvm/StringRef.h"
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace llvm {
|
||||
class raw_ostream;
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// \b 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
|
||||
/// isNullary(), \see isUnary(), and \see isBinary() predicates exist for
|
||||
/// testing the number of children.
|
||||
///
|
||||
/// 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 \see isValid().
|
||||
///
|
||||
/// \b 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.
|
||||
class Twine {
|
||||
/// NodeKind - Represent the type of an argument.
|
||||
enum NodeKind : unsigned char {
|
||||
/// An empty string; the result of concatenating anything with it is also
|
||||
/// empty.
|
||||
NullKind,
|
||||
|
||||
/// The empty string.
|
||||
EmptyKind,
|
||||
|
||||
/// A pointer to a Twine instance.
|
||||
TwineKind,
|
||||
|
||||
/// A pointer to a C string instance.
|
||||
CStringKind,
|
||||
|
||||
/// A pointer to an std::string instance.
|
||||
StdStringKind,
|
||||
|
||||
/// A pointer to a StringRef instance.
|
||||
StringRefKind,
|
||||
|
||||
/// A pointer to a SmallString instance.
|
||||
SmallStringKind,
|
||||
|
||||
/// A char value, to render as a character.
|
||||
CharKind,
|
||||
|
||||
/// An unsigned int value, to render as an unsigned decimal integer.
|
||||
DecUIKind,
|
||||
|
||||
/// An int value, to render as a signed decimal integer.
|
||||
DecIKind,
|
||||
|
||||
/// A pointer to an unsigned long value, to render as an unsigned decimal
|
||||
/// integer.
|
||||
DecULKind,
|
||||
|
||||
/// A pointer to a long value, to render as a signed decimal integer.
|
||||
DecLKind,
|
||||
|
||||
/// A pointer to an unsigned long long value, to render as an unsigned
|
||||
/// decimal integer.
|
||||
DecULLKind,
|
||||
|
||||
/// A pointer to a long long value, to render as a signed decimal integer.
|
||||
DecLLKind,
|
||||
|
||||
/// A pointer to a uint64_t value, to render as an unsigned hexadecimal
|
||||
/// integer.
|
||||
UHexKind
|
||||
};
|
||||
|
||||
union Child
|
||||
{
|
||||
const Twine *twine;
|
||||
const char *cString;
|
||||
const std::string *stdString;
|
||||
const StringRef *stringRef;
|
||||
const SmallVectorImpl<char> *smallString;
|
||||
char character;
|
||||
unsigned int decUI;
|
||||
int decI;
|
||||
const unsigned long *decUL;
|
||||
const long *decL;
|
||||
const unsigned long long *decULL;
|
||||
const long long *decLL;
|
||||
const uint64_t *uHex;
|
||||
};
|
||||
|
||||
private:
|
||||
/// LHS - The prefix in the concatenation, which may be uninitialized for
|
||||
/// Null or Empty kinds.
|
||||
Child LHS;
|
||||
/// RHS - The suffix in the concatenation, which may be uninitialized for
|
||||
/// Null or Empty kinds.
|
||||
Child RHS;
|
||||
/// LHSKind - The NodeKind of the left hand side, \see getLHSKind().
|
||||
NodeKind LHSKind;
|
||||
/// RHSKind - The NodeKind of the right hand side, \see getRHSKind().
|
||||
NodeKind RHSKind;
|
||||
|
||||
private:
|
||||
/// Construct a nullary twine; the kind must be NullKind or EmptyKind.
|
||||
explicit Twine(NodeKind Kind)
|
||||
: LHSKind(Kind), RHSKind(EmptyKind) {
|
||||
assert(isNullary() && "Invalid kind!");
|
||||
}
|
||||
|
||||
/// Construct a binary twine.
|
||||
explicit Twine(const Twine &LHS, const Twine &RHS)
|
||||
: LHSKind(TwineKind), RHSKind(TwineKind) {
|
||||
this->LHS.twine = &LHS;
|
||||
this->RHS.twine = &RHS;
|
||||
assert(isValid() && "Invalid twine!");
|
||||
}
|
||||
|
||||
/// Construct a twine from explicit values.
|
||||
explicit Twine(Child LHS, NodeKind LHSKind, Child RHS, NodeKind RHSKind)
|
||||
: LHS(LHS), RHS(RHS), LHSKind(LHSKind), RHSKind(RHSKind) {
|
||||
assert(isValid() && "Invalid twine!");
|
||||
}
|
||||
|
||||
/// Since the intended use of twines is as temporary objects, assignments
|
||||
/// when concatenating might cause undefined behavior or stack corruptions
|
||||
Twine &operator=(const Twine &Other) = delete;
|
||||
|
||||
/// Check for the null twine.
|
||||
bool isNull() const {
|
||||
return getLHSKind() == NullKind;
|
||||
}
|
||||
|
||||
/// Check for the empty twine.
|
||||
bool isEmpty() const {
|
||||
return getLHSKind() == EmptyKind;
|
||||
}
|
||||
|
||||
/// Check if this is a nullary twine (null or empty).
|
||||
bool isNullary() const {
|
||||
return isNull() || isEmpty();
|
||||
}
|
||||
|
||||
/// Check if this is a unary twine.
|
||||
bool isUnary() const {
|
||||
return getRHSKind() == EmptyKind && !isNullary();
|
||||
}
|
||||
|
||||
/// Check if this is a binary twine.
|
||||
bool isBinary() const {
|
||||
return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
|
||||
}
|
||||
|
||||
/// Check if this is a valid twine (satisfying the invariants on
|
||||
/// order and number of arguments).
|
||||
bool isValid() const {
|
||||
// Nullary twines always have Empty on the RHS.
|
||||
if (isNullary() && getRHSKind() != EmptyKind)
|
||||
return false;
|
||||
|
||||
// Null should never appear on the RHS.
|
||||
if (getRHSKind() == NullKind)
|
||||
return false;
|
||||
|
||||
// The RHS cannot be non-empty if the LHS is empty.
|
||||
if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
|
||||
return false;
|
||||
|
||||
// A twine child should always be binary.
|
||||
if (getLHSKind() == TwineKind &&
|
||||
!LHS.twine->isBinary())
|
||||
return false;
|
||||
if (getRHSKind() == TwineKind &&
|
||||
!RHS.twine->isBinary())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Get the NodeKind of the left-hand side.
|
||||
NodeKind getLHSKind() const { return LHSKind; }
|
||||
|
||||
/// Get the NodeKind of the right-hand side.
|
||||
NodeKind getRHSKind() const { return RHSKind; }
|
||||
|
||||
/// Print one child from a twine.
|
||||
void printOneChild(raw_ostream &OS, Child Ptr, NodeKind Kind) const;
|
||||
|
||||
/// Print the representation of one child from a twine.
|
||||
void printOneChildRepr(raw_ostream &OS, Child Ptr,
|
||||
NodeKind Kind) const;
|
||||
|
||||
public:
|
||||
/// @name Constructors
|
||||
/// @{
|
||||
|
||||
/// Construct from an empty string.
|
||||
/*implicit*/ Twine() : LHSKind(EmptyKind), RHSKind(EmptyKind) {
|
||||
assert(isValid() && "Invalid twine!");
|
||||
}
|
||||
|
||||
Twine(const Twine &) = default;
|
||||
|
||||
/// 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.
|
||||
/*implicit*/ Twine(const char *Str)
|
||||
: RHSKind(EmptyKind) {
|
||||
if (Str[0] != '\0') {
|
||||
LHS.cString = Str;
|
||||
LHSKind = CStringKind;
|
||||
} else
|
||||
LHSKind = EmptyKind;
|
||||
|
||||
assert(isValid() && "Invalid twine!");
|
||||
}
|
||||
|
||||
/// Construct from an std::string.
|
||||
/*implicit*/ Twine(const std::string &Str)
|
||||
: LHSKind(StdStringKind), RHSKind(EmptyKind) {
|
||||
LHS.stdString = &Str;
|
||||
assert(isValid() && "Invalid twine!");
|
||||
}
|
||||
|
||||
/// Construct from a StringRef.
|
||||
/*implicit*/ Twine(const StringRef &Str)
|
||||
: LHSKind(StringRefKind), RHSKind(EmptyKind) {
|
||||
LHS.stringRef = &Str;
|
||||
assert(isValid() && "Invalid twine!");
|
||||
}
|
||||
|
||||
/// Construct from a SmallString.
|
||||
/*implicit*/ Twine(const SmallVectorImpl<char> &Str)
|
||||
: LHSKind(SmallStringKind), RHSKind(EmptyKind) {
|
||||
LHS.smallString = &Str;
|
||||
assert(isValid() && "Invalid twine!");
|
||||
}
|
||||
|
||||
/// Construct from a char.
|
||||
explicit Twine(char Val)
|
||||
: LHSKind(CharKind), RHSKind(EmptyKind) {
|
||||
LHS.character = Val;
|
||||
}
|
||||
|
||||
/// Construct from a signed char.
|
||||
explicit Twine(signed char Val)
|
||||
: LHSKind(CharKind), RHSKind(EmptyKind) {
|
||||
LHS.character = static_cast<char>(Val);
|
||||
}
|
||||
|
||||
/// Construct from an unsigned char.
|
||||
explicit Twine(unsigned char Val)
|
||||
: LHSKind(CharKind), RHSKind(EmptyKind) {
|
||||
LHS.character = static_cast<char>(Val);
|
||||
}
|
||||
|
||||
/// Construct a twine to print \p Val as an unsigned decimal integer.
|
||||
explicit Twine(unsigned Val)
|
||||
: LHSKind(DecUIKind), RHSKind(EmptyKind) {
|
||||
LHS.decUI = Val;
|
||||
}
|
||||
|
||||
/// Construct a twine to print \p Val as a signed decimal integer.
|
||||
explicit Twine(int Val)
|
||||
: LHSKind(DecIKind), RHSKind(EmptyKind) {
|
||||
LHS.decI = Val;
|
||||
}
|
||||
|
||||
/// Construct a twine to print \p Val as an unsigned decimal integer.
|
||||
explicit Twine(const unsigned long &Val)
|
||||
: LHSKind(DecULKind), RHSKind(EmptyKind) {
|
||||
LHS.decUL = &Val;
|
||||
}
|
||||
|
||||
/// Construct a twine to print \p Val as a signed decimal integer.
|
||||
explicit Twine(const long &Val)
|
||||
: LHSKind(DecLKind), RHSKind(EmptyKind) {
|
||||
LHS.decL = &Val;
|
||||
}
|
||||
|
||||
/// Construct a twine to print \p Val as an unsigned decimal integer.
|
||||
explicit Twine(const unsigned long long &Val)
|
||||
: LHSKind(DecULLKind), RHSKind(EmptyKind) {
|
||||
LHS.decULL = &Val;
|
||||
}
|
||||
|
||||
/// Construct a twine to print \p Val as a signed decimal integer.
|
||||
explicit Twine(const long long &Val)
|
||||
: LHSKind(DecLLKind), RHSKind(EmptyKind) {
|
||||
LHS.decLL = &Val;
|
||||
}
|
||||
|
||||
// FIXME: Unfortunately, to make sure this is as efficient as possible we
|
||||
// need extra binary constructors from particular types. We can't rely on
|
||||
// the compiler to be smart enough to fold operator+()/concat() down to the
|
||||
// right thing. Yet.
|
||||
|
||||
/// Construct as the concatenation of a C string and a StringRef.
|
||||
/*implicit*/ Twine(const char *LHS, const StringRef &RHS)
|
||||
: LHSKind(CStringKind), RHSKind(StringRefKind) {
|
||||
this->LHS.cString = LHS;
|
||||
this->RHS.stringRef = &RHS;
|
||||
assert(isValid() && "Invalid twine!");
|
||||
}
|
||||
|
||||
/// Construct as the concatenation of a StringRef and a C string.
|
||||
/*implicit*/ Twine(const StringRef &LHS, const char *RHS)
|
||||
: LHSKind(StringRefKind), RHSKind(CStringKind) {
|
||||
this->LHS.stringRef = &LHS;
|
||||
this->RHS.cString = RHS;
|
||||
assert(isValid() && "Invalid twine!");
|
||||
}
|
||||
|
||||
/// Create a 'null' string, which is an empty string that always
|
||||
/// concatenates to form another empty string.
|
||||
static Twine createNull() {
|
||||
return Twine(NullKind);
|
||||
}
|
||||
|
||||
/// @}
|
||||
/// @name Numeric Conversions
|
||||
/// @{
|
||||
|
||||
// Construct a twine to print \p Val as an unsigned hexadecimal integer.
|
||||
static Twine utohexstr(const uint64_t &Val) {
|
||||
Child LHS, RHS;
|
||||
LHS.uHex = &Val;
|
||||
RHS.twine = nullptr;
|
||||
return Twine(LHS, UHexKind, RHS, EmptyKind);
|
||||
}
|
||||
|
||||
/// @}
|
||||
/// @name Predicate Operations
|
||||
/// @{
|
||||
|
||||
/// Check if this twine is trivially empty; a false return value does not
|
||||
/// necessarily mean the twine is empty.
|
||||
bool isTriviallyEmpty() const {
|
||||
return isNullary();
|
||||
}
|
||||
|
||||
/// Return true if this twine can be dynamically accessed as a single
|
||||
/// StringRef value with getSingleStringRef().
|
||||
bool isSingleStringRef() const {
|
||||
if (getRHSKind() != EmptyKind) return false;
|
||||
|
||||
switch (getLHSKind()) {
|
||||
case EmptyKind:
|
||||
case CStringKind:
|
||||
case StdStringKind:
|
||||
case StringRefKind:
|
||||
case SmallStringKind:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// @}
|
||||
/// @name String Operations
|
||||
/// @{
|
||||
|
||||
Twine concat(const Twine &Suffix) const;
|
||||
|
||||
/// @}
|
||||
/// @name Output & Conversion.
|
||||
/// @{
|
||||
|
||||
/// Return the twine contents as a std::string.
|
||||
std::string str() const;
|
||||
|
||||
/// Append the concatenated string into the given SmallString or SmallVector.
|
||||
void toVector(SmallVectorImpl<char> &Out) const;
|
||||
|
||||
/// This returns the twine as a single StringRef. This method is only valid
|
||||
/// if isSingleStringRef() is true.
|
||||
StringRef getSingleStringRef() const {
|
||||
assert(isSingleStringRef() &&"This cannot be had as a single stringref!");
|
||||
switch (getLHSKind()) {
|
||||
default:
|
||||
// unreachable("Out of sync with isSingleStringRef");
|
||||
return StringRef();
|
||||
case EmptyKind: return StringRef();
|
||||
case CStringKind: return StringRef(LHS.cString);
|
||||
case StdStringKind: return StringRef(*LHS.stdString);
|
||||
case StringRefKind: return *LHS.stringRef;
|
||||
case SmallStringKind:
|
||||
return StringRef(LHS.smallString->data(), LHS.smallString->size());
|
||||
}
|
||||
}
|
||||
|
||||
/// This returns the twine as a single 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.
|
||||
StringRef toStringRef(SmallVectorImpl<char> &Out) const {
|
||||
if (isSingleStringRef())
|
||||
return getSingleStringRef();
|
||||
toVector(Out);
|
||||
return StringRef(Out.data(), Out.size());
|
||||
}
|
||||
|
||||
/// 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.
|
||||
StringRef toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const;
|
||||
|
||||
/// Write the concatenated string represented by this twine to the
|
||||
/// stream \p OS.
|
||||
void print(raw_ostream &OS) const;
|
||||
|
||||
/// Dump the concatenated string represented by this twine to stderr.
|
||||
void dump() const;
|
||||
|
||||
/// Write the representation of this twine to the stream \p OS.
|
||||
void printRepr(raw_ostream &OS) const;
|
||||
|
||||
/// Dump the representation of this twine to stderr.
|
||||
void dumpRepr() const;
|
||||
|
||||
/// @}
|
||||
};
|
||||
|
||||
/// @name Twine Inline Implementations
|
||||
/// @{
|
||||
|
||||
inline Twine Twine::concat(const Twine &Suffix) const {
|
||||
// Concatenation with null is null.
|
||||
if (isNull() || Suffix.isNull())
|
||||
return Twine(NullKind);
|
||||
|
||||
// Concatenation with empty yields the other side.
|
||||
if (isEmpty())
|
||||
return Suffix;
|
||||
if (Suffix.isEmpty())
|
||||
return *this;
|
||||
|
||||
// Otherwise we need to create a new node, taking care to fold in unary
|
||||
// twines.
|
||||
Child NewLHS, NewRHS;
|
||||
NewLHS.twine = this;
|
||||
NewRHS.twine = &Suffix;
|
||||
NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
|
||||
if (isUnary()) {
|
||||
NewLHS = LHS;
|
||||
NewLHSKind = getLHSKind();
|
||||
}
|
||||
if (Suffix.isUnary()) {
|
||||
NewRHS = Suffix.LHS;
|
||||
NewRHSKind = Suffix.getLHSKind();
|
||||
}
|
||||
|
||||
return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
|
||||
}
|
||||
|
||||
inline Twine operator+(const Twine &LHS, const Twine &RHS) {
|
||||
return LHS.concat(RHS);
|
||||
}
|
||||
|
||||
/// Additional overload to guarantee simplified codegen; this is equivalent to
|
||||
/// concat().
|
||||
|
||||
inline Twine operator+(const char *LHS, const StringRef &RHS) {
|
||||
return Twine(LHS, RHS);
|
||||
}
|
||||
|
||||
/// Additional overload to guarantee simplified codegen; this is equivalent to
|
||||
/// concat().
|
||||
|
||||
inline Twine operator+(const StringRef &LHS, const char *RHS) {
|
||||
return Twine(LHS, RHS);
|
||||
}
|
||||
|
||||
inline raw_ostream &operator<<(raw_ostream &OS, const Twine &RHS) {
|
||||
RHS.print(OS);
|
||||
return OS;
|
||||
}
|
||||
|
||||
/// @}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user