Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

524 lines
17 KiB

  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. #ifndef LLVM_ADT_TWINE_H
  10. #define LLVM_ADT_TWINE_H
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/DataTypes.h"
  13. #include "llvm/Support/ErrorHandling.h"
  14. #include <cassert>
  15. #include <string>
  16. namespace llvm {
  17. template <typename T>
  18. class SmallVectorImpl;
  19. class StringRef;
  20. class raw_ostream;
  21. /// Twine - A lightweight data structure for efficiently representing the
  22. /// concatenation of temporary values as strings.
  23. ///
  24. /// A Twine is a kind of rope, it represents a concatenated string using a
  25. /// binary-tree, where the string is the preorder of the nodes. Since the
  26. /// Twine can be efficiently rendered into a buffer when its result is used,
  27. /// it avoids the cost of generating temporary values for intermediate string
  28. /// results -- particularly in cases when the Twine result is never
  29. /// required. By explicitly tracking the type of leaf nodes, we can also avoid
  30. /// the creation of temporary strings for conversions operations (such as
  31. /// appending an integer to a string).
  32. ///
  33. /// A Twine is not intended for use directly and should not be stored, its
  34. /// implementation relies on the ability to store pointers to temporary stack
  35. /// objects which may be deallocated at the end of a statement. Twines should
  36. /// only be used accepted as const references in arguments, when an API wishes
  37. /// to accept possibly-concatenated strings.
  38. ///
  39. /// Twines support a special 'null' value, which always concatenates to form
  40. /// itself, and renders as an empty string. This can be returned from APIs to
  41. /// effectively nullify any concatenations performed on the result.
  42. ///
  43. /// \b Implementation
  44. ///
  45. /// Given the nature of a Twine, it is not possible for the Twine's
  46. /// concatenation method to construct interior nodes; the result must be
  47. /// represented inside the returned value. For this reason a Twine object
  48. /// actually holds two values, the left- and right-hand sides of a
  49. /// concatenation. We also have nullary Twine objects, which are effectively
  50. /// sentinel values that represent empty strings.
  51. ///
  52. /// Thus, a Twine can effectively have zero, one, or two children. The \see
  53. /// isNullary(), \see isUnary(), and \see isBinary() predicates exist for
  54. /// testing the number of children.
  55. ///
  56. /// We maintain a number of invariants on Twine objects (FIXME: Why):
  57. /// - Nullary twines are always represented with their Kind on the left-hand
  58. /// side, and the Empty kind on the right-hand side.
  59. /// - Unary twines are always represented with the value on the left-hand
  60. /// side, and the Empty kind on the right-hand side.
  61. /// - If a Twine has another Twine as a child, that child should always be
  62. /// binary (otherwise it could have been folded into the parent).
  63. ///
  64. /// These invariants are check by \see isValid().
  65. ///
  66. /// \b Efficiency Considerations
  67. ///
  68. /// The Twine is designed to yield efficient and small code for common
  69. /// situations. For this reason, the concat() method is inlined so that
  70. /// concatenations of leaf nodes can be optimized into stores directly into a
  71. /// single stack allocated object.
  72. ///
  73. /// In practice, not all compilers can be trusted to optimize concat() fully,
  74. /// so we provide two additional methods (and accompanying operator+
  75. /// overloads) to guarantee that particularly important cases (cstring plus
  76. /// StringRef) codegen as desired.
  77. class Twine {
  78. /// NodeKind - Represent the type of an argument.
  79. enum NodeKind {
  80. /// An empty string; the result of concatenating anything with it is also
  81. /// empty.
  82. NullKind,
  83. /// The empty string.
  84. EmptyKind,
  85. /// A pointer to a Twine instance.
  86. TwineKind,
  87. /// A pointer to a C string instance.
  88. CStringKind,
  89. /// A pointer to an std::string instance.
  90. StdStringKind,
  91. /// A pointer to a StringRef instance.
  92. StringRefKind,
  93. /// A char value reinterpreted as a pointer, to render as a character.
  94. CharKind,
  95. /// An unsigned int value reinterpreted as a pointer, to render as an
  96. /// unsigned decimal integer.
  97. DecUIKind,
  98. /// An int value reinterpreted as a pointer, to render as a signed
  99. /// decimal integer.
  100. DecIKind,
  101. /// A pointer to an unsigned long value, to render as an unsigned decimal
  102. /// integer.
  103. DecULKind,
  104. /// A pointer to a long value, to render as a signed decimal integer.
  105. DecLKind,
  106. /// A pointer to an unsigned long long value, to render as an unsigned
  107. /// decimal integer.
  108. DecULLKind,
  109. /// A pointer to a long long value, to render as a signed decimal integer.
  110. DecLLKind,
  111. /// A pointer to a uint64_t value, to render as an unsigned hexadecimal
  112. /// integer.
  113. UHexKind
  114. };
  115. union Child
  116. {
  117. const Twine *twine;
  118. const char *cString;
  119. const std::string *stdString;
  120. const StringRef *stringRef;
  121. char character;
  122. unsigned int decUI;
  123. int decI;
  124. const unsigned long *decUL;
  125. const long *decL;
  126. const unsigned long long *decULL;
  127. const long long *decLL;
  128. const uint64_t *uHex;
  129. };
  130. private:
  131. /// LHS - The prefix in the concatenation, which may be uninitialized for
  132. /// Null or Empty kinds.
  133. Child LHS;
  134. /// RHS - The suffix in the concatenation, which may be uninitialized for
  135. /// Null or Empty kinds.
  136. Child RHS;
  137. // enums stored as unsigned chars to save on space while some compilers
  138. // don't support specifying the backing type for an enum
  139. /// LHSKind - The NodeKind of the left hand side, \see getLHSKind().
  140. unsigned char LHSKind;
  141. /// RHSKind - The NodeKind of the left hand side, \see getLHSKind().
  142. unsigned char RHSKind;
  143. private:
  144. /// Construct a nullary twine; the kind must be NullKind or EmptyKind.
  145. explicit Twine(NodeKind Kind)
  146. : LHSKind(Kind), RHSKind(EmptyKind) {
  147. assert(isNullary() && "Invalid kind!");
  148. }
  149. /// Construct a binary twine.
  150. explicit Twine(const Twine &_LHS, const Twine &_RHS)
  151. : LHSKind(TwineKind), RHSKind(TwineKind) {
  152. LHS.twine = &_LHS;
  153. RHS.twine = &_RHS;
  154. assert(isValid() && "Invalid twine!");
  155. }
  156. /// Construct a twine from explicit values.
  157. explicit Twine(Child _LHS, NodeKind _LHSKind,
  158. Child _RHS, NodeKind _RHSKind)
  159. : LHS(_LHS), RHS(_RHS), LHSKind(_LHSKind), RHSKind(_RHSKind) {
  160. assert(isValid() && "Invalid twine!");
  161. }
  162. /// isNull - Check for the null twine.
  163. bool isNull() const {
  164. return getLHSKind() == NullKind;
  165. }
  166. /// isEmpty - Check for the empty twine.
  167. bool isEmpty() const {
  168. return getLHSKind() == EmptyKind;
  169. }
  170. /// isNullary - Check if this is a nullary twine (null or empty).
  171. bool isNullary() const {
  172. return isNull() || isEmpty();
  173. }
  174. /// isUnary - Check if this is a unary twine.
  175. bool isUnary() const {
  176. return getRHSKind() == EmptyKind && !isNullary();
  177. }
  178. /// isBinary - Check if this is a binary twine.
  179. bool isBinary() const {
  180. return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
  181. }
  182. /// isValid - Check if this is a valid twine (satisfying the invariants on
  183. /// order and number of arguments).
  184. bool isValid() const {
  185. // Nullary twines always have Empty on the RHS.
  186. if (isNullary() && getRHSKind() != EmptyKind)
  187. return false;
  188. // Null should never appear on the RHS.
  189. if (getRHSKind() == NullKind)
  190. return false;
  191. // The RHS cannot be non-empty if the LHS is empty.
  192. if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
  193. return false;
  194. // A twine child should always be binary.
  195. if (getLHSKind() == TwineKind &&
  196. !LHS.twine->isBinary())
  197. return false;
  198. if (getRHSKind() == TwineKind &&
  199. !RHS.twine->isBinary())
  200. return false;
  201. return true;
  202. }
  203. /// getLHSKind - Get the NodeKind of the left-hand side.
  204. NodeKind getLHSKind() const { return (NodeKind) LHSKind; }
  205. /// getRHSKind - Get the NodeKind of the right-hand side.
  206. NodeKind getRHSKind() const { return (NodeKind) RHSKind; }
  207. /// printOneChild - Print one child from a twine.
  208. void printOneChild(raw_ostream &OS, Child Ptr, NodeKind Kind) const;
  209. /// printOneChildRepr - Print the representation of one child from a twine.
  210. void printOneChildRepr(raw_ostream &OS, Child Ptr,
  211. NodeKind Kind) const;
  212. public:
  213. /// @name Constructors
  214. /// @{
  215. /// Construct from an empty string.
  216. /*implicit*/ Twine() : LHSKind(EmptyKind), RHSKind(EmptyKind) {
  217. assert(isValid() && "Invalid twine!");
  218. }
  219. /// Construct from a C string.
  220. ///
  221. /// We take care here to optimize "" into the empty twine -- this will be
  222. /// optimized out for string constants. This allows Twine arguments have
  223. /// default "" values, without introducing unnecessary string constants.
  224. /*implicit*/ Twine(const char *Str)
  225. : RHSKind(EmptyKind) {
  226. if (Str[0] != '\0') {
  227. LHS.cString = Str;
  228. LHSKind = CStringKind;
  229. } else
  230. LHSKind = EmptyKind;
  231. assert(isValid() && "Invalid twine!");
  232. }
  233. /// Construct from an std::string.
  234. /*implicit*/ Twine(const std::string &Str)
  235. : LHSKind(StdStringKind), RHSKind(EmptyKind) {
  236. LHS.stdString = &Str;
  237. assert(isValid() && "Invalid twine!");
  238. }
  239. /// Construct from a StringRef.
  240. /*implicit*/ Twine(const StringRef &Str)
  241. : LHSKind(StringRefKind), RHSKind(EmptyKind) {
  242. LHS.stringRef = &Str;
  243. assert(isValid() && "Invalid twine!");
  244. }
  245. /// Construct from a char.
  246. explicit Twine(char Val)
  247. : LHSKind(CharKind), RHSKind(EmptyKind) {
  248. LHS.character = Val;
  249. }
  250. /// Construct from a signed char.
  251. explicit Twine(signed char Val)
  252. : LHSKind(CharKind), RHSKind(EmptyKind) {
  253. LHS.character = static_cast<char>(Val);
  254. }
  255. /// Construct from an unsigned char.
  256. explicit Twine(unsigned char Val)
  257. : LHSKind(CharKind), RHSKind(EmptyKind) {
  258. LHS.character = static_cast<char>(Val);
  259. }
  260. /// Construct a twine to print \p Val as an unsigned decimal integer.
  261. explicit Twine(unsigned Val)
  262. : LHSKind(DecUIKind), RHSKind(EmptyKind) {
  263. LHS.decUI = Val;
  264. }
  265. /// Construct a twine to print \p Val as a signed decimal integer.
  266. explicit Twine(int Val)
  267. : LHSKind(DecIKind), RHSKind(EmptyKind) {
  268. LHS.decI = Val;
  269. }
  270. /// Construct a twine to print \p Val as an unsigned decimal integer.
  271. explicit Twine(const unsigned long &Val)
  272. : LHSKind(DecULKind), RHSKind(EmptyKind) {
  273. LHS.decUL = &Val;
  274. }
  275. /// Construct a twine to print \p Val as a signed decimal integer.
  276. explicit Twine(const long &Val)
  277. : LHSKind(DecLKind), RHSKind(EmptyKind) {
  278. LHS.decL = &Val;
  279. }
  280. /// Construct a twine to print \p Val as an unsigned decimal integer.
  281. explicit Twine(const unsigned long long &Val)
  282. : LHSKind(DecULLKind), RHSKind(EmptyKind) {
  283. LHS.decULL = &Val;
  284. }
  285. /// Construct a twine to print \p Val as a signed decimal integer.
  286. explicit Twine(const long long &Val)
  287. : LHSKind(DecLLKind), RHSKind(EmptyKind) {
  288. LHS.decLL = &Val;
  289. }
  290. // FIXME: Unfortunately, to make sure this is as efficient as possible we
  291. // need extra binary constructors from particular types. We can't rely on
  292. // the compiler to be smart enough to fold operator+()/concat() down to the
  293. // right thing. Yet.
  294. /// Construct as the concatenation of a C string and a StringRef.
  295. /*implicit*/ Twine(const char *_LHS, const StringRef &_RHS)
  296. : LHSKind(CStringKind), RHSKind(StringRefKind) {
  297. LHS.cString = _LHS;
  298. RHS.stringRef = &_RHS;
  299. assert(isValid() && "Invalid twine!");
  300. }
  301. /// Construct as the concatenation of a StringRef and a C string.
  302. /*implicit*/ Twine(const StringRef &_LHS, const char *_RHS)
  303. : LHSKind(StringRefKind), RHSKind(CStringKind) {
  304. LHS.stringRef = &_LHS;
  305. RHS.cString = _RHS;
  306. assert(isValid() && "Invalid twine!");
  307. }
  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);
  312. }
  313. /// @}
  314. /// @name Numeric Conversions
  315. /// @{
  316. // Construct a twine to print \p Val as an unsigned hexadecimal integer.
  317. static Twine utohexstr(const uint64_t &Val) {
  318. Child LHS, RHS;
  319. LHS.uHex = &Val;
  320. RHS.twine = 0;
  321. return Twine(LHS, UHexKind, RHS, EmptyKind);
  322. }
  323. /// @}
  324. /// @name Predicate Operations
  325. /// @{
  326. /// isTriviallyEmpty - Check if this twine is trivially empty; a false
  327. /// return value does not necessarily mean the twine is empty.
  328. bool isTriviallyEmpty() const {
  329. return isNullary();
  330. }
  331. /// isSingleStringRef - Return true if this twine can be dynamically
  332. /// accessed as a single StringRef value with getSingleStringRef().
  333. bool isSingleStringRef() const {
  334. if (getRHSKind() != EmptyKind) return false;
  335. switch (getLHSKind()) {
  336. case EmptyKind:
  337. case CStringKind:
  338. case StdStringKind:
  339. case StringRefKind:
  340. return true;
  341. default:
  342. return false;
  343. }
  344. }
  345. /// @}
  346. /// @name String Operations
  347. /// @{
  348. Twine concat(const Twine &Suffix) const;
  349. /// @}
  350. /// @name Output & Conversion.
  351. /// @{
  352. /// str - Return the twine contents as a std::string.
  353. std::string str() const;
  354. /// toVector - Write the concatenated string into the given SmallString or
  355. /// SmallVector.
  356. void toVector(SmallVectorImpl<char> &Out) const;
  357. /// getSingleStringRef - This returns the twine as a single StringRef. This
  358. /// method is only valid if isSingleStringRef() is true.
  359. StringRef getSingleStringRef() const {
  360. assert(isSingleStringRef() &&"This cannot be had as a single stringref!");
  361. switch (getLHSKind()) {
  362. default: llvm_unreachable("Out of sync with isSingleStringRef");
  363. case EmptyKind: return StringRef();
  364. case CStringKind: return StringRef(LHS.cString);
  365. case StdStringKind: return StringRef(*LHS.stdString);
  366. case StringRefKind: return *LHS.stringRef;
  367. }
  368. }
  369. /// toStringRef - This returns the twine as a single StringRef if it can be
  370. /// represented as such. Otherwise the twine is written into the given
  371. /// SmallVector and a StringRef to the SmallVector's data is returned.
  372. StringRef toStringRef(SmallVectorImpl<char> &Out) const;
  373. /// toNullTerminatedStringRef - This returns the twine as a single null
  374. /// terminated StringRef if it can be represented as such. Otherwise the
  375. /// twine is written into the given SmallVector and a StringRef to the
  376. /// SmallVector's data is returned.
  377. ///
  378. /// The returned StringRef's size does not include the null terminator.
  379. StringRef toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const;
  380. /// Write the concatenated string represented by this twine to the
  381. /// stream \p OS.
  382. void print(raw_ostream &OS) const;
  383. /// Dump the concatenated string represented by this twine to stderr.
  384. void dump() const;
  385. /// Write the representation of this twine to the stream \p OS.
  386. void printRepr(raw_ostream &OS) const;
  387. /// Dump the representation of this twine to stderr.
  388. void dumpRepr() const;
  389. /// @}
  390. };
  391. /// @name Twine Inline Implementations
  392. /// @{
  393. inline Twine Twine::concat(const Twine &Suffix) const {
  394. // Concatenation with null is null.
  395. if (isNull() || Suffix.isNull())
  396. return Twine(NullKind);
  397. // Concatenation with empty yields the other side.
  398. if (isEmpty())
  399. return Suffix;
  400. if (Suffix.isEmpty())
  401. return *this;
  402. // Otherwise we need to create a new node, taking care to fold in unary
  403. // twines.
  404. Child NewLHS, NewRHS;
  405. NewLHS.twine = this;
  406. NewRHS.twine = &Suffix;
  407. NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
  408. if (isUnary()) {
  409. NewLHS = LHS;
  410. NewLHSKind = getLHSKind();
  411. }
  412. if (Suffix.isUnary()) {
  413. NewRHS = Suffix.LHS;
  414. NewRHSKind = Suffix.getLHSKind();
  415. }
  416. return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
  417. }
  418. inline Twine operator+(const Twine &LHS, const Twine &RHS) {
  419. return LHS.concat(RHS);
  420. }
  421. /// Additional overload to guarantee simplified codegen; this is equivalent to
  422. /// concat().
  423. inline Twine operator+(const char *LHS, const StringRef &RHS) {
  424. return Twine(LHS, RHS);
  425. }
  426. /// Additional overload to guarantee simplified codegen; this is equivalent to
  427. /// concat().
  428. inline Twine operator+(const StringRef &LHS, const char *RHS) {
  429. return Twine(LHS, RHS);
  430. }
  431. inline raw_ostream &operator<<(raw_ostream &OS, const Twine &RHS) {
  432. RHS.print(OS);
  433. return OS;
  434. }
  435. /// @}
  436. }
  437. #endif