|
| 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 | + |
| 18 | +namespace lldb_private { |
| 19 | + |
| 20 | +/// Stores information about where certain portions of a demangled |
| 21 | +/// function name begin and end. |
| 22 | +struct 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 lits. |
| 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 | + /// Returns \c true if this object holds a valid basename range. |
| 52 | + bool hasBasename() const { |
| 53 | + return BasenameRange.first != BasenameRange.second && |
| 54 | + BasenameRange.second > 0; |
| 55 | + } |
| 56 | + |
| 57 | + friend bool operator==(const DemangledNameInfo &lhs, |
| 58 | + const DemangledNameInfo &rhs) { |
| 59 | + return std::tie(lhs.BasenameRange, lhs.ArgumentsRange, lhs.ScopeRange, |
| 60 | + lhs.QualifiersRange) == |
| 61 | + std::tie(rhs.BasenameRange, rhs.ArgumentsRange, rhs.ScopeRange, |
| 62 | + lhs.QualifiersRange); |
| 63 | + } |
| 64 | + |
| 65 | + friend bool operator!=(const DemangledNameInfo &lhs, |
| 66 | + const DemangledNameInfo &rhs) { |
| 67 | + return !(lhs == rhs); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +/// An OutputBuffer which keeps a record of where certain parts of a |
| 72 | +/// demangled name begin/end (e.g., basename, scope, argument list, etc.). |
| 73 | +/// The tracking occurs during printing of the Itanium demangle tree. |
| 74 | +/// |
| 75 | +/// Usage: |
| 76 | +/// \code{.cpp} |
| 77 | +/// |
| 78 | +/// Node *N = mangling_parser.parseType(); |
| 79 | +/// |
| 80 | +/// TrackingOutputBuffer buffer; |
| 81 | +/// N->printLeft(OB); |
| 82 | +/// |
| 83 | +/// assert (buffer.NameInfo.hasBasename()); |
| 84 | +/// |
| 85 | +/// \endcode |
| 86 | +struct TrackingOutputBuffer : public llvm::itanium_demangle::OutputBuffer { |
| 87 | + using OutputBuffer::OutputBuffer; |
| 88 | + |
| 89 | + /// Holds information about the demangled name that is |
| 90 | + /// being printed into this buffer. |
| 91 | + DemangledNameInfo NameInfo; |
| 92 | + |
| 93 | + void printLeft(const llvm::itanium_demangle::Node &N) override; |
| 94 | + void printRight(const llvm::itanium_demangle::Node &N) override; |
| 95 | + |
| 96 | +private: |
| 97 | + void printLeftImpl(const llvm::itanium_demangle::FunctionType &N); |
| 98 | + void printRightImpl(const llvm::itanium_demangle::FunctionType &N); |
| 99 | + |
| 100 | + void printLeftImpl(const llvm::itanium_demangle::FunctionEncoding &N); |
| 101 | + void printRightImpl(const llvm::itanium_demangle::FunctionEncoding &N); |
| 102 | + |
| 103 | + void printLeftImpl(const llvm::itanium_demangle::NestedName &N); |
| 104 | + void printLeftImpl(const llvm::itanium_demangle::NameWithTemplateArgs &N); |
| 105 | + |
| 106 | + /// Called whenever we start printing a function type in the Itanium |
| 107 | + /// mangling scheme. Examples include \ref FunctionEncoding, \ref |
| 108 | + /// FunctionType, etc. |
| 109 | + /// |
| 110 | + /// \returns A ScopedOverride which will update the nesting depth of |
| 111 | + /// currently printed function types on destruction. |
| 112 | + [[nodiscard]] llvm::itanium_demangle::ScopedOverride<unsigned> |
| 113 | + enterFunctionTypePrinting(); |
| 114 | + |
| 115 | + /// Returns \c true if we're not printing any nested function types, |
| 116 | + /// just a \ref FunctionEncoding in the Itanium mangling scheme. |
| 117 | + bool isPrintingTopLevelFunctionType() const; |
| 118 | + |
| 119 | + /// If this object \ref shouldTrack, then update the end of |
| 120 | + /// the basename range to the current \c OB position. |
| 121 | + void updateBasenameEnd(); |
| 122 | + |
| 123 | + /// If this object \ref shouldTrack, then update the beginning |
| 124 | + /// of the scope range to the current \c OB position. |
| 125 | + void updateScopeStart(); |
| 126 | + |
| 127 | + /// If this object \ref shouldTrack, then update the end of |
| 128 | + /// the scope range to the current \c OB position. |
| 129 | + void updateScopeEnd(); |
| 130 | + |
| 131 | + /// Returns \c true if the members of this object can be |
| 132 | + /// updated. E.g., when we're printing nested template |
| 133 | + /// arguments, we don't need to be tracking basename |
| 134 | + /// locations. |
| 135 | + bool shouldTrack() const; |
| 136 | + |
| 137 | + /// Helpers called to track beginning and end of the function |
| 138 | + /// arguments. |
| 139 | + void finalizeArgumentEnd(); |
| 140 | + void finalizeStart(); |
| 141 | + void finalizeEnd(); |
| 142 | + |
| 143 | + /// Helper used in the finalize APIs. |
| 144 | + bool canFinalize() const; |
| 145 | + |
| 146 | + /// Incremented each time we start printing a function type node |
| 147 | + /// in the Itanium mangling scheme (e.g., \ref FunctionEncoding |
| 148 | + /// or \ref FunctionType). |
| 149 | + unsigned FunctionPrintingDepth = 0; |
| 150 | +}; |
| 151 | +} // namespace lldb_private |
| 152 | + |
| 153 | +#endif // LLDB_CORE_DEMANGLEDNAMEINFO_H |
0 commit comments