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 "StringView.h"
27 DEMANGLE_NAMESPACE_BEGIN
29 // Stream that AST nodes write their string representation into after the AST
32 char *Buffer
= nullptr;
33 size_t CurrentPosition
= 0;
34 size_t BufferCapacity
= 0;
36 // Ensure there are at least N more positions in the buffer.
38 size_t Need
= N
+ CurrentPosition
;
39 if (Need
> BufferCapacity
) {
40 // Reduce the number of reallocations, with a bit of hysteresis. The
41 // number here is chosen so the first allocation will more-than-likely not
42 // allocate more than 1K.
45 if (BufferCapacity
< Need
)
46 BufferCapacity
= Need
;
47 Buffer
= static_cast<char *>(std::realloc(Buffer
, BufferCapacity
));
48 if (Buffer
== nullptr)
53 OutputBuffer
&writeUnsigned(uint64_t N
, bool isNeg
= false) {
54 std::array
<char, 21> Temp
;
55 char *TempPtr
= Temp
.data() + Temp
.size();
57 // Output at least one character.
59 *--TempPtr
= char('0' + N
% 10);
67 return operator+=(StringView(TempPtr
, Temp
.data() + Temp
.size()));
71 OutputBuffer(char *StartBuf
, size_t Size
)
72 : Buffer(StartBuf
), BufferCapacity(Size
) {}
73 OutputBuffer(char *StartBuf
, size_t *SizePtr
)
74 : OutputBuffer(StartBuf
, StartBuf
? *SizePtr
: 0) {}
75 OutputBuffer() = default;
77 OutputBuffer(const OutputBuffer
&) = delete;
78 OutputBuffer
&operator=(const OutputBuffer
&) = delete;
80 operator StringView() const { return StringView(Buffer
, CurrentPosition
); }
82 /// If a ParameterPackExpansion (or similar type) is encountered, the offset
83 /// into the pack that we're currently printing.
84 unsigned CurrentPackIndex
= std::numeric_limits
<unsigned>::max();
85 unsigned CurrentPackMax
= std::numeric_limits
<unsigned>::max();
87 /// When zero, we're printing template args and '>' needs to be parenthesized.
88 /// Use a counter so we can simply increment inside parentheses.
91 bool isGtInsideTemplateArgs() const { return GtIsGt
== 0; }
93 void printOpen(char Open
= '(') {
97 void printClose(char Close
= ')') {
102 OutputBuffer
&operator+=(StringView R
) {
103 if (size_t Size
= R
.size()) {
105 std::memcpy(Buffer
+ CurrentPosition
, R
.begin(), Size
);
106 CurrentPosition
+= Size
;
111 OutputBuffer
&operator+=(char C
) {
113 Buffer
[CurrentPosition
++] = C
;
117 OutputBuffer
&prepend(StringView R
) {
118 size_t Size
= R
.size();
121 std::memmove(Buffer
+ Size
, Buffer
, CurrentPosition
);
122 std::memcpy(Buffer
, R
.begin(), Size
);
123 CurrentPosition
+= Size
;
128 OutputBuffer
&operator<<(StringView R
) { return (*this += R
); }
130 OutputBuffer
&operator<<(char C
) { return (*this += C
); }
132 OutputBuffer
&operator<<(long long N
) {
133 return writeUnsigned(static_cast<unsigned long long>(std::abs(N
)), N
< 0);
136 OutputBuffer
&operator<<(unsigned long long N
) {
137 return writeUnsigned(N
, false);
140 OutputBuffer
&operator<<(long N
) {
141 return this->operator<<(static_cast<long long>(N
));
144 OutputBuffer
&operator<<(unsigned long N
) {
145 return this->operator<<(static_cast<unsigned long long>(N
));
148 OutputBuffer
&operator<<(int N
) {
149 return this->operator<<(static_cast<long long>(N
));
152 OutputBuffer
&operator<<(unsigned int N
) {
153 return this->operator<<(static_cast<unsigned long long>(N
));
156 void insert(size_t Pos
, const char *S
, size_t N
) {
157 assert(Pos
<= CurrentPosition
);
161 std::memmove(Buffer
+ Pos
+ N
, Buffer
+ Pos
, CurrentPosition
- Pos
);
162 std::memcpy(Buffer
+ Pos
, S
, N
);
163 CurrentPosition
+= N
;
166 size_t getCurrentPosition() const { return CurrentPosition
; }
167 void setCurrentPosition(size_t NewPos
) { CurrentPosition
= NewPos
; }
170 assert(CurrentPosition
);
171 return Buffer
[CurrentPosition
- 1];
174 bool empty() const { return CurrentPosition
== 0; }
176 char *getBuffer() { return Buffer
; }
177 char *getBufferEnd() { return Buffer
+ CurrentPosition
- 1; }
178 size_t getBufferCapacity() const { return BufferCapacity
; }
181 template <class T
> class ScopedOverride
{
186 ScopedOverride(T
&Loc_
) : ScopedOverride(Loc_
, Loc_
) {}
188 ScopedOverride(T
&Loc_
, T NewVal
) : Loc(Loc_
), Original(Loc_
) {
189 Loc_
= std::move(NewVal
);
191 ~ScopedOverride() { Loc
= std::move(Original
); }
193 ScopedOverride(const ScopedOverride
&) = delete;
194 ScopedOverride
&operator=(const ScopedOverride
&) = delete;
197 DEMANGLE_NAMESPACE_END