1 //===--- Utility.h ----------------------------------------------*- C++ -*-===//
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-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Provide some utility classes for use in the demangler.
10 // There are two copies of this file in the source tree. The one in libcxxabi
11 // is the original and the one in llvm is the copy. Use cp-to-llvm.sh to update
12 // the copy. See README.txt for more details.
14 //===----------------------------------------------------------------------===//
16 #ifndef DEMANGLE_UTILITY_H
17 #define DEMANGLE_UTILITY_H
19 #include "DemangleConfig.h"
27 #include <string_view>
29 DEMANGLE_NAMESPACE_BEGIN
31 // Stream that AST nodes write their string representation into after the AST
34 char *Buffer
= nullptr;
35 size_t CurrentPosition
= 0;
36 size_t BufferCapacity
= 0;
38 // Ensure there are at least N more positions in the buffer.
40 size_t Need
= N
+ CurrentPosition
;
41 if (Need
> BufferCapacity
) {
42 // Reduce the number of reallocations, with a bit of hysteresis. The
43 // number here is chosen so the first allocation will more-than-likely not
44 // allocate more than 1K.
47 if (BufferCapacity
< Need
)
48 BufferCapacity
= Need
;
49 Buffer
= static_cast<char *>(std::realloc(Buffer
, BufferCapacity
));
50 if (Buffer
== nullptr)
55 OutputBuffer
&writeUnsigned(uint64_t N
, bool isNeg
= false) {
56 std::array
<char, 21> Temp
;
57 char *TempPtr
= Temp
.data() + Temp
.size();
59 // Output at least one character.
61 *--TempPtr
= char('0' + N
% 10);
70 std::string_view(TempPtr
, Temp
.data() + Temp
.size() - TempPtr
));
74 OutputBuffer(char *StartBuf
, size_t Size
)
75 : Buffer(StartBuf
), BufferCapacity(Size
) {}
76 OutputBuffer(char *StartBuf
, size_t *SizePtr
)
77 : OutputBuffer(StartBuf
, StartBuf
? *SizePtr
: 0) {}
78 OutputBuffer() = default;
80 OutputBuffer(const OutputBuffer
&) = delete;
81 OutputBuffer
&operator=(const OutputBuffer
&) = delete;
83 operator std::string_view() const {
84 return std::string_view(Buffer
, CurrentPosition
);
87 /// If a ParameterPackExpansion (or similar type) is encountered, the offset
88 /// into the pack that we're currently printing.
89 unsigned CurrentPackIndex
= std::numeric_limits
<unsigned>::max();
90 unsigned CurrentPackMax
= std::numeric_limits
<unsigned>::max();
92 /// When zero, we're printing template args and '>' needs to be parenthesized.
93 /// Use a counter so we can simply increment inside parentheses.
96 bool isGtInsideTemplateArgs() const { return GtIsGt
== 0; }
98 void printOpen(char Open
= '(') {
102 void printClose(char Close
= ')') {
107 OutputBuffer
&operator+=(std::string_view R
) {
108 if (size_t Size
= R
.size()) {
110 std::memcpy(Buffer
+ CurrentPosition
, &*R
.begin(), Size
);
111 CurrentPosition
+= Size
;
116 OutputBuffer
&operator+=(char C
) {
118 Buffer
[CurrentPosition
++] = C
;
122 OutputBuffer
&prepend(std::string_view R
) {
123 size_t Size
= R
.size();
126 std::memmove(Buffer
+ Size
, Buffer
, CurrentPosition
);
127 std::memcpy(Buffer
, &*R
.begin(), Size
);
128 CurrentPosition
+= Size
;
133 OutputBuffer
&operator<<(std::string_view R
) { return (*this += R
); }
135 OutputBuffer
&operator<<(char C
) { return (*this += C
); }
137 OutputBuffer
&operator<<(long long N
) {
138 return writeUnsigned(static_cast<unsigned long long>(std::abs(N
)), N
< 0);
141 OutputBuffer
&operator<<(unsigned long long N
) {
142 return writeUnsigned(N
, false);
145 OutputBuffer
&operator<<(long N
) {
146 return this->operator<<(static_cast<long long>(N
));
149 OutputBuffer
&operator<<(unsigned long N
) {
150 return this->operator<<(static_cast<unsigned long long>(N
));
153 OutputBuffer
&operator<<(int N
) {
154 return this->operator<<(static_cast<long long>(N
));
157 OutputBuffer
&operator<<(unsigned int N
) {
158 return this->operator<<(static_cast<unsigned long long>(N
));
161 void insert(size_t Pos
, const char *S
, size_t N
) {
162 assert(Pos
<= CurrentPosition
);
166 std::memmove(Buffer
+ Pos
+ N
, Buffer
+ Pos
, CurrentPosition
- Pos
);
167 std::memcpy(Buffer
+ Pos
, S
, N
);
168 CurrentPosition
+= N
;
171 size_t getCurrentPosition() const { return CurrentPosition
; }
172 void setCurrentPosition(size_t NewPos
) { CurrentPosition
= NewPos
; }
175 assert(CurrentPosition
);
176 return Buffer
[CurrentPosition
- 1];
179 bool empty() const { return CurrentPosition
== 0; }
181 char *getBuffer() { return Buffer
; }
182 char *getBufferEnd() { return Buffer
+ CurrentPosition
- 1; }
183 size_t getBufferCapacity() const { return BufferCapacity
; }
186 template <class T
> class ScopedOverride
{
191 ScopedOverride(T
&Loc_
) : ScopedOverride(Loc_
, Loc_
) {}
193 ScopedOverride(T
&Loc_
, T NewVal
) : Loc(Loc_
), Original(Loc_
) {
194 Loc_
= std::move(NewVal
);
196 ~ScopedOverride() { Loc
= std::move(Original
); }
198 ScopedOverride(const ScopedOverride
&) = delete;
199 ScopedOverride
&operator=(const ScopedOverride
&) = delete;
202 DEMANGLE_NAMESPACE_END