1//===-- DemangledNameInfo.h -------------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_CORE_DEMANGLEDNAMEINFO_H
10#define LLDB_CORE_DEMANGLEDNAMEINFO_H
11
12#include "llvm/Demangle/ItaniumDemangle.h"
13#include "llvm/Demangle/Utility.h"
14
15#include <cstddef>
16#include <utility>
17
18namespace lldb_private {
19
20/// Stores information about where certain portions of a demangled
21/// function name begin and end.
22struct DemangledNameInfo {
23 /// A [start, end) pair for the function basename.
24 /// The basename is the name without scope qualifiers
25 /// and without template parameters. E.g.,
26 /// \code{.cpp}
27 /// void foo::bar<int>::someFunc<float>(int) const &&
28 /// ^ ^
29 /// start end
30 /// \endcode
31 std::pair<size_t, size_t> BasenameRange;
32
33 /// A [start, end) pair for the function scope qualifiers.
34 /// E.g., for
35 /// \code{.cpp}
36 /// void foo::bar<int>::qux<float>(int) const &&
37 /// ^ ^
38 /// start end
39 /// \endcode
40 std::pair<size_t, size_t> ScopeRange;
41
42 /// Indicates the [start, end) of the function argument list.
43 /// E.g.,
44 /// \code{.cpp}
45 /// int (*getFunc<float>(float, double))(int, int)
46 /// ^ ^
47 /// start end
48 /// \endcode
49 std::pair<size_t, size_t> ArgumentsRange;
50
51 /// Indicates the [start, end) of the function qualifiers
52 /// (e.g., CV-qualifiers, reference qualifiers, requires clauses).
53 ///
54 /// E.g.,
55 /// \code{.cpp}
56 /// void foo::bar<int>::qux<float>(int) const &&
57 /// ^ ^
58 /// start end
59 /// \endcode
60 std::pair<size_t, size_t> QualifiersRange;
61
62 /// Indicates the [start, end) of the function's prefix. This is a
63 /// catch-all range for anything that is not tracked by the rest of
64 /// the pairs.
65 std::pair<size_t, size_t> PrefixRange;
66
67 /// Indicates the [start, end) of the function's suffix. This is a
68 /// catch-all range for anything that is not tracked by the rest of
69 /// the pairs.
70 std::pair<size_t, size_t> SuffixRange;
71
72 /// Returns \c true if this object holds a valid basename range.
73 bool hasBasename() const {
74 return BasenameRange.second > BasenameRange.first;
75 }
76};
77
78/// An OutputBuffer which keeps a record of where certain parts of a
79/// demangled name begin/end (e.g., basename, scope, argument list, etc.).
80/// The tracking occurs during printing of the Itanium demangle tree.
81///
82/// Usage:
83/// \code{.cpp}
84///
85/// Node *N = mangling_parser.parseType();
86///
87/// TrackingOutputBuffer buffer;
88/// N->printLeft(OB);
89///
90/// assert (buffer.NameInfo.hasBasename());
91///
92/// \endcode
93struct TrackingOutputBuffer : public llvm::itanium_demangle::OutputBuffer {
94 using OutputBuffer::OutputBuffer;
95
96 /// Holds information about the demangled name that is
97 /// being printed into this buffer.
98 DemangledNameInfo NameInfo;
99
100 void printLeft(const llvm::itanium_demangle::Node &N) override;
101 void printRight(const llvm::itanium_demangle::Node &N) override;
102
103private:
104 void printLeftImpl(const llvm::itanium_demangle::FunctionType &N);
105 void printRightImpl(const llvm::itanium_demangle::FunctionType &N);
106
107 void printLeftImpl(const llvm::itanium_demangle::FunctionEncoding &N);
108 void printRightImpl(const llvm::itanium_demangle::FunctionEncoding &N);
109
110 void printLeftImpl(const llvm::itanium_demangle::NestedName &N);
111 void printLeftImpl(const llvm::itanium_demangle::NameWithTemplateArgs &N);
112
113 /// Called whenever we start printing a function type in the Itanium
114 /// mangling scheme. Examples include \ref FunctionEncoding, \ref
115 /// FunctionType, etc.
116 ///
117 /// \returns A ScopedOverride which will update the nesting depth of
118 /// currently printed function types on destruction.
119 [[nodiscard]] llvm::itanium_demangle::ScopedOverride<unsigned>
120 enterFunctionTypePrinting();
121
122 /// Returns \c true if we're not printing any nested function types,
123 /// just a \ref FunctionEncoding in the Itanium mangling scheme.
124 bool isPrintingTopLevelFunctionType() const;
125
126 /// If this object \ref shouldTrack, then update the end of
127 /// the basename range to the current \c OB position.
128 void updateBasenameEnd();
129
130 /// If this object \ref shouldTrack, then update the beginning
131 /// of the scope range to the current \c OB position.
132 void updateScopeStart();
133
134 /// If this object \ref shouldTrack, then update the end of
135 /// the scope range to the current \c OB position.
136 void updateScopeEnd();
137
138 /// Returns \c true if the members of this object can be
139 /// updated. E.g., when we're printing nested template
140 /// arguments, we don't need to be tracking basename
141 /// locations.
142 bool shouldTrack() const;
143
144 /// Helpers called to track beginning and end of the function
145 /// arguments.
146 void finalizeArgumentEnd();
147 void finalizeStart();
148 void finalizeEnd();
149 void finalizeQualifiersStart();
150 void finalizeQualifiersEnd();
151
152 /// Helper used in the finalize APIs.
153 bool canFinalize() const;
154
155 /// Incremented each time we start printing a function type node
156 /// in the Itanium mangling scheme (e.g., \ref FunctionEncoding
157 /// or \ref FunctionType).
158 unsigned FunctionPrintingDepth = 0;
159};
160} // namespace lldb_private
161
162#endif // LLDB_CORE_DEMANGLEDNAMEINFO_H
163

source code of lldb/include/lldb/Core/DemangledNameInfo.h