Twine.h revision fe09b2098ac483f6d6ce6ea4ab237a9539bdb6b9
1//===-- Twine.h - Fast Temporary String Concatenation -----------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef LLVM_ADT_TWINE_H 11#define LLVM_ADT_TWINE_H 12 13#include "llvm/ADT/StringRef.h" 14#include "llvm/Support/DataTypes.h" 15#include <cassert> 16#include <string> 17 18namespace llvm { 19 template <typename T> 20 class SmallVectorImpl; 21 class StringRef; 22 class raw_ostream; 23 24 /// Twine - A lightweight data structure for efficiently representing the 25 /// concatenation of temporary values as strings. 26 /// 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). 35 /// 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. 41 /// 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. 45 /// 46 /// \b Implementation \n 47 /// 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. 54 /// 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. 58 /// 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). 66 /// 67 /// These invariants are check by \see isValid(). 68 /// 69 /// \b Efficiency Considerations \n 70 /// 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. 75 /// 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. 80 class Twine { 81 /// NodeKind - Represent the type of an argument. 82 enum NodeKind { 83 /// An empty string; the result of concatenating anything with it is also 84 /// empty. 85 NullKind, 86 87 /// The empty string. 88 EmptyKind, 89 90 /// A pointer to a Twine instance. 91 TwineKind, 92 93 /// A pointer to a C string instance. 94 CStringKind, 95 96 /// A pointer to an std::string instance. 97 StdStringKind, 98 99 /// A pointer to a StringRef instance. 100 StringRefKind, 101 102 /// A pointer to a uint64_t value, to render as an unsigned decimal 103 /// integer. 104 UDec32Kind, 105 106 /// A pointer to a uint64_t value, to render as a signed decimal integer. 107 SDec32Kind, 108 109 /// A pointer to a uint64_t value, to render as an unsigned decimal 110 /// integer. 111 UDec64Kind, 112 113 /// A pointer to a uint64_t value, to render as a signed decimal integer. 114 SDec64Kind, 115 116 /// A pointer to a uint64_t value, to render as an unsigned hexadecimal 117 /// integer. 118 UHexKind 119 }; 120 121 private: 122 /// LHS - The prefix in the concatenation, which may be uninitialized for 123 /// Null or Empty kinds. 124 const void *LHS; 125 /// RHS - The suffix in the concatenation, which may be uninitialized for 126 /// Null or Empty kinds. 127 const void *RHS; 128 /// LHSKind - The NodeKind of the left hand side, \see getLHSKind(). 129 NodeKind LHSKind : 8; 130 /// RHSKind - The NodeKind of the left hand side, \see getLHSKind(). 131 NodeKind RHSKind : 8; 132 133 private: 134 /// Construct a nullary twine; the kind must be NullKind or EmptyKind. 135 explicit Twine(NodeKind Kind) 136 : LHSKind(Kind), RHSKind(EmptyKind) { 137 assert(isNullary() && "Invalid kind!"); 138 } 139 140 /// Construct a binary twine. 141 explicit Twine(const Twine &_LHS, const Twine &_RHS) 142 : LHS(&_LHS), RHS(&_RHS), LHSKind(TwineKind), RHSKind(TwineKind) { 143 assert(isValid() && "Invalid twine!"); 144 } 145 146 /// Construct a twine from explicit values. 147 explicit Twine(const void *_LHS, NodeKind _LHSKind, 148 const void *_RHS, NodeKind _RHSKind) 149 : LHS(_LHS), RHS(_RHS), LHSKind(_LHSKind), RHSKind(_RHSKind) { 150 assert(isValid() && "Invalid twine!"); 151 } 152 153 /// isNull - Check for the null twine. 154 bool isNull() const { 155 return getLHSKind() == NullKind; 156 } 157 158 /// isEmpty - Check for the empty twine. 159 bool isEmpty() const { 160 return getLHSKind() == EmptyKind; 161 } 162 163 /// isNullary - Check if this is a nullary twine (null or empty). 164 bool isNullary() const { 165 return isNull() || isEmpty(); 166 } 167 168 /// isUnary - Check if this is a unary twine. 169 bool isUnary() const { 170 return getRHSKind() == EmptyKind && !isNullary(); 171 } 172 173 /// isBinary - Check if this is a binary twine. 174 bool isBinary() const { 175 return getLHSKind() != NullKind && getRHSKind() != EmptyKind; 176 } 177 178 /// isValid - Check if this is a valid twine (satisfying the invariants on 179 /// order and number of arguments). 180 bool isValid() const { 181 // Nullary twines always have Empty on the RHS. 182 if (isNullary() && getRHSKind() != EmptyKind) 183 return false; 184 185 // Null should never appear on the RHS. 186 if (getRHSKind() == NullKind) 187 return false; 188 189 // The RHS cannot be non-empty if the LHS is empty. 190 if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind) 191 return false; 192 193 // A twine child should always be binary. 194 if (getLHSKind() == TwineKind && 195 !static_cast<const Twine*>(LHS)->isBinary()) 196 return false; 197 if (getRHSKind() == TwineKind && 198 !static_cast<const Twine*>(RHS)->isBinary()) 199 return false; 200 201 return true; 202 } 203 204 /// getLHSKind - Get the NodeKind of the left-hand side. 205 NodeKind getLHSKind() const { return LHSKind; } 206 207 /// getRHSKind - Get the NodeKind of the left-hand side. 208 NodeKind getRHSKind() const { return RHSKind; } 209 210 /// printOneChild - Print one child from a twine. 211 void printOneChild(raw_ostream &OS, const void *Ptr, NodeKind Kind) const; 212 213 /// printOneChildRepr - Print the representation of one child from a twine. 214 void printOneChildRepr(raw_ostream &OS, const void *Ptr, 215 NodeKind Kind) const; 216 217 public: 218 /// @name Constructors 219 /// @{ 220 221 /// Construct from an empty string. 222 /*implicit*/ Twine() : LHSKind(EmptyKind), RHSKind(EmptyKind) { 223 assert(isValid() && "Invalid twine!"); 224 } 225 226 /// Construct from a C string. 227 /// 228 /// We take care here to optimize "" into the empty twine -- this will be 229 /// optimized out for string constants. This allows Twine arguments have 230 /// default "" values, without introducing unnecessary string constants. 231 /*implicit*/ Twine(const char *Str) 232 : RHSKind(EmptyKind) { 233 if (Str[0] != '\0') { 234 LHS = Str; 235 LHSKind = CStringKind; 236 } else 237 LHSKind = EmptyKind; 238 239 assert(isValid() && "Invalid twine!"); 240 } 241 242 /// Construct from an std::string. 243 /*implicit*/ Twine(const std::string &Str) 244 : LHS(&Str), LHSKind(StdStringKind), RHSKind(EmptyKind) { 245 assert(isValid() && "Invalid twine!"); 246 } 247 248 /// Construct from a StringRef. 249 /*implicit*/ Twine(const StringRef &Str) 250 : LHS(&Str), LHSKind(StringRefKind), RHSKind(EmptyKind) { 251 assert(isValid() && "Invalid twine!"); 252 } 253 254 /// Construct a twine to print \arg Val as an unsigned decimal integer. 255 explicit Twine(const uint32_t &Val) 256 : LHS(&Val), LHSKind(UDec32Kind), RHSKind(EmptyKind) { 257 } 258 259 /// Construct a twine to print \arg Val as a signed decimal integer. 260 explicit Twine(const int32_t &Val) 261 : LHS(&Val), LHSKind(SDec32Kind), RHSKind(EmptyKind) { 262 } 263 264 /// Construct a twine to print \arg Val as an unsigned decimal integer. 265 explicit Twine(const uint64_t &Val) 266 : LHS(&Val), LHSKind(UDec64Kind), RHSKind(EmptyKind) { 267 } 268 269 /// Construct a twine to print \arg Val as a signed decimal integer. 270 explicit Twine(const int64_t &Val) 271 : LHS(&Val), LHSKind(SDec64Kind), RHSKind(EmptyKind) { 272 } 273 274 // FIXME: Unfortunately, to make sure this is as efficient as possible we 275 // need extra binary constructors from particular types. We can't rely on 276 // the compiler to be smart enough to fold operator+()/concat() down to the 277 // right thing. Yet. 278 279 /// Construct as the concatenation of a C string and a StringRef. 280 /*implicit*/ Twine(const char *_LHS, const StringRef &_RHS) 281 : LHS(_LHS), RHS(&_RHS), LHSKind(CStringKind), RHSKind(StringRefKind) { 282 assert(isValid() && "Invalid twine!"); 283 } 284 285 /// Construct as the concatenation of a StringRef and a C string. 286 /*implicit*/ Twine(const StringRef &_LHS, const char *_RHS) 287 : LHS(&_LHS), RHS(_RHS), LHSKind(StringRefKind), RHSKind(CStringKind) { 288 assert(isValid() && "Invalid twine!"); 289 } 290 291 /// Create a 'null' string, which is an empty string that always 292 /// concatenates to form another empty string. 293 static Twine createNull() { 294 return Twine(NullKind); 295 } 296 297 /// @} 298 /// @name Numeric Conversions 299 /// @{ 300 301 // Construct a twine to print \arg Val as an unsigned hexadecimal integer. 302 static Twine utohexstr(const uint64_t &Val) { 303 return Twine(&Val, UHexKind, 0, EmptyKind); 304 } 305 306 // Construct a twine to print \arg Val as an unsigned hexadecimal 307 // integer. This routine is provided as a convenience to sign extend values 308 // before printing. 309 static Twine itohexstr(const int64_t &Val) { 310 return Twine(&Val, UHexKind, 0, EmptyKind); 311 } 312 313 /// @} 314 /// @name String Operations 315 /// @{ 316 317 Twine concat(const Twine &Suffix) const; 318 319 /// @} 320 /// @name Output & Conversion. 321 /// @{ 322 323 /// str - Return the twine contents as a std::string. 324 std::string str() const; 325 326 /// toVector - Write the concatenated string into the given SmallString or 327 /// SmallVector. 328 void toVector(SmallVectorImpl<char> &Out) const; 329 330 /// print - Write the concatenated string represented by this twine to the 331 /// stream \arg OS. 332 void print(raw_ostream &OS) const; 333 334 /// dump - Dump the concatenated string represented by this twine to stderr. 335 void dump() const; 336 337 /// print - Write the representation of this twine to the stream \arg OS. 338 void printRepr(raw_ostream &OS) const; 339 340 /// dumpRepr - Dump the representation of this twine to stderr. 341 void dumpRepr() const; 342 343 /// @} 344 }; 345 346 /// @name Twine Inline Implementations 347 /// @{ 348 349 inline Twine Twine::concat(const Twine &Suffix) const { 350 // Concatenation with null is null. 351 if (isNull() || Suffix.isNull()) 352 return Twine(NullKind); 353 354 // Concatenation with empty yields the other side. 355 if (isEmpty()) 356 return Suffix; 357 if (Suffix.isEmpty()) 358 return *this; 359 360 // Otherwise we need to create a new node, taking care to fold in unary 361 // twines. 362 const void *NewLHS = this, *NewRHS = &Suffix; 363 NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind; 364 if (isUnary()) { 365 NewLHS = LHS; 366 NewLHSKind = getLHSKind(); 367 } 368 if (Suffix.isUnary()) { 369 NewRHS = Suffix.LHS; 370 NewRHSKind = Suffix.getLHSKind(); 371 } 372 373 return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind); 374 } 375 376 inline Twine operator+(const Twine &LHS, const Twine &RHS) { 377 return LHS.concat(RHS); 378 } 379 380 /// Additional overload to guarantee simplified codegen; this is equivalent to 381 /// concat(). 382 383 inline Twine operator+(const char *LHS, const StringRef &RHS) { 384 return Twine(LHS, RHS); 385 } 386 387 /// Additional overload to guarantee simplified codegen; this is equivalent to 388 /// concat(). 389 390 inline Twine operator+(const StringRef &LHS, const char *RHS) { 391 return Twine(LHS, RHS); 392 } 393 394 inline raw_ostream &operator<<(raw_ostream &OS, const Twine &RHS) { 395 RHS.print(OS); 396 return OS; 397 } 398 399 /// @} 400} 401 402#endif 403