Utility.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //===--- Utility.h -------------------*- mode:c++;eval:(read-only-mode) -*-===//
  2. // Do not edit! See README.txt.
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-FileCopyrightText: Part of the LLVM Project
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Provide some utility classes for use in the demangler.
  11. // There are two copies of this file in the source tree. The one in libcxxabi
  12. // is the original and the one in llvm is the copy. Use cp-to-llvm.sh to update
  13. // the copy. See README.txt for more details.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef DEMANGLE_UTILITY_H
  17. #define DEMANGLE_UTILITY_H
  18. #include "DemangleConfig.h"
  19. #include <array>
  20. #include <cassert>
  21. #include <cstdint>
  22. #include <cstdlib>
  23. #include <cstring>
  24. #include <exception>
  25. #include <limits>
  26. #include <string_view>
  27. DEMANGLE_NAMESPACE_BEGIN
  28. // Stream that AST nodes write their string representation into after the AST
  29. // has been parsed.
  30. class OutputBuffer {
  31. char *Buffer = nullptr;
  32. size_t CurrentPosition = 0;
  33. size_t BufferCapacity = 0;
  34. // Ensure there are at least N more positions in the buffer.
  35. void grow(size_t N) {
  36. size_t Need = N + CurrentPosition;
  37. if (Need > BufferCapacity) {
  38. // Reduce the number of reallocations, with a bit of hysteresis. The
  39. // number here is chosen so the first allocation will more-than-likely not
  40. // allocate more than 1K.
  41. Need += 1024 - 32;
  42. BufferCapacity *= 2;
  43. if (BufferCapacity < Need)
  44. BufferCapacity = Need;
  45. Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity));
  46. if (Buffer == nullptr)
  47. std::terminate();
  48. }
  49. }
  50. OutputBuffer &writeUnsigned(uint64_t N, bool isNeg = false) {
  51. std::array<char, 21> Temp;
  52. char *TempPtr = Temp.data() + Temp.size();
  53. // Output at least one character.
  54. do {
  55. *--TempPtr = char('0' + N % 10);
  56. N /= 10;
  57. } while (N);
  58. // Add negative sign.
  59. if (isNeg)
  60. *--TempPtr = '-';
  61. return operator+=(
  62. std::string_view(TempPtr, Temp.data() + Temp.size() - TempPtr));
  63. }
  64. public:
  65. OutputBuffer(char *StartBuf, size_t Size)
  66. : Buffer(StartBuf), BufferCapacity(Size) {}
  67. OutputBuffer(char *StartBuf, size_t *SizePtr)
  68. : OutputBuffer(StartBuf, StartBuf ? *SizePtr : 0) {}
  69. OutputBuffer() = default;
  70. // Non-copyable
  71. OutputBuffer(const OutputBuffer &) = delete;
  72. OutputBuffer &operator=(const OutputBuffer &) = delete;
  73. operator std::string_view() const {
  74. return std::string_view(Buffer, CurrentPosition);
  75. }
  76. /// If a ParameterPackExpansion (or similar type) is encountered, the offset
  77. /// into the pack that we're currently printing.
  78. unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();
  79. unsigned CurrentPackMax = std::numeric_limits<unsigned>::max();
  80. /// When zero, we're printing template args and '>' needs to be parenthesized.
  81. /// Use a counter so we can simply increment inside parentheses.
  82. unsigned GtIsGt = 1;
  83. bool isGtInsideTemplateArgs() const { return GtIsGt == 0; }
  84. void printOpen(char Open = '(') {
  85. GtIsGt++;
  86. *this += Open;
  87. }
  88. void printClose(char Close = ')') {
  89. GtIsGt--;
  90. *this += Close;
  91. }
  92. OutputBuffer &operator+=(std::string_view R) {
  93. if (size_t Size = R.size()) {
  94. grow(Size);
  95. std::memcpy(Buffer + CurrentPosition, &*R.begin(), Size);
  96. CurrentPosition += Size;
  97. }
  98. return *this;
  99. }
  100. OutputBuffer &operator+=(char C) {
  101. grow(1);
  102. Buffer[CurrentPosition++] = C;
  103. return *this;
  104. }
  105. OutputBuffer &prepend(std::string_view R) {
  106. size_t Size = R.size();
  107. grow(Size);
  108. std::memmove(Buffer + Size, Buffer, CurrentPosition);
  109. std::memcpy(Buffer, &*R.begin(), Size);
  110. CurrentPosition += Size;
  111. return *this;
  112. }
  113. OutputBuffer &operator<<(std::string_view R) { return (*this += R); }
  114. OutputBuffer &operator<<(char C) { return (*this += C); }
  115. OutputBuffer &operator<<(long long N) {
  116. return writeUnsigned(static_cast<unsigned long long>(std::abs(N)), N < 0);
  117. }
  118. OutputBuffer &operator<<(unsigned long long N) {
  119. return writeUnsigned(N, false);
  120. }
  121. OutputBuffer &operator<<(long N) {
  122. return this->operator<<(static_cast<long long>(N));
  123. }
  124. OutputBuffer &operator<<(unsigned long N) {
  125. return this->operator<<(static_cast<unsigned long long>(N));
  126. }
  127. OutputBuffer &operator<<(int N) {
  128. return this->operator<<(static_cast<long long>(N));
  129. }
  130. OutputBuffer &operator<<(unsigned int N) {
  131. return this->operator<<(static_cast<unsigned long long>(N));
  132. }
  133. void insert(size_t Pos, const char *S, size_t N) {
  134. assert(Pos <= CurrentPosition);
  135. if (N == 0)
  136. return;
  137. grow(N);
  138. std::memmove(Buffer + Pos + N, Buffer + Pos, CurrentPosition - Pos);
  139. std::memcpy(Buffer + Pos, S, N);
  140. CurrentPosition += N;
  141. }
  142. size_t getCurrentPosition() const { return CurrentPosition; }
  143. void setCurrentPosition(size_t NewPos) { CurrentPosition = NewPos; }
  144. char back() const {
  145. assert(CurrentPosition);
  146. return Buffer[CurrentPosition - 1];
  147. }
  148. bool empty() const { return CurrentPosition == 0; }
  149. char *getBuffer() { return Buffer; }
  150. char *getBufferEnd() { return Buffer + CurrentPosition - 1; }
  151. size_t getBufferCapacity() const { return BufferCapacity; }
  152. };
  153. template <class T> class ScopedOverride {
  154. T &Loc;
  155. T Original;
  156. public:
  157. ScopedOverride(T &Loc_) : ScopedOverride(Loc_, Loc_) {}
  158. ScopedOverride(T &Loc_, T NewVal) : Loc(Loc_), Original(Loc_) {
  159. Loc_ = std::move(NewVal);
  160. }
  161. ~ScopedOverride() { Loc = std::move(Original); }
  162. ScopedOverride(const ScopedOverride &) = delete;
  163. ScopedOverride &operator=(const ScopedOverride &) = delete;
  164. };
  165. DEMANGLE_NAMESPACE_END
  166. #endif