1 //===- Twine.h - Fast Temporary String Concatenation ------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ADT_TWINE_H
10 #define LLVM_ADT_TWINE_H
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/ErrorHandling.h"
21 class formatv_object_base
;
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.
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
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.
82 enum NodeKind
: unsigned char {
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 a SmallString instance.
105 /// A pointer to a formatv_object_base instance.
108 /// A char value, to render as a character.
111 /// An unsigned int value, to render as an unsigned decimal integer.
114 /// An int value, to render as a signed decimal integer.
117 /// A pointer to an unsigned long value, to render as an unsigned decimal
121 /// A pointer to a long value, to render as a signed decimal integer.
124 /// A pointer to an unsigned long long value, to render as an unsigned
128 /// A pointer to a long long value, to render as a signed decimal integer.
131 /// A pointer to a uint64_t value, to render as an unsigned hexadecimal
140 const std::string
*stdString
;
141 const StringRef
*stringRef
;
142 const SmallVectorImpl
<char> *smallString
;
143 const formatv_object_base
*formatvObject
;
147 const unsigned long *decUL
;
149 const unsigned long long *decULL
;
150 const long long *decLL
;
151 const uint64_t *uHex
;
154 /// LHS - The prefix in the concatenation, which may be uninitialized for
155 /// Null or Empty kinds.
158 /// RHS - The suffix in the concatenation, which may be uninitialized for
159 /// Null or Empty kinds.
162 /// LHSKind - The NodeKind of the left hand side, \see getLHSKind().
163 NodeKind LHSKind
= EmptyKind
;
165 /// RHSKind - The NodeKind of the right hand side, \see getRHSKind().
166 NodeKind RHSKind
= EmptyKind
;
168 /// Construct a nullary twine; the kind must be NullKind or EmptyKind.
169 explicit Twine(NodeKind Kind
) : LHSKind(Kind
) {
170 assert(isNullary() && "Invalid kind!");
173 /// Construct a binary twine.
174 explicit Twine(const Twine
&LHS
, const Twine
&RHS
)
175 : LHSKind(TwineKind
), RHSKind(TwineKind
) {
176 this->LHS
.twine
= &LHS
;
177 this->RHS
.twine
= &RHS
;
178 assert(isValid() && "Invalid twine!");
181 /// Construct a twine from explicit values.
182 explicit Twine(Child LHS
, NodeKind LHSKind
, Child RHS
, NodeKind RHSKind
)
183 : LHS(LHS
), RHS(RHS
), LHSKind(LHSKind
), RHSKind(RHSKind
) {
184 assert(isValid() && "Invalid twine!");
187 /// Check for the null twine.
188 bool isNull() const {
189 return getLHSKind() == NullKind
;
192 /// Check for the empty twine.
193 bool isEmpty() const {
194 return getLHSKind() == EmptyKind
;
197 /// Check if this is a nullary twine (null or empty).
198 bool isNullary() const {
199 return isNull() || isEmpty();
202 /// Check if this is a unary twine.
203 bool isUnary() const {
204 return getRHSKind() == EmptyKind
&& !isNullary();
207 /// Check if this is a binary twine.
208 bool isBinary() const {
209 return getLHSKind() != NullKind
&& getRHSKind() != EmptyKind
;
212 /// Check if this is a valid twine (satisfying the invariants on
213 /// order and number of arguments).
214 bool isValid() const {
215 // Nullary twines always have Empty on the RHS.
216 if (isNullary() && getRHSKind() != EmptyKind
)
219 // Null should never appear on the RHS.
220 if (getRHSKind() == NullKind
)
223 // The RHS cannot be non-empty if the LHS is empty.
224 if (getRHSKind() != EmptyKind
&& getLHSKind() == EmptyKind
)
227 // A twine child should always be binary.
228 if (getLHSKind() == TwineKind
&&
229 !LHS
.twine
->isBinary())
231 if (getRHSKind() == TwineKind
&&
232 !RHS
.twine
->isBinary())
238 /// Get the NodeKind of the left-hand side.
239 NodeKind
getLHSKind() const { return LHSKind
; }
241 /// Get the NodeKind of the right-hand side.
242 NodeKind
getRHSKind() const { return RHSKind
; }
244 /// Print one child from a twine.
245 void printOneChild(raw_ostream
&OS
, Child Ptr
, NodeKind Kind
) const;
247 /// Print the representation of one child from a twine.
248 void printOneChildRepr(raw_ostream
&OS
, Child Ptr
,
249 NodeKind Kind
) const;
252 /// @name Constructors
255 /// Construct from an empty string.
256 /*implicit*/ Twine() {
257 assert(isValid() && "Invalid twine!");
260 Twine(const Twine
&) = default;
262 /// Construct from a C string.
264 /// We take care here to optimize "" into the empty twine -- this will be
265 /// optimized out for string constants. This allows Twine arguments have
266 /// default "" values, without introducing unnecessary string constants.
267 /*implicit*/ Twine(const char *Str
) {
268 if (Str
[0] != '\0') {
270 LHSKind
= CStringKind
;
274 assert(isValid() && "Invalid twine!");
276 /// Delete the implicit conversion from nullptr as Twine(const char *)
277 /// cannot take nullptr.
278 /*implicit*/ Twine(std::nullptr_t
) = delete;
280 /// Construct from an std::string.
281 /*implicit*/ Twine(const std::string
&Str
) : LHSKind(StdStringKind
) {
282 LHS
.stdString
= &Str
;
283 assert(isValid() && "Invalid twine!");
286 /// Construct from a StringRef.
287 /*implicit*/ Twine(const StringRef
&Str
) : LHSKind(StringRefKind
) {
288 LHS
.stringRef
= &Str
;
289 assert(isValid() && "Invalid twine!");
292 /// Construct from a SmallString.
293 /*implicit*/ Twine(const SmallVectorImpl
<char> &Str
)
294 : LHSKind(SmallStringKind
) {
295 LHS
.smallString
= &Str
;
296 assert(isValid() && "Invalid twine!");
299 /// Construct from a formatv_object_base.
300 /*implicit*/ Twine(const formatv_object_base
&Fmt
)
301 : LHSKind(FormatvObjectKind
) {
302 LHS
.formatvObject
= &Fmt
;
303 assert(isValid() && "Invalid twine!");
306 /// Construct from a char.
307 explicit Twine(char Val
) : LHSKind(CharKind
) {
311 /// Construct from a signed char.
312 explicit Twine(signed char Val
) : LHSKind(CharKind
) {
313 LHS
.character
= static_cast<char>(Val
);
316 /// Construct from an unsigned char.
317 explicit Twine(unsigned char Val
) : LHSKind(CharKind
) {
318 LHS
.character
= static_cast<char>(Val
);
321 /// Construct a twine to print \p Val as an unsigned decimal integer.
322 explicit Twine(unsigned Val
) : LHSKind(DecUIKind
) {
326 /// Construct a twine to print \p Val as a signed decimal integer.
327 explicit Twine(int Val
) : LHSKind(DecIKind
) {
331 /// Construct a twine to print \p Val as an unsigned decimal integer.
332 explicit Twine(const unsigned long &Val
) : LHSKind(DecULKind
) {
336 /// Construct a twine to print \p Val as a signed decimal integer.
337 explicit Twine(const long &Val
) : LHSKind(DecLKind
) {
341 /// Construct a twine to print \p Val as an unsigned decimal integer.
342 explicit Twine(const unsigned long long &Val
) : LHSKind(DecULLKind
) {
346 /// Construct a twine to print \p Val as a signed decimal integer.
347 explicit Twine(const long long &Val
) : LHSKind(DecLLKind
) {
351 // FIXME: Unfortunately, to make sure this is as efficient as possible we
352 // need extra binary constructors from particular types. We can't rely on
353 // the compiler to be smart enough to fold operator+()/concat() down to the
356 /// Construct as the concatenation of a C string and a StringRef.
357 /*implicit*/ Twine(const char *LHS
, const StringRef
&RHS
)
358 : LHSKind(CStringKind
), RHSKind(StringRefKind
) {
359 this->LHS
.cString
= LHS
;
360 this->RHS
.stringRef
= &RHS
;
361 assert(isValid() && "Invalid twine!");
364 /// Construct as the concatenation of a StringRef and a C string.
365 /*implicit*/ Twine(const StringRef
&LHS
, const char *RHS
)
366 : LHSKind(StringRefKind
), RHSKind(CStringKind
) {
367 this->LHS
.stringRef
= &LHS
;
368 this->RHS
.cString
= RHS
;
369 assert(isValid() && "Invalid twine!");
372 /// Since the intended use of twines is as temporary objects, assignments
373 /// when concatenating might cause undefined behavior or stack corruptions
374 Twine
&operator=(const Twine
&) = delete;
376 /// Create a 'null' string, which is an empty string that always
377 /// concatenates to form another empty string.
378 static Twine
createNull() {
379 return Twine(NullKind
);
383 /// @name Numeric Conversions
386 // Construct a twine to print \p Val as an unsigned hexadecimal integer.
387 static Twine
utohexstr(const uint64_t &Val
) {
391 return Twine(LHS
, UHexKind
, RHS
, EmptyKind
);
395 /// @name Predicate Operations
398 /// Check if this twine is trivially empty; a false return value does not
399 /// necessarily mean the twine is empty.
400 bool isTriviallyEmpty() const {
404 /// Return true if this twine can be dynamically accessed as a single
405 /// StringRef value with getSingleStringRef().
406 bool isSingleStringRef() const {
407 if (getRHSKind() != EmptyKind
) return false;
409 switch (getLHSKind()) {
414 case SmallStringKind
:
422 /// @name String Operations
425 Twine
concat(const Twine
&Suffix
) const;
428 /// @name Output & Conversion.
431 /// Return the twine contents as a std::string.
432 std::string
str() const;
434 /// Append the concatenated string into the given SmallString or SmallVector.
435 void toVector(SmallVectorImpl
<char> &Out
) const;
437 /// This returns the twine as a single StringRef. This method is only valid
438 /// if isSingleStringRef() is true.
439 StringRef
getSingleStringRef() const {
440 assert(isSingleStringRef() &&"This cannot be had as a single stringref!");
441 switch (getLHSKind()) {
442 default: llvm_unreachable("Out of sync with isSingleStringRef");
443 case EmptyKind
: return StringRef();
444 case CStringKind
: return StringRef(LHS
.cString
);
445 case StdStringKind
: return StringRef(*LHS
.stdString
);
446 case StringRefKind
: return *LHS
.stringRef
;
447 case SmallStringKind
:
448 return StringRef(LHS
.smallString
->data(), LHS
.smallString
->size());
452 /// This returns the twine as a single StringRef if it can be
453 /// represented as such. Otherwise the twine is written into the given
454 /// SmallVector and a StringRef to the SmallVector's data is returned.
455 StringRef
toStringRef(SmallVectorImpl
<char> &Out
) const {
456 if (isSingleStringRef())
457 return getSingleStringRef();
459 return StringRef(Out
.data(), Out
.size());
462 /// This returns the twine as a single null terminated StringRef if it
463 /// can be represented as such. Otherwise the twine is written into the
464 /// given SmallVector and a StringRef to the SmallVector's data is returned.
466 /// The returned StringRef's size does not include the null terminator.
467 StringRef
toNullTerminatedStringRef(SmallVectorImpl
<char> &Out
) const;
469 /// Write the concatenated string represented by this twine to the
471 void print(raw_ostream
&OS
) const;
473 /// Dump the concatenated string represented by this twine to stderr.
476 /// Write the representation of this twine to the stream \p OS.
477 void printRepr(raw_ostream
&OS
) const;
479 /// Dump the representation of this twine to stderr.
480 void dumpRepr() const;
485 /// @name Twine Inline Implementations
488 inline Twine
Twine::concat(const Twine
&Suffix
) const {
489 // Concatenation with null is null.
490 if (isNull() || Suffix
.isNull())
491 return Twine(NullKind
);
493 // Concatenation with empty yields the other side.
496 if (Suffix
.isEmpty())
499 // Otherwise we need to create a new node, taking care to fold in unary
501 Child NewLHS
, NewRHS
;
503 NewRHS
.twine
= &Suffix
;
504 NodeKind NewLHSKind
= TwineKind
, NewRHSKind
= TwineKind
;
507 NewLHSKind
= getLHSKind();
509 if (Suffix
.isUnary()) {
511 NewRHSKind
= Suffix
.getLHSKind();
514 return Twine(NewLHS
, NewLHSKind
, NewRHS
, NewRHSKind
);
517 inline Twine
operator+(const Twine
&LHS
, const Twine
&RHS
) {
518 return LHS
.concat(RHS
);
521 /// Additional overload to guarantee simplified codegen; this is equivalent to
524 inline Twine
operator+(const char *LHS
, const StringRef
&RHS
) {
525 return Twine(LHS
, RHS
);
528 /// Additional overload to guarantee simplified codegen; this is equivalent to
531 inline Twine
operator+(const StringRef
&LHS
, const char *RHS
) {
532 return Twine(LHS
, RHS
);
535 inline raw_ostream
&operator<<(raw_ostream
&OS
, const Twine
&RHS
) {
542 } // end namespace llvm
544 #endif // LLVM_ADT_TWINE_H