Twine.h 19 KB

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