1198090Srdivacky//===-- Twine.h - Fast Temporary String Concatenation -----------*- C++ -*-===//
2198090Srdivacky//
3198090Srdivacky//                     The LLVM Compiler Infrastructure
4198090Srdivacky//
5198090Srdivacky// This file is distributed under the University of Illinois Open Source
6198090Srdivacky// License. See LICENSE.TXT for details.
7198090Srdivacky//
8198090Srdivacky//===----------------------------------------------------------------------===//
9198090Srdivacky
10198090Srdivacky#ifndef LLVM_ADT_TWINE_H
11198090Srdivacky#define LLVM_ADT_TWINE_H
12198090Srdivacky
13198090Srdivacky#include "llvm/ADT/StringRef.h"
14218893Sdim#include "llvm/Support/DataTypes.h"
15235633Sdim#include "llvm/Support/ErrorHandling.h"
16198090Srdivacky#include <cassert>
17198090Srdivacky#include <string>
18198090Srdivacky
19198090Srdivackynamespace llvm {
20198090Srdivacky  template <typename T>
21198090Srdivacky  class SmallVectorImpl;
22198090Srdivacky  class StringRef;
23198090Srdivacky  class raw_ostream;
24198090Srdivacky
25198090Srdivacky  /// Twine - A lightweight data structure for efficiently representing the
26198090Srdivacky  /// concatenation of temporary values as strings.
27198090Srdivacky  ///
28198090Srdivacky  /// A Twine is a kind of rope, it represents a concatenated string using a
29198090Srdivacky  /// binary-tree, where the string is the preorder of the nodes. Since the
30198090Srdivacky  /// Twine can be efficiently rendered into a buffer when its result is used,
31198090Srdivacky  /// it avoids the cost of generating temporary values for intermediate string
32198090Srdivacky  /// results -- particularly in cases when the Twine result is never
33198090Srdivacky  /// required. By explicitly tracking the type of leaf nodes, we can also avoid
34198090Srdivacky  /// the creation of temporary strings for conversions operations (such as
35198090Srdivacky  /// appending an integer to a string).
36198090Srdivacky  ///
37198090Srdivacky  /// A Twine is not intended for use directly and should not be stored, its
38198090Srdivacky  /// implementation relies on the ability to store pointers to temporary stack
39198090Srdivacky  /// objects which may be deallocated at the end of a statement. Twines should
40198090Srdivacky  /// only be used accepted as const references in arguments, when an API wishes
41198090Srdivacky  /// to accept possibly-concatenated strings.
42198090Srdivacky  ///
43198090Srdivacky  /// Twines support a special 'null' value, which always concatenates to form
44198090Srdivacky  /// itself, and renders as an empty string. This can be returned from APIs to
45198090Srdivacky  /// effectively nullify any concatenations performed on the result.
46218893Sdim  ///
47245431Sdim  /// \b Implementation
48198090Srdivacky  ///
49198090Srdivacky  /// Given the nature of a Twine, it is not possible for the Twine's
50198090Srdivacky  /// concatenation method to construct interior nodes; the result must be
51198090Srdivacky  /// represented inside the returned value. For this reason a Twine object
52198090Srdivacky  /// actually holds two values, the left- and right-hand sides of a
53198090Srdivacky  /// concatenation. We also have nullary Twine objects, which are effectively
54198090Srdivacky  /// sentinel values that represent empty strings.
55198090Srdivacky  ///
56198090Srdivacky  /// Thus, a Twine can effectively have zero, one, or two children. The \see
57198090Srdivacky  /// isNullary(), \see isUnary(), and \see isBinary() predicates exist for
58198090Srdivacky  /// testing the number of children.
59198090Srdivacky  ///
60198090Srdivacky  /// We maintain a number of invariants on Twine objects (FIXME: Why):
61198090Srdivacky  ///  - Nullary twines are always represented with their Kind on the left-hand
62198090Srdivacky  ///    side, and the Empty kind on the right-hand side.
63198090Srdivacky  ///  - Unary twines are always represented with the value on the left-hand
64198090Srdivacky  ///    side, and the Empty kind on the right-hand side.
65198090Srdivacky  ///  - If a Twine has another Twine as a child, that child should always be
66198090Srdivacky  ///    binary (otherwise it could have been folded into the parent).
67198090Srdivacky  ///
68198090Srdivacky  /// These invariants are check by \see isValid().
69198090Srdivacky  ///
70245431Sdim  /// \b Efficiency Considerations
71198090Srdivacky  ///
72198090Srdivacky  /// The Twine is designed to yield efficient and small code for common
73198090Srdivacky  /// situations. For this reason, the concat() method is inlined so that
74198090Srdivacky  /// concatenations of leaf nodes can be optimized into stores directly into a
75198090Srdivacky  /// single stack allocated object.
76198090Srdivacky  ///
77198090Srdivacky  /// In practice, not all compilers can be trusted to optimize concat() fully,
78198090Srdivacky  /// so we provide two additional methods (and accompanying operator+
79198090Srdivacky  /// overloads) to guarantee that particularly important cases (cstring plus
80198090Srdivacky  /// StringRef) codegen as desired.
81198090Srdivacky  class Twine {
82198090Srdivacky    /// NodeKind - Represent the type of an argument.
83198090Srdivacky    enum NodeKind {
84198090Srdivacky      /// An empty string; the result of concatenating anything with it is also
85198090Srdivacky      /// empty.
86198090Srdivacky      NullKind,
87198090Srdivacky
88198090Srdivacky      /// The empty string.
89198090Srdivacky      EmptyKind,
90198090Srdivacky
91198090Srdivacky      /// A pointer to a Twine instance.
92198090Srdivacky      TwineKind,
93198090Srdivacky
94198090Srdivacky      /// A pointer to a C string instance.
95198090Srdivacky      CStringKind,
96198090Srdivacky
97198090Srdivacky      /// A pointer to an std::string instance.
98198090Srdivacky      StdStringKind,
99198090Srdivacky
100198090Srdivacky      /// A pointer to a StringRef instance.
101198090Srdivacky      StringRefKind,
102198090Srdivacky
103226890Sdim      /// A char value reinterpreted as a pointer, to render as a character.
104226890Sdim      CharKind,
105226890Sdim
106218893Sdim      /// An unsigned int value reinterpreted as a pointer, to render as an
107208599Srdivacky      /// unsigned decimal integer.
108198090Srdivacky      DecUIKind,
109198090Srdivacky
110208599Srdivacky      /// An int value reinterpreted as a pointer, to render as a signed
111208599Srdivacky      /// decimal integer.
112198090Srdivacky      DecIKind,
113198090Srdivacky
114198090Srdivacky      /// A pointer to an unsigned long value, to render as an unsigned decimal
115198090Srdivacky      /// integer.
116198090Srdivacky      DecULKind,
117198090Srdivacky
118198090Srdivacky      /// A pointer to a long value, to render as a signed decimal integer.
119198090Srdivacky      DecLKind,
120198090Srdivacky
121198090Srdivacky      /// A pointer to an unsigned long long value, to render as an unsigned
122198090Srdivacky      /// decimal integer.
123198090Srdivacky      DecULLKind,
124198090Srdivacky
125198090Srdivacky      /// A pointer to a long long value, to render as a signed decimal integer.
126198090Srdivacky      DecLLKind,
127198090Srdivacky
128198090Srdivacky      /// A pointer to a uint64_t value, to render as an unsigned hexadecimal
129198090Srdivacky      /// integer.
130198090Srdivacky      UHexKind
131198090Srdivacky    };
132198090Srdivacky
133226890Sdim    union Child
134226890Sdim    {
135226890Sdim      const Twine *twine;
136226890Sdim      const char *cString;
137226890Sdim      const std::string *stdString;
138226890Sdim      const StringRef *stringRef;
139226890Sdim      char character;
140226890Sdim      unsigned int decUI;
141226890Sdim      int decI;
142226890Sdim      const unsigned long *decUL;
143226890Sdim      const long *decL;
144226890Sdim      const unsigned long long *decULL;
145226890Sdim      const long long *decLL;
146226890Sdim      const uint64_t *uHex;
147226890Sdim    };
148226890Sdim
149198090Srdivacky  private:
150198090Srdivacky    /// LHS - The prefix in the concatenation, which may be uninitialized for
151198090Srdivacky    /// Null or Empty kinds.
152226890Sdim    Child LHS;
153198090Srdivacky    /// RHS - The suffix in the concatenation, which may be uninitialized for
154198090Srdivacky    /// Null or Empty kinds.
155226890Sdim    Child RHS;
156226890Sdim    // enums stored as unsigned chars to save on space while some compilers
157226890Sdim    // don't support specifying the backing type for an enum
158198090Srdivacky    /// LHSKind - The NodeKind of the left hand side, \see getLHSKind().
159199511Srdivacky    unsigned char LHSKind;
160198090Srdivacky    /// RHSKind - The NodeKind of the left hand side, \see getLHSKind().
161199511Srdivacky    unsigned char RHSKind;
162198090Srdivacky
163198090Srdivacky  private:
164198090Srdivacky    /// Construct a nullary twine; the kind must be NullKind or EmptyKind.
165198090Srdivacky    explicit Twine(NodeKind Kind)
166198090Srdivacky      : LHSKind(Kind), RHSKind(EmptyKind) {
167198090Srdivacky      assert(isNullary() && "Invalid kind!");
168198090Srdivacky    }
169198090Srdivacky
170198090Srdivacky    /// Construct a binary twine.
171198090Srdivacky    explicit Twine(const Twine &_LHS, const Twine &_RHS)
172226890Sdim      : LHSKind(TwineKind), RHSKind(TwineKind) {
173226890Sdim      LHS.twine = &_LHS;
174226890Sdim      RHS.twine = &_RHS;
175198090Srdivacky      assert(isValid() && "Invalid twine!");
176198090Srdivacky    }
177198090Srdivacky
178198090Srdivacky    /// Construct a twine from explicit values.
179226890Sdim    explicit Twine(Child _LHS, NodeKind _LHSKind,
180226890Sdim                   Child _RHS, NodeKind _RHSKind)
181198090Srdivacky      : LHS(_LHS), RHS(_RHS), LHSKind(_LHSKind), RHSKind(_RHSKind) {
182198090Srdivacky      assert(isValid() && "Invalid twine!");
183198090Srdivacky    }
184198090Srdivacky
185198090Srdivacky    /// isNull - Check for the null twine.
186198090Srdivacky    bool isNull() const {
187198090Srdivacky      return getLHSKind() == NullKind;
188198090Srdivacky    }
189198090Srdivacky
190198090Srdivacky    /// isEmpty - Check for the empty twine.
191198090Srdivacky    bool isEmpty() const {
192198090Srdivacky      return getLHSKind() == EmptyKind;
193198090Srdivacky    }
194198090Srdivacky
195198090Srdivacky    /// isNullary - Check if this is a nullary twine (null or empty).
196198090Srdivacky    bool isNullary() const {
197198090Srdivacky      return isNull() || isEmpty();
198198090Srdivacky    }
199198090Srdivacky
200198090Srdivacky    /// isUnary - Check if this is a unary twine.
201198090Srdivacky    bool isUnary() const {
202198090Srdivacky      return getRHSKind() == EmptyKind && !isNullary();
203198090Srdivacky    }
204198090Srdivacky
205198090Srdivacky    /// isBinary - Check if this is a binary twine.
206198090Srdivacky    bool isBinary() const {
207198090Srdivacky      return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
208198090Srdivacky    }
209198090Srdivacky
210198090Srdivacky    /// isValid - Check if this is a valid twine (satisfying the invariants on
211198090Srdivacky    /// order and number of arguments).
212198090Srdivacky    bool isValid() const {
213198090Srdivacky      // Nullary twines always have Empty on the RHS.
214198090Srdivacky      if (isNullary() && getRHSKind() != EmptyKind)
215198090Srdivacky        return false;
216198090Srdivacky
217198090Srdivacky      // Null should never appear on the RHS.
218198090Srdivacky      if (getRHSKind() == NullKind)
219198090Srdivacky        return false;
220198090Srdivacky
221198090Srdivacky      // The RHS cannot be non-empty if the LHS is empty.
222198090Srdivacky      if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
223198090Srdivacky        return false;
224198090Srdivacky
225198090Srdivacky      // A twine child should always be binary.
226198090Srdivacky      if (getLHSKind() == TwineKind &&
227226890Sdim          !LHS.twine->isBinary())
228198090Srdivacky        return false;
229198090Srdivacky      if (getRHSKind() == TwineKind &&
230226890Sdim          !RHS.twine->isBinary())
231198090Srdivacky        return false;
232198090Srdivacky
233198090Srdivacky      return true;
234198090Srdivacky    }
235198090Srdivacky
236198090Srdivacky    /// getLHSKind - Get the NodeKind of the left-hand side.
237199511Srdivacky    NodeKind getLHSKind() const { return (NodeKind) LHSKind; }
238198090Srdivacky
239252723Sdim    /// getRHSKind - Get the NodeKind of the right-hand side.
240199511Srdivacky    NodeKind getRHSKind() const { return (NodeKind) RHSKind; }
241198090Srdivacky
242198090Srdivacky    /// printOneChild - Print one child from a twine.
243226890Sdim    void printOneChild(raw_ostream &OS, Child Ptr, NodeKind Kind) const;
244198090Srdivacky
245198090Srdivacky    /// printOneChildRepr - Print the representation of one child from a twine.
246226890Sdim    void printOneChildRepr(raw_ostream &OS, Child Ptr,
247198090Srdivacky                           NodeKind Kind) const;
248198090Srdivacky
249198090Srdivacky  public:
250198090Srdivacky    /// @name Constructors
251198090Srdivacky    /// @{
252198090Srdivacky
253198090Srdivacky    /// Construct from an empty string.
254198090Srdivacky    /*implicit*/ Twine() : LHSKind(EmptyKind), RHSKind(EmptyKind) {
255198090Srdivacky      assert(isValid() && "Invalid twine!");
256198090Srdivacky    }
257198090Srdivacky
258198090Srdivacky    /// Construct from a C string.
259198090Srdivacky    ///
260198090Srdivacky    /// We take care here to optimize "" into the empty twine -- this will be
261198090Srdivacky    /// optimized out for string constants. This allows Twine arguments have
262198090Srdivacky    /// default "" values, without introducing unnecessary string constants.
263198090Srdivacky    /*implicit*/ Twine(const char *Str)
264198090Srdivacky      : RHSKind(EmptyKind) {
265198090Srdivacky      if (Str[0] != '\0') {
266226890Sdim        LHS.cString = Str;
267198090Srdivacky        LHSKind = CStringKind;
268198090Srdivacky      } else
269198090Srdivacky        LHSKind = EmptyKind;
270198090Srdivacky
271198090Srdivacky      assert(isValid() && "Invalid twine!");
272198090Srdivacky    }
273198090Srdivacky
274198090Srdivacky    /// Construct from an std::string.
275198090Srdivacky    /*implicit*/ Twine(const std::string &Str)
276226890Sdim      : LHSKind(StdStringKind), RHSKind(EmptyKind) {
277226890Sdim      LHS.stdString = &Str;
278198090Srdivacky      assert(isValid() && "Invalid twine!");
279198090Srdivacky    }
280198090Srdivacky
281198090Srdivacky    /// Construct from a StringRef.
282198090Srdivacky    /*implicit*/ Twine(const StringRef &Str)
283226890Sdim      : LHSKind(StringRefKind), RHSKind(EmptyKind) {
284226890Sdim      LHS.stringRef = &Str;
285198090Srdivacky      assert(isValid() && "Invalid twine!");
286198090Srdivacky    }
287198090Srdivacky
288226890Sdim    /// Construct from a char.
289226890Sdim    explicit Twine(char Val)
290226890Sdim      : LHSKind(CharKind), RHSKind(EmptyKind) {
291226890Sdim      LHS.character = Val;
292226890Sdim    }
293226890Sdim
294226890Sdim    /// Construct from a signed char.
295226890Sdim    explicit Twine(signed char Val)
296226890Sdim      : LHSKind(CharKind), RHSKind(EmptyKind) {
297226890Sdim      LHS.character = static_cast<char>(Val);
298226890Sdim    }
299226890Sdim
300226890Sdim    /// Construct from an unsigned char.
301226890Sdim    explicit Twine(unsigned char Val)
302226890Sdim      : LHSKind(CharKind), RHSKind(EmptyKind) {
303226890Sdim      LHS.character = static_cast<char>(Val);
304226890Sdim    }
305226890Sdim
306245431Sdim    /// Construct a twine to print \p Val as an unsigned decimal integer.
307218893Sdim    explicit Twine(unsigned Val)
308226890Sdim      : LHSKind(DecUIKind), RHSKind(EmptyKind) {
309226890Sdim      LHS.decUI = Val;
310198090Srdivacky    }
311198090Srdivacky
312245431Sdim    /// Construct a twine to print \p Val as a signed decimal integer.
313218893Sdim    explicit Twine(int Val)
314226890Sdim      : LHSKind(DecIKind), RHSKind(EmptyKind) {
315226890Sdim      LHS.decI = Val;
316198090Srdivacky    }
317198090Srdivacky
318245431Sdim    /// Construct a twine to print \p Val as an unsigned decimal integer.
319218893Sdim    explicit Twine(const unsigned long &Val)
320226890Sdim      : LHSKind(DecULKind), RHSKind(EmptyKind) {
321226890Sdim      LHS.decUL = &Val;
322198090Srdivacky    }
323198090Srdivacky
324245431Sdim    /// Construct a twine to print \p Val as a signed decimal integer.
325218893Sdim    explicit Twine(const long &Val)
326226890Sdim      : LHSKind(DecLKind), RHSKind(EmptyKind) {
327226890Sdim      LHS.decL = &Val;
328198090Srdivacky    }
329198090Srdivacky
330245431Sdim    /// Construct a twine to print \p Val as an unsigned decimal integer.
331218893Sdim    explicit Twine(const unsigned long long &Val)
332226890Sdim      : LHSKind(DecULLKind), RHSKind(EmptyKind) {
333226890Sdim      LHS.decULL = &Val;
334198090Srdivacky    }
335198090Srdivacky
336245431Sdim    /// Construct a twine to print \p Val as a signed decimal integer.
337218893Sdim    explicit Twine(const long long &Val)
338226890Sdim      : LHSKind(DecLLKind), RHSKind(EmptyKind) {
339226890Sdim      LHS.decLL = &Val;
340198090Srdivacky    }
341198090Srdivacky
342198090Srdivacky    // FIXME: Unfortunately, to make sure this is as efficient as possible we
343198090Srdivacky    // need extra binary constructors from particular types. We can't rely on
344198090Srdivacky    // the compiler to be smart enough to fold operator+()/concat() down to the
345198090Srdivacky    // right thing. Yet.
346198090Srdivacky
347198090Srdivacky    /// Construct as the concatenation of a C string and a StringRef.
348198090Srdivacky    /*implicit*/ Twine(const char *_LHS, const StringRef &_RHS)
349226890Sdim      : LHSKind(CStringKind), RHSKind(StringRefKind) {
350226890Sdim      LHS.cString = _LHS;
351226890Sdim      RHS.stringRef = &_RHS;
352198090Srdivacky      assert(isValid() && "Invalid twine!");
353198090Srdivacky    }
354198090Srdivacky
355198090Srdivacky    /// Construct as the concatenation of a StringRef and a C string.
356198090Srdivacky    /*implicit*/ Twine(const StringRef &_LHS, const char *_RHS)
357226890Sdim      : LHSKind(StringRefKind), RHSKind(CStringKind) {
358226890Sdim      LHS.stringRef = &_LHS;
359226890Sdim      RHS.cString = _RHS;
360198090Srdivacky      assert(isValid() && "Invalid twine!");
361198090Srdivacky    }
362198090Srdivacky
363198090Srdivacky    /// Create a 'null' string, which is an empty string that always
364198090Srdivacky    /// concatenates to form another empty string.
365198090Srdivacky    static Twine createNull() {
366198090Srdivacky      return Twine(NullKind);
367198090Srdivacky    }
368198090Srdivacky
369198090Srdivacky    /// @}
370198090Srdivacky    /// @name Numeric Conversions
371198090Srdivacky    /// @{
372198090Srdivacky
373245431Sdim    // Construct a twine to print \p Val as an unsigned hexadecimal integer.
374198090Srdivacky    static Twine utohexstr(const uint64_t &Val) {
375226890Sdim      Child LHS, RHS;
376226890Sdim      LHS.uHex = &Val;
377226890Sdim      RHS.twine = 0;
378226890Sdim      return Twine(LHS, UHexKind, RHS, EmptyKind);
379198090Srdivacky    }
380198090Srdivacky
381198090Srdivacky    /// @}
382198090Srdivacky    /// @name Predicate Operations
383198090Srdivacky    /// @{
384198090Srdivacky
385198090Srdivacky    /// isTriviallyEmpty - Check if this twine is trivially empty; a false
386198090Srdivacky    /// return value does not necessarily mean the twine is empty.
387198090Srdivacky    bool isTriviallyEmpty() const {
388198090Srdivacky      return isNullary();
389198090Srdivacky    }
390218893Sdim
391202375Srdivacky    /// isSingleStringRef - Return true if this twine can be dynamically
392202375Srdivacky    /// accessed as a single StringRef value with getSingleStringRef().
393202375Srdivacky    bool isSingleStringRef() const {
394202375Srdivacky      if (getRHSKind() != EmptyKind) return false;
395218893Sdim
396202375Srdivacky      switch (getLHSKind()) {
397202375Srdivacky      case EmptyKind:
398202375Srdivacky      case CStringKind:
399202375Srdivacky      case StdStringKind:
400202375Srdivacky      case StringRefKind:
401202375Srdivacky        return true;
402202375Srdivacky      default:
403202375Srdivacky        return false;
404202375Srdivacky      }
405202375Srdivacky    }
406198090Srdivacky
407198090Srdivacky    /// @}
408198090Srdivacky    /// @name String Operations
409198090Srdivacky    /// @{
410198090Srdivacky
411198090Srdivacky    Twine concat(const Twine &Suffix) const;
412198090Srdivacky
413198090Srdivacky    /// @}
414198090Srdivacky    /// @name Output & Conversion.
415198090Srdivacky    /// @{
416198090Srdivacky
417198090Srdivacky    /// str - Return the twine contents as a std::string.
418198090Srdivacky    std::string str() const;
419198090Srdivacky
420198090Srdivacky    /// toVector - Write the concatenated string into the given SmallString or
421198090Srdivacky    /// SmallVector.
422198090Srdivacky    void toVector(SmallVectorImpl<char> &Out) const;
423198090Srdivacky
424202375Srdivacky    /// getSingleStringRef - This returns the twine as a single StringRef.  This
425202375Srdivacky    /// method is only valid if isSingleStringRef() is true.
426202375Srdivacky    StringRef getSingleStringRef() const {
427202375Srdivacky      assert(isSingleStringRef() &&"This cannot be had as a single stringref!");
428202375Srdivacky      switch (getLHSKind()) {
429235633Sdim      default: llvm_unreachable("Out of sync with isSingleStringRef");
430202375Srdivacky      case EmptyKind:      return StringRef();
431226890Sdim      case CStringKind:    return StringRef(LHS.cString);
432226890Sdim      case StdStringKind:  return StringRef(*LHS.stdString);
433226890Sdim      case StringRefKind:  return *LHS.stringRef;
434202375Srdivacky      }
435202375Srdivacky    }
436202375Srdivacky
437202375Srdivacky    /// toStringRef - This returns the twine as a single StringRef if it can be
438202375Srdivacky    /// represented as such. Otherwise the twine is written into the given
439202375Srdivacky    /// SmallVector and a StringRef to the SmallVector's data is returned.
440202375Srdivacky    StringRef toStringRef(SmallVectorImpl<char> &Out) const;
441202375Srdivacky
442218893Sdim    /// toNullTerminatedStringRef - This returns the twine as a single null
443218893Sdim    /// terminated StringRef if it can be represented as such. Otherwise the
444218893Sdim    /// twine is written into the given SmallVector and a StringRef to the
445218893Sdim    /// SmallVector's data is returned.
446218893Sdim    ///
447218893Sdim    /// The returned StringRef's size does not include the null terminator.
448218893Sdim    StringRef toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const;
449218893Sdim
450245431Sdim    /// Write the concatenated string represented by this twine to the
451245431Sdim    /// stream \p OS.
452198090Srdivacky    void print(raw_ostream &OS) const;
453198090Srdivacky
454245431Sdim    /// Dump the concatenated string represented by this twine to stderr.
455198090Srdivacky    void dump() const;
456198090Srdivacky
457245431Sdim    /// Write the representation of this twine to the stream \p OS.
458198090Srdivacky    void printRepr(raw_ostream &OS) const;
459198090Srdivacky
460245431Sdim    /// Dump the representation of this twine to stderr.
461198090Srdivacky    void dumpRepr() const;
462198090Srdivacky
463198090Srdivacky    /// @}
464198090Srdivacky  };
465198090Srdivacky
466198090Srdivacky  /// @name Twine Inline Implementations
467198090Srdivacky  /// @{
468198090Srdivacky
469198090Srdivacky  inline Twine Twine::concat(const Twine &Suffix) const {
470198090Srdivacky    // Concatenation with null is null.
471198090Srdivacky    if (isNull() || Suffix.isNull())
472198090Srdivacky      return Twine(NullKind);
473198090Srdivacky
474198090Srdivacky    // Concatenation with empty yields the other side.
475198090Srdivacky    if (isEmpty())
476198090Srdivacky      return Suffix;
477198090Srdivacky    if (Suffix.isEmpty())
478198090Srdivacky      return *this;
479198090Srdivacky
480198090Srdivacky    // Otherwise we need to create a new node, taking care to fold in unary
481198090Srdivacky    // twines.
482226890Sdim    Child NewLHS, NewRHS;
483226890Sdim    NewLHS.twine = this;
484226890Sdim    NewRHS.twine = &Suffix;
485198090Srdivacky    NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
486198090Srdivacky    if (isUnary()) {
487198090Srdivacky      NewLHS = LHS;
488198090Srdivacky      NewLHSKind = getLHSKind();
489198090Srdivacky    }
490198090Srdivacky    if (Suffix.isUnary()) {
491198090Srdivacky      NewRHS = Suffix.LHS;
492198090Srdivacky      NewRHSKind = Suffix.getLHSKind();
493198090Srdivacky    }
494198090Srdivacky
495198090Srdivacky    return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
496198090Srdivacky  }
497198090Srdivacky
498198090Srdivacky  inline Twine operator+(const Twine &LHS, const Twine &RHS) {
499198090Srdivacky    return LHS.concat(RHS);
500198090Srdivacky  }
501198090Srdivacky
502198090Srdivacky  /// Additional overload to guarantee simplified codegen; this is equivalent to
503198090Srdivacky  /// concat().
504198090Srdivacky
505198090Srdivacky  inline Twine operator+(const char *LHS, const StringRef &RHS) {
506198090Srdivacky    return Twine(LHS, RHS);
507198090Srdivacky  }
508198090Srdivacky
509198090Srdivacky  /// Additional overload to guarantee simplified codegen; this is equivalent to
510198090Srdivacky  /// concat().
511198090Srdivacky
512198090Srdivacky  inline Twine operator+(const StringRef &LHS, const char *RHS) {
513198090Srdivacky    return Twine(LHS, RHS);
514198090Srdivacky  }
515198090Srdivacky
516198090Srdivacky  inline raw_ostream &operator<<(raw_ostream &OS, const Twine &RHS) {
517198090Srdivacky    RHS.print(OS);
518198090Srdivacky    return OS;
519198090Srdivacky  }
520198090Srdivacky
521198090Srdivacky  /// @}
522198090Srdivacky}
523198090Srdivacky
524198090Srdivacky#endif
525