LLVM  mainline
Twine.h
Go to the documentation of this file.
00001 //===-- Twine.h - Fast Temporary String Concatenation -----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_ADT_TWINE_H
00011 #define LLVM_ADT_TWINE_H
00012 
00013 #include "llvm/ADT/SmallVector.h"
00014 #include "llvm/ADT/StringRef.h"
00015 #include "llvm/Support/DataTypes.h"
00016 #include "llvm/Support/ErrorHandling.h"
00017 #include <cassert>
00018 #include <string>
00019 
00020 namespace llvm {
00021   class raw_ostream;
00022 
00023   /// Twine - A lightweight data structure for efficiently representing the
00024   /// concatenation of temporary values as strings.
00025   ///
00026   /// A Twine is a kind of rope, it represents a concatenated string using a
00027   /// binary-tree, where the string is the preorder of the nodes. Since the
00028   /// Twine can be efficiently rendered into a buffer when its result is used,
00029   /// it avoids the cost of generating temporary values for intermediate string
00030   /// results -- particularly in cases when the Twine result is never
00031   /// required. By explicitly tracking the type of leaf nodes, we can also avoid
00032   /// the creation of temporary strings for conversions operations (such as
00033   /// appending an integer to a string).
00034   ///
00035   /// A Twine is not intended for use directly and should not be stored, its
00036   /// implementation relies on the ability to store pointers to temporary stack
00037   /// objects which may be deallocated at the end of a statement. Twines should
00038   /// only be used accepted as const references in arguments, when an API wishes
00039   /// to accept possibly-concatenated strings.
00040   ///
00041   /// Twines support a special 'null' value, which always concatenates to form
00042   /// itself, and renders as an empty string. This can be returned from APIs to
00043   /// effectively nullify any concatenations performed on the result.
00044   ///
00045   /// \b Implementation
00046   ///
00047   /// Given the nature of a Twine, it is not possible for the Twine's
00048   /// concatenation method to construct interior nodes; the result must be
00049   /// represented inside the returned value. For this reason a Twine object
00050   /// actually holds two values, the left- and right-hand sides of a
00051   /// concatenation. We also have nullary Twine objects, which are effectively
00052   /// sentinel values that represent empty strings.
00053   ///
00054   /// Thus, a Twine can effectively have zero, one, or two children. The \see
00055   /// isNullary(), \see isUnary(), and \see isBinary() predicates exist for
00056   /// testing the number of children.
00057   ///
00058   /// We maintain a number of invariants on Twine objects (FIXME: Why):
00059   ///  - Nullary twines are always represented with their Kind on the left-hand
00060   ///    side, and the Empty kind on the right-hand side.
00061   ///  - Unary twines are always represented with the value on the left-hand
00062   ///    side, and the Empty kind on the right-hand side.
00063   ///  - If a Twine has another Twine as a child, that child should always be
00064   ///    binary (otherwise it could have been folded into the parent).
00065   ///
00066   /// These invariants are check by \see isValid().
00067   ///
00068   /// \b Efficiency Considerations
00069   ///
00070   /// The Twine is designed to yield efficient and small code for common
00071   /// situations. For this reason, the concat() method is inlined so that
00072   /// concatenations of leaf nodes can be optimized into stores directly into a
00073   /// single stack allocated object.
00074   ///
00075   /// In practice, not all compilers can be trusted to optimize concat() fully,
00076   /// so we provide two additional methods (and accompanying operator+
00077   /// overloads) to guarantee that particularly important cases (cstring plus
00078   /// StringRef) codegen as desired.
00079   class Twine {
00080     /// NodeKind - Represent the type of an argument.
00081     enum NodeKind : unsigned char {
00082       /// An empty string; the result of concatenating anything with it is also
00083       /// empty.
00084       NullKind,
00085 
00086       /// The empty string.
00087       EmptyKind,
00088 
00089       /// A pointer to a Twine instance.
00090       TwineKind,
00091 
00092       /// A pointer to a C string instance.
00093       CStringKind,
00094 
00095       /// A pointer to an std::string instance.
00096       StdStringKind,
00097 
00098       /// A pointer to a StringRef instance.
00099       StringRefKind,
00100 
00101       /// A pointer to a SmallString instance.
00102       SmallStringKind,
00103 
00104       /// A char value, to render as a character.
00105       CharKind,
00106 
00107       /// An unsigned int value, to render as an unsigned decimal integer.
00108       DecUIKind,
00109 
00110       /// An int value, to render as a signed decimal integer.
00111       DecIKind,
00112 
00113       /// A pointer to an unsigned long value, to render as an unsigned decimal
00114       /// integer.
00115       DecULKind,
00116 
00117       /// A pointer to a long value, to render as a signed decimal integer.
00118       DecLKind,
00119 
00120       /// A pointer to an unsigned long long value, to render as an unsigned
00121       /// decimal integer.
00122       DecULLKind,
00123 
00124       /// A pointer to a long long value, to render as a signed decimal integer.
00125       DecLLKind,
00126 
00127       /// A pointer to a uint64_t value, to render as an unsigned hexadecimal
00128       /// integer.
00129       UHexKind
00130     };
00131 
00132     union Child
00133     {
00134       const Twine *twine;
00135       const char *cString;
00136       const std::string *stdString;
00137       const StringRef *stringRef;
00138       const SmallVectorImpl<char> *smallString;
00139       char character;
00140       unsigned int decUI;
00141       int decI;
00142       const unsigned long *decUL;
00143       const long *decL;
00144       const unsigned long long *decULL;
00145       const long long *decLL;
00146       const uint64_t *uHex;
00147     };
00148 
00149   private:
00150     /// LHS - The prefix in the concatenation, which may be uninitialized for
00151     /// Null or Empty kinds.
00152     Child LHS;
00153     /// RHS - The suffix in the concatenation, which may be uninitialized for
00154     /// Null or Empty kinds.
00155     Child RHS;
00156     /// LHSKind - The NodeKind of the left hand side, \see getLHSKind().
00157     NodeKind LHSKind;
00158     /// RHSKind - The NodeKind of the right hand side, \see getRHSKind().
00159     NodeKind RHSKind;
00160 
00161   private:
00162     /// Construct a nullary twine; the kind must be NullKind or EmptyKind.
00163     explicit Twine(NodeKind Kind)
00164       : LHSKind(Kind), RHSKind(EmptyKind) {
00165       assert(isNullary() && "Invalid kind!");
00166     }
00167 
00168     /// Construct a binary twine.
00169     explicit Twine(const Twine &LHS, const Twine &RHS)
00170         : LHSKind(TwineKind), RHSKind(TwineKind) {
00171       this->LHS.twine = &LHS;
00172       this->RHS.twine = &RHS;
00173       assert(isValid() && "Invalid twine!");
00174     }
00175 
00176     /// Construct a twine from explicit values.
00177     explicit Twine(Child LHS, NodeKind LHSKind, Child RHS, NodeKind RHSKind)
00178         : LHS(LHS), RHS(RHS), LHSKind(LHSKind), RHSKind(RHSKind) {
00179       assert(isValid() && "Invalid twine!");
00180     }
00181 
00182     /// Since the intended use of twines is as temporary objects, assignments
00183     /// when concatenating might cause undefined behavior or stack corruptions
00184     Twine &operator=(const Twine &Other) = delete;
00185 
00186     /// Check for the null twine.
00187     bool isNull() const {
00188       return getLHSKind() == NullKind;
00189     }
00190 
00191     /// Check for the empty twine.
00192     bool isEmpty() const {
00193       return getLHSKind() == EmptyKind;
00194     }
00195 
00196     /// Check if this is a nullary twine (null or empty).
00197     bool isNullary() const {
00198       return isNull() || isEmpty();
00199     }
00200 
00201     /// Check if this is a unary twine.
00202     bool isUnary() const {
00203       return getRHSKind() == EmptyKind && !isNullary();
00204     }
00205 
00206     /// Check if this is a binary twine.
00207     bool isBinary() const {
00208       return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
00209     }
00210 
00211     /// Check if this is a valid twine (satisfying the invariants on
00212     /// order and number of arguments).
00213     bool isValid() const {
00214       // Nullary twines always have Empty on the RHS.
00215       if (isNullary() && getRHSKind() != EmptyKind)
00216         return false;
00217 
00218       // Null should never appear on the RHS.
00219       if (getRHSKind() == NullKind)
00220         return false;
00221 
00222       // The RHS cannot be non-empty if the LHS is empty.
00223       if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
00224         return false;
00225 
00226       // A twine child should always be binary.
00227       if (getLHSKind() == TwineKind &&
00228           !LHS.twine->isBinary())
00229         return false;
00230       if (getRHSKind() == TwineKind &&
00231           !RHS.twine->isBinary())
00232         return false;
00233 
00234       return true;
00235     }
00236 
00237     /// Get the NodeKind of the left-hand side.
00238     NodeKind getLHSKind() const { return LHSKind; }
00239 
00240     /// Get the NodeKind of the right-hand side.
00241     NodeKind getRHSKind() const { return RHSKind; }
00242 
00243     /// Print one child from a twine.
00244     void printOneChild(raw_ostream &OS, Child Ptr, NodeKind Kind) const;
00245 
00246     /// Print the representation of one child from a twine.
00247     void printOneChildRepr(raw_ostream &OS, Child Ptr,
00248                            NodeKind Kind) const;
00249 
00250   public:
00251     /// @name Constructors
00252     /// @{
00253 
00254     /// Construct from an empty string.
00255     /*implicit*/ Twine() : LHSKind(EmptyKind), RHSKind(EmptyKind) {
00256       assert(isValid() && "Invalid twine!");
00257     }
00258 
00259     Twine(const Twine &) = default;
00260 
00261     /// Construct from a C string.
00262     ///
00263     /// We take care here to optimize "" into the empty twine -- this will be
00264     /// optimized out for string constants. This allows Twine arguments have
00265     /// default "" values, without introducing unnecessary string constants.
00266     /*implicit*/ Twine(const char *Str)
00267       : RHSKind(EmptyKind) {
00268       if (Str[0] != '\0') {
00269         LHS.cString = Str;
00270         LHSKind = CStringKind;
00271       } else
00272         LHSKind = EmptyKind;
00273 
00274       assert(isValid() && "Invalid twine!");
00275     }
00276 
00277     /// Construct from an std::string.
00278     /*implicit*/ Twine(const std::string &Str)
00279       : LHSKind(StdStringKind), RHSKind(EmptyKind) {
00280       LHS.stdString = &Str;
00281       assert(isValid() && "Invalid twine!");
00282     }
00283 
00284     /// Construct from a StringRef.
00285     /*implicit*/ Twine(const StringRef &Str)
00286       : LHSKind(StringRefKind), RHSKind(EmptyKind) {
00287       LHS.stringRef = &Str;
00288       assert(isValid() && "Invalid twine!");
00289     }
00290 
00291     /// Construct from a SmallString.
00292     /*implicit*/ Twine(const SmallVectorImpl<char> &Str)
00293       : LHSKind(SmallStringKind), RHSKind(EmptyKind) {
00294       LHS.smallString = &Str;
00295       assert(isValid() && "Invalid twine!");
00296     }
00297 
00298     /// Construct from a char.
00299     explicit Twine(char Val)
00300       : LHSKind(CharKind), RHSKind(EmptyKind) {
00301       LHS.character = Val;
00302     }
00303 
00304     /// Construct from a signed char.
00305     explicit Twine(signed char Val)
00306       : LHSKind(CharKind), RHSKind(EmptyKind) {
00307       LHS.character = static_cast<char>(Val);
00308     }
00309 
00310     /// Construct from an unsigned char.
00311     explicit Twine(unsigned char Val)
00312       : LHSKind(CharKind), RHSKind(EmptyKind) {
00313       LHS.character = static_cast<char>(Val);
00314     }
00315 
00316     /// Construct a twine to print \p Val as an unsigned decimal integer.
00317     explicit Twine(unsigned Val)
00318       : LHSKind(DecUIKind), RHSKind(EmptyKind) {
00319       LHS.decUI = Val;
00320     }
00321 
00322     /// Construct a twine to print \p Val as a signed decimal integer.
00323     explicit Twine(int Val)
00324       : LHSKind(DecIKind), RHSKind(EmptyKind) {
00325       LHS.decI = Val;
00326     }
00327 
00328     /// Construct a twine to print \p Val as an unsigned decimal integer.
00329     explicit Twine(const unsigned long &Val)
00330       : LHSKind(DecULKind), RHSKind(EmptyKind) {
00331       LHS.decUL = &Val;
00332     }
00333 
00334     /// Construct a twine to print \p Val as a signed decimal integer.
00335     explicit Twine(const long &Val)
00336       : LHSKind(DecLKind), RHSKind(EmptyKind) {
00337       LHS.decL = &Val;
00338     }
00339 
00340     /// Construct a twine to print \p Val as an unsigned decimal integer.
00341     explicit Twine(const unsigned long long &Val)
00342       : LHSKind(DecULLKind), RHSKind(EmptyKind) {
00343       LHS.decULL = &Val;
00344     }
00345 
00346     /// Construct a twine to print \p Val as a signed decimal integer.
00347     explicit Twine(const long long &Val)
00348       : LHSKind(DecLLKind), RHSKind(EmptyKind) {
00349       LHS.decLL = &Val;
00350     }
00351 
00352     // FIXME: Unfortunately, to make sure this is as efficient as possible we
00353     // need extra binary constructors from particular types. We can't rely on
00354     // the compiler to be smart enough to fold operator+()/concat() down to the
00355     // right thing. Yet.
00356 
00357     /// Construct as the concatenation of a C string and a StringRef.
00358     /*implicit*/ Twine(const char *LHS, const StringRef &RHS)
00359         : LHSKind(CStringKind), RHSKind(StringRefKind) {
00360       this->LHS.cString = LHS;
00361       this->RHS.stringRef = &RHS;
00362       assert(isValid() && "Invalid twine!");
00363     }
00364 
00365     /// Construct as the concatenation of a StringRef and a C string.
00366     /*implicit*/ Twine(const StringRef &LHS, const char *RHS)
00367         : LHSKind(StringRefKind), RHSKind(CStringKind) {
00368       this->LHS.stringRef = &LHS;
00369       this->RHS.cString = RHS;
00370       assert(isValid() && "Invalid twine!");
00371     }
00372 
00373     /// Create a 'null' string, which is an empty string that always
00374     /// concatenates to form another empty string.
00375     static Twine createNull() {
00376       return Twine(NullKind);
00377     }
00378 
00379     /// @}
00380     /// @name Numeric Conversions
00381     /// @{
00382 
00383     // Construct a twine to print \p Val as an unsigned hexadecimal integer.
00384     static Twine utohexstr(const uint64_t &Val) {
00385       Child LHS, RHS;
00386       LHS.uHex = &Val;
00387       RHS.twine = nullptr;
00388       return Twine(LHS, UHexKind, RHS, EmptyKind);
00389     }
00390 
00391     /// @}
00392     /// @name Predicate Operations
00393     /// @{
00394 
00395     /// Check if this twine is trivially empty; a false return value does not
00396     /// necessarily mean the twine is empty.
00397     bool isTriviallyEmpty() const {
00398       return isNullary();
00399     }
00400 
00401     /// Return true if this twine can be dynamically accessed as a single
00402     /// StringRef value with getSingleStringRef().
00403     bool isSingleStringRef() const {
00404       if (getRHSKind() != EmptyKind) return false;
00405 
00406       switch (getLHSKind()) {
00407       case EmptyKind:
00408       case CStringKind:
00409       case StdStringKind:
00410       case StringRefKind:
00411       case SmallStringKind:
00412         return true;
00413       default:
00414         return false;
00415       }
00416     }
00417 
00418     /// @}
00419     /// @name String Operations
00420     /// @{
00421 
00422     Twine concat(const Twine &Suffix) const;
00423 
00424     /// @}
00425     /// @name Output & Conversion.
00426     /// @{
00427 
00428     /// Return the twine contents as a std::string.
00429     std::string str() const;
00430 
00431     /// Append the concatenated string into the given SmallString or SmallVector.
00432     void toVector(SmallVectorImpl<char> &Out) const;
00433 
00434     /// This returns the twine as a single StringRef.  This method is only valid
00435     /// if isSingleStringRef() is true.
00436     StringRef getSingleStringRef() const {
00437       assert(isSingleStringRef() &&"This cannot be had as a single stringref!");
00438       switch (getLHSKind()) {
00439       default: llvm_unreachable("Out of sync with isSingleStringRef");
00440       case EmptyKind:      return StringRef();
00441       case CStringKind:    return StringRef(LHS.cString);
00442       case StdStringKind:  return StringRef(*LHS.stdString);
00443       case StringRefKind:  return *LHS.stringRef;
00444       case SmallStringKind:
00445         return StringRef(LHS.smallString->data(), LHS.smallString->size());
00446       }
00447     }
00448 
00449     /// This returns the twine as a single StringRef if it can be
00450     /// represented as such. Otherwise the twine is written into the given
00451     /// SmallVector and a StringRef to the SmallVector's data is returned.
00452     StringRef toStringRef(SmallVectorImpl<char> &Out) const {
00453       if (isSingleStringRef())
00454         return getSingleStringRef();
00455       toVector(Out);
00456       return StringRef(Out.data(), Out.size());
00457     }
00458 
00459     /// This returns the twine as a single null terminated StringRef if it
00460     /// can be represented as such. Otherwise the twine is written into the
00461     /// given SmallVector and a StringRef to the SmallVector's data is returned.
00462     ///
00463     /// The returned StringRef's size does not include the null terminator.
00464     StringRef toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const;
00465 
00466     /// Write the concatenated string represented by this twine to the
00467     /// stream \p OS.
00468     void print(raw_ostream &OS) const;
00469 
00470     /// Dump the concatenated string represented by this twine to stderr.
00471     void dump() const;
00472 
00473     /// Write the representation of this twine to the stream \p OS.
00474     void printRepr(raw_ostream &OS) const;
00475 
00476     /// Dump the representation of this twine to stderr.
00477     void dumpRepr() const;
00478 
00479     /// @}
00480   };
00481 
00482   /// @name Twine Inline Implementations
00483   /// @{
00484 
00485   inline Twine Twine::concat(const Twine &Suffix) const {
00486     // Concatenation with null is null.
00487     if (isNull() || Suffix.isNull())
00488       return Twine(NullKind);
00489 
00490     // Concatenation with empty yields the other side.
00491     if (isEmpty())
00492       return Suffix;
00493     if (Suffix.isEmpty())
00494       return *this;
00495 
00496     // Otherwise we need to create a new node, taking care to fold in unary
00497     // twines.
00498     Child NewLHS, NewRHS;
00499     NewLHS.twine = this;
00500     NewRHS.twine = &Suffix;
00501     NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
00502     if (isUnary()) {
00503       NewLHS = LHS;
00504       NewLHSKind = getLHSKind();
00505     }
00506     if (Suffix.isUnary()) {
00507       NewRHS = Suffix.LHS;
00508       NewRHSKind = Suffix.getLHSKind();
00509     }
00510 
00511     return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
00512   }
00513 
00514   inline Twine operator+(const Twine &LHS, const Twine &RHS) {
00515     return LHS.concat(RHS);
00516   }
00517 
00518   /// Additional overload to guarantee simplified codegen; this is equivalent to
00519   /// concat().
00520 
00521   inline Twine operator+(const char *LHS, const StringRef &RHS) {
00522     return Twine(LHS, RHS);
00523   }
00524 
00525   /// Additional overload to guarantee simplified codegen; this is equivalent to
00526   /// concat().
00527 
00528   inline Twine operator+(const StringRef &LHS, const char *RHS) {
00529     return Twine(LHS, RHS);
00530   }
00531 
00532   inline raw_ostream &operator<<(raw_ostream &OS, const Twine &RHS) {
00533     RHS.print(OS);
00534     return OS;
00535   }
00536 
00537   /// @}
00538 }
00539 
00540 #endif