1
//===-- Twine.h - Fast Temporary String Concatenation -----------*- C++ -*-===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
#ifndef LLVM_ADT_TWINE_H
11
#define LLVM_ADT_TWINE_H
13
#include "llvm/ADT/StringRef.h"
14
#include "llvm/System/DataTypes.h"
20
class SmallVectorImpl;
24
/// Twine - A lightweight data structure for efficiently representing the
25
/// concatenation of temporary values as strings.
27
/// A Twine is a kind of rope, it represents a concatenated string using a
28
/// binary-tree, where the string is the preorder of the nodes. Since the
29
/// Twine can be efficiently rendered into a buffer when its result is used,
30
/// it avoids the cost of generating temporary values for intermediate string
31
/// results -- particularly in cases when the Twine result is never
32
/// required. By explicitly tracking the type of leaf nodes, we can also avoid
33
/// the creation of temporary strings for conversions operations (such as
34
/// appending an integer to a string).
36
/// A Twine is not intended for use directly and should not be stored, its
37
/// implementation relies on the ability to store pointers to temporary stack
38
/// objects which may be deallocated at the end of a statement. Twines should
39
/// only be used accepted as const references in arguments, when an API wishes
40
/// to accept possibly-concatenated strings.
42
/// Twines support a special 'null' value, which always concatenates to form
43
/// itself, and renders as an empty string. This can be returned from APIs to
44
/// effectively nullify any concatenations performed on the result.
46
/// \b Implementation \n
48
/// Given the nature of a Twine, it is not possible for the Twine's
49
/// concatenation method to construct interior nodes; the result must be
50
/// represented inside the returned value. For this reason a Twine object
51
/// actually holds two values, the left- and right-hand sides of a
52
/// concatenation. We also have nullary Twine objects, which are effectively
53
/// sentinel values that represent empty strings.
55
/// Thus, a Twine can effectively have zero, one, or two children. The \see
56
/// isNullary(), \see isUnary(), and \see isBinary() predicates exist for
57
/// testing the number of children.
59
/// We maintain a number of invariants on Twine objects (FIXME: Why):
60
/// - Nullary twines are always represented with their Kind on the left-hand
61
/// side, and the Empty kind on the right-hand side.
62
/// - Unary twines are always represented with the value on the left-hand
63
/// side, and the Empty kind on the right-hand side.
64
/// - If a Twine has another Twine as a child, that child should always be
65
/// binary (otherwise it could have been folded into the parent).
67
/// These invariants are check by \see isValid().
69
/// \b Efficiency Considerations \n
71
/// The Twine is designed to yield efficient and small code for common
72
/// situations. For this reason, the concat() method is inlined so that
73
/// concatenations of leaf nodes can be optimized into stores directly into a
74
/// single stack allocated object.
76
/// In practice, not all compilers can be trusted to optimize concat() fully,
77
/// so we provide two additional methods (and accompanying operator+
78
/// overloads) to guarantee that particularly important cases (cstring plus
79
/// StringRef) codegen as desired.
81
/// NodeKind - Represent the type of an argument.
83
/// An empty string; the result of concatenating anything with it is also
90
/// A pointer to a Twine instance.
93
/// A pointer to a C string instance.
96
/// A pointer to an std::string instance.
99
/// A pointer to a StringRef instance.
102
/// A pointer to an unsigned int value, to render as an unsigned decimal
106
/// A pointer to an int value, to render as a signed decimal integer.
109
/// A pointer to an unsigned long value, to render as an unsigned decimal
113
/// A pointer to a long value, to render as a signed decimal integer.
116
/// A pointer to an unsigned long long value, to render as an unsigned
120
/// A pointer to a long long value, to render as a signed decimal integer.
123
/// A pointer to a uint64_t value, to render as an unsigned hexadecimal
129
/// LHS - The prefix in the concatenation, which may be uninitialized for
130
/// Null or Empty kinds.
132
/// RHS - The suffix in the concatenation, which may be uninitialized for
133
/// Null or Empty kinds.
135
/// LHSKind - The NodeKind of the left hand side, \see getLHSKind().
136
unsigned char LHSKind;
137
/// RHSKind - The NodeKind of the left hand side, \see getLHSKind().
138
unsigned char RHSKind;
141
/// Construct a nullary twine; the kind must be NullKind or EmptyKind.
142
explicit Twine(NodeKind Kind)
143
: LHSKind(Kind), RHSKind(EmptyKind) {
144
assert(isNullary() && "Invalid kind!");
147
/// Construct a binary twine.
148
explicit Twine(const Twine &_LHS, const Twine &_RHS)
149
: LHS(&_LHS), RHS(&_RHS), LHSKind(TwineKind), RHSKind(TwineKind) {
150
assert(isValid() && "Invalid twine!");
153
/// Construct a twine from explicit values.
154
explicit Twine(const void *_LHS, NodeKind _LHSKind,
155
const void *_RHS, NodeKind _RHSKind)
156
: LHS(_LHS), RHS(_RHS), LHSKind(_LHSKind), RHSKind(_RHSKind) {
157
assert(isValid() && "Invalid twine!");
160
/// isNull - Check for the null twine.
161
bool isNull() const {
162
return getLHSKind() == NullKind;
165
/// isEmpty - Check for the empty twine.
166
bool isEmpty() const {
167
return getLHSKind() == EmptyKind;
170
/// isNullary - Check if this is a nullary twine (null or empty).
171
bool isNullary() const {
172
return isNull() || isEmpty();
175
/// isUnary - Check if this is a unary twine.
176
bool isUnary() const {
177
return getRHSKind() == EmptyKind && !isNullary();
180
/// isBinary - Check if this is a binary twine.
181
bool isBinary() const {
182
return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
185
/// isValid - Check if this is a valid twine (satisfying the invariants on
186
/// order and number of arguments).
187
bool isValid() const {
188
// Nullary twines always have Empty on the RHS.
189
if (isNullary() && getRHSKind() != EmptyKind)
192
// Null should never appear on the RHS.
193
if (getRHSKind() == NullKind)
196
// The RHS cannot be non-empty if the LHS is empty.
197
if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
200
// A twine child should always be binary.
201
if (getLHSKind() == TwineKind &&
202
!static_cast<const Twine*>(LHS)->isBinary())
204
if (getRHSKind() == TwineKind &&
205
!static_cast<const Twine*>(RHS)->isBinary())
211
/// getLHSKind - Get the NodeKind of the left-hand side.
212
NodeKind getLHSKind() const { return (NodeKind) LHSKind; }
214
/// getRHSKind - Get the NodeKind of the left-hand side.
215
NodeKind getRHSKind() const { return (NodeKind) RHSKind; }
217
/// printOneChild - Print one child from a twine.
218
void printOneChild(raw_ostream &OS, const void *Ptr, NodeKind Kind) const;
220
/// printOneChildRepr - Print the representation of one child from a twine.
221
void printOneChildRepr(raw_ostream &OS, const void *Ptr,
222
NodeKind Kind) const;
225
/// @name Constructors
228
/// Construct from an empty string.
229
/*implicit*/ Twine() : LHSKind(EmptyKind), RHSKind(EmptyKind) {
230
assert(isValid() && "Invalid twine!");
233
/// Construct from a C string.
235
/// We take care here to optimize "" into the empty twine -- this will be
236
/// optimized out for string constants. This allows Twine arguments have
237
/// default "" values, without introducing unnecessary string constants.
238
/*implicit*/ Twine(const char *Str)
239
: RHSKind(EmptyKind) {
240
if (Str[0] != '\0') {
242
LHSKind = CStringKind;
246
assert(isValid() && "Invalid twine!");
249
/// Construct from an std::string.
250
/*implicit*/ Twine(const std::string &Str)
251
: LHS(&Str), LHSKind(StdStringKind), RHSKind(EmptyKind) {
252
assert(isValid() && "Invalid twine!");
255
/// Construct from a StringRef.
256
/*implicit*/ Twine(const StringRef &Str)
257
: LHS(&Str), LHSKind(StringRefKind), RHSKind(EmptyKind) {
258
assert(isValid() && "Invalid twine!");
261
/// Construct a twine to print \arg Val as an unsigned decimal integer.
262
explicit Twine(const unsigned int &Val)
263
: LHS(&Val), LHSKind(DecUIKind), RHSKind(EmptyKind) {
266
/// Construct a twine to print \arg Val as a signed decimal integer.
267
explicit Twine(const int &Val)
268
: LHS(&Val), LHSKind(DecIKind), RHSKind(EmptyKind) {
271
/// Construct a twine to print \arg Val as an unsigned decimal integer.
272
explicit Twine(const unsigned long &Val)
273
: LHS(&Val), LHSKind(DecULKind), RHSKind(EmptyKind) {
276
/// Construct a twine to print \arg Val as a signed decimal integer.
277
explicit Twine(const long &Val)
278
: LHS(&Val), LHSKind(DecLKind), RHSKind(EmptyKind) {
281
/// Construct a twine to print \arg Val as an unsigned decimal integer.
282
explicit Twine(const unsigned long long &Val)
283
: LHS(&Val), LHSKind(DecULLKind), RHSKind(EmptyKind) {
286
/// Construct a twine to print \arg Val as a signed decimal integer.
287
explicit Twine(const long long &Val)
288
: LHS(&Val), LHSKind(DecLLKind), RHSKind(EmptyKind) {
291
// FIXME: Unfortunately, to make sure this is as efficient as possible we
292
// need extra binary constructors from particular types. We can't rely on
293
// the compiler to be smart enough to fold operator+()/concat() down to the
296
/// Construct as the concatenation of a C string and a StringRef.
297
/*implicit*/ Twine(const char *_LHS, const StringRef &_RHS)
298
: LHS(_LHS), RHS(&_RHS), LHSKind(CStringKind), RHSKind(StringRefKind) {
299
assert(isValid() && "Invalid twine!");
302
/// Construct as the concatenation of a StringRef and a C string.
303
/*implicit*/ Twine(const StringRef &_LHS, const char *_RHS)
304
: LHS(&_LHS), RHS(_RHS), LHSKind(StringRefKind), RHSKind(CStringKind) {
305
assert(isValid() && "Invalid twine!");
308
/// Create a 'null' string, which is an empty string that always
309
/// concatenates to form another empty string.
310
static Twine createNull() {
311
return Twine(NullKind);
315
/// @name Numeric Conversions
318
// Construct a twine to print \arg Val as an unsigned hexadecimal integer.
319
static Twine utohexstr(const uint64_t &Val) {
320
return Twine(&Val, UHexKind, 0, EmptyKind);
324
/// @name Predicate Operations
327
/// isTriviallyEmpty - Check if this twine is trivially empty; a false
328
/// return value does not necessarily mean the twine is empty.
329
bool isTriviallyEmpty() const {
333
/// isSingleStringRef - Return true if this twine can be dynamically
334
/// accessed as a single StringRef value with getSingleStringRef().
335
bool isSingleStringRef() const {
336
if (getRHSKind() != EmptyKind) return false;
338
switch (getLHSKind()) {
350
/// @name String Operations
353
Twine concat(const Twine &Suffix) const;
356
/// @name Output & Conversion.
359
/// str - Return the twine contents as a std::string.
360
std::string str() const;
362
/// toVector - Write the concatenated string into the given SmallString or
364
void toVector(SmallVectorImpl<char> &Out) const;
366
/// getSingleStringRef - This returns the twine as a single StringRef. This
367
/// method is only valid if isSingleStringRef() is true.
368
StringRef getSingleStringRef() const {
369
assert(isSingleStringRef() &&"This cannot be had as a single stringref!");
370
switch (getLHSKind()) {
371
default: assert(0 && "Out of sync with isSingleStringRef");
372
case EmptyKind: return StringRef();
373
case CStringKind: return StringRef((const char*)LHS);
374
case StdStringKind: return StringRef(*(const std::string*)LHS);
375
case StringRefKind: return *(const StringRef*)LHS;
379
/// toStringRef - This returns the twine as a single StringRef if it can be
380
/// represented as such. Otherwise the twine is written into the given
381
/// SmallVector and a StringRef to the SmallVector's data is returned.
382
StringRef toStringRef(SmallVectorImpl<char> &Out) const;
384
/// print - Write the concatenated string represented by this twine to the
386
void print(raw_ostream &OS) const;
388
/// dump - Dump the concatenated string represented by this twine to stderr.
391
/// print - Write the representation of this twine to the stream \arg OS.
392
void printRepr(raw_ostream &OS) const;
394
/// dumpRepr - Dump the representation of this twine to stderr.
395
void dumpRepr() const;
400
/// @name Twine Inline Implementations
403
inline Twine Twine::concat(const Twine &Suffix) const {
404
// Concatenation with null is null.
405
if (isNull() || Suffix.isNull())
406
return Twine(NullKind);
408
// Concatenation with empty yields the other side.
411
if (Suffix.isEmpty())
414
// Otherwise we need to create a new node, taking care to fold in unary
416
const void *NewLHS = this, *NewRHS = &Suffix;
417
NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
420
NewLHSKind = getLHSKind();
422
if (Suffix.isUnary()) {
424
NewRHSKind = Suffix.getLHSKind();
427
return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
430
inline Twine operator+(const Twine &LHS, const Twine &RHS) {
431
return LHS.concat(RHS);
434
/// Additional overload to guarantee simplified codegen; this is equivalent to
437
inline Twine operator+(const char *LHS, const StringRef &RHS) {
438
return Twine(LHS, RHS);
441
/// Additional overload to guarantee simplified codegen; this is equivalent to
444
inline Twine operator+(const StringRef &LHS, const char *RHS) {
445
return Twine(LHS, RHS);
448
inline raw_ostream &operator<<(raw_ostream &OS, const Twine &RHS) {