Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1 | //===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 8ee27c3 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Nathan Sidwell | 5b0a8cf | 2022-01-24 06:38:47 -0800 | [diff] [blame] | 9 | // Generic itanium demangler library. |
| 10 | // There are two copies of this file in the source tree. The one under |
| 11 | // libcxxabi is the original and the one under llvm is the copy. Use |
| 12 | // cp-to-llvm.sh to update the copy. See README.txt for more details. |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 16 | #ifndef DEMANGLE_ITANIUMDEMANGLE_H |
| 17 | #define DEMANGLE_ITANIUMDEMANGLE_H |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 18 | |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 19 | #include "DemangleConfig.h" |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 20 | #include "StringView.h" |
| 21 | #include "Utility.h" |
Nathan Sidwell | bbc632f | 2022-01-24 04:11:59 -0800 | [diff] [blame] | 22 | #include <algorithm> |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 23 | #include <cassert> |
| 24 | #include <cctype> |
| 25 | #include <cstdio> |
| 26 | #include <cstdlib> |
| 27 | #include <cstring> |
Nathan Sidwell | bbc632f | 2022-01-24 04:11:59 -0800 | [diff] [blame] | 28 | #include <limits> |
Nikolas Klauser | 6285577 | 2022-09-05 00:01:15 +0200 | [diff] [blame] | 29 | #include <new> |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 30 | #include <utility> |
| 31 | |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 32 | DEMANGLE_NAMESPACE_BEGIN |
| 33 | |
Mikhail Borisov | 8452f06 | 2021-08-17 18:06:53 -0400 | [diff] [blame] | 34 | template <class T, size_t N> class PODSmallVector { |
| 35 | static_assert(std::is_pod<T>::value, |
| 36 | "T is required to be a plain old data type"); |
| 37 | |
| 38 | T *First = nullptr; |
| 39 | T *Last = nullptr; |
| 40 | T *Cap = nullptr; |
| 41 | T Inline[N] = {0}; |
| 42 | |
| 43 | bool isInline() const { return First == Inline; } |
| 44 | |
| 45 | void clearInline() { |
| 46 | First = Inline; |
| 47 | Last = Inline; |
| 48 | Cap = Inline + N; |
| 49 | } |
| 50 | |
| 51 | void reserve(size_t NewCap) { |
| 52 | size_t S = size(); |
| 53 | if (isInline()) { |
| 54 | auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T))); |
| 55 | if (Tmp == nullptr) |
| 56 | std::terminate(); |
| 57 | std::copy(First, Last, Tmp); |
| 58 | First = Tmp; |
| 59 | } else { |
| 60 | First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T))); |
| 61 | if (First == nullptr) |
| 62 | std::terminate(); |
| 63 | } |
| 64 | Last = First + S; |
| 65 | Cap = First + NewCap; |
| 66 | } |
| 67 | |
| 68 | public: |
| 69 | PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {} |
| 70 | |
| 71 | PODSmallVector(const PODSmallVector &) = delete; |
| 72 | PODSmallVector &operator=(const PODSmallVector &) = delete; |
| 73 | |
| 74 | PODSmallVector(PODSmallVector &&Other) : PODSmallVector() { |
| 75 | if (Other.isInline()) { |
| 76 | std::copy(Other.begin(), Other.end(), First); |
| 77 | Last = First + Other.size(); |
| 78 | Other.clear(); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | First = Other.First; |
| 83 | Last = Other.Last; |
| 84 | Cap = Other.Cap; |
| 85 | Other.clearInline(); |
| 86 | } |
| 87 | |
| 88 | PODSmallVector &operator=(PODSmallVector &&Other) { |
| 89 | if (Other.isInline()) { |
| 90 | if (!isInline()) { |
| 91 | std::free(First); |
| 92 | clearInline(); |
| 93 | } |
| 94 | std::copy(Other.begin(), Other.end(), First); |
| 95 | Last = First + Other.size(); |
| 96 | Other.clear(); |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | if (isInline()) { |
| 101 | First = Other.First; |
| 102 | Last = Other.Last; |
| 103 | Cap = Other.Cap; |
| 104 | Other.clearInline(); |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | std::swap(First, Other.First); |
| 109 | std::swap(Last, Other.Last); |
| 110 | std::swap(Cap, Other.Cap); |
| 111 | Other.clear(); |
| 112 | return *this; |
| 113 | } |
| 114 | |
| 115 | // NOLINTNEXTLINE(readability-identifier-naming) |
| 116 | void push_back(const T &Elem) { |
| 117 | if (Last == Cap) |
| 118 | reserve(size() * 2); |
| 119 | *Last++ = Elem; |
| 120 | } |
| 121 | |
| 122 | // NOLINTNEXTLINE(readability-identifier-naming) |
| 123 | void pop_back() { |
| 124 | assert(Last != First && "Popping empty vector!"); |
| 125 | --Last; |
| 126 | } |
| 127 | |
| 128 | void dropBack(size_t Index) { |
| 129 | assert(Index <= size() && "dropBack() can't expand!"); |
| 130 | Last = First + Index; |
| 131 | } |
| 132 | |
| 133 | T *begin() { return First; } |
| 134 | T *end() { return Last; } |
| 135 | |
| 136 | bool empty() const { return First == Last; } |
| 137 | size_t size() const { return static_cast<size_t>(Last - First); } |
| 138 | T &back() { |
| 139 | assert(Last != First && "Calling back() on empty vector!"); |
| 140 | return *(Last - 1); |
| 141 | } |
| 142 | T &operator[](size_t Index) { |
| 143 | assert(Index < size() && "Invalid access!"); |
| 144 | return *(begin() + Index); |
| 145 | } |
| 146 | void clear() { Last = First; } |
| 147 | |
| 148 | ~PODSmallVector() { |
| 149 | if (!isInline()) |
| 150 | std::free(First); |
| 151 | } |
| 152 | }; |
| 153 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 154 | // Base class of all AST nodes. The AST is built by the parser, then is |
| 155 | // traversed by the printLeft/Right functions to produce a demangled string. |
| 156 | class Node { |
| 157 | public: |
| 158 | enum Kind : unsigned char { |
Nathan Sidwell | 8b55cc0 | 2022-03-30 05:59:16 -0700 | [diff] [blame] | 159 | #define NODE(NodeKind) K##NodeKind, |
| 160 | #include "ItaniumNodes.def" |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | /// Three-way bool to track a cached value. Unknown is possible if this node |
| 164 | /// has an unexpanded parameter pack below it that may affect this cache. |
| 165 | enum class Cache : unsigned char { Yes, No, Unknown, }; |
| 166 | |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 167 | /// Operator precedence for expression nodes. Used to determine required |
| 168 | /// parens in expression emission. |
| 169 | enum class Prec { |
| 170 | Primary, |
| 171 | Postfix, |
| 172 | Unary, |
| 173 | Cast, |
| 174 | PtrMem, |
| 175 | Multiplicative, |
| 176 | Additive, |
| 177 | Shift, |
| 178 | Spaceship, |
| 179 | Relational, |
| 180 | Equality, |
| 181 | And, |
| 182 | Xor, |
| 183 | Ior, |
| 184 | AndIf, |
| 185 | OrIf, |
| 186 | Conditional, |
| 187 | Assign, |
| 188 | Comma, |
| 189 | Default, |
| 190 | }; |
| 191 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 192 | private: |
| 193 | Kind K; |
| 194 | |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 195 | Prec Precedence : 6; |
| 196 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 197 | // FIXME: Make these protected. |
| 198 | public: |
| 199 | /// Tracks if this node has a component on its right side, in which case we |
| 200 | /// need to call printRight. |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 201 | Cache RHSComponentCache : 2; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 202 | |
| 203 | /// Track if this node is a (possibly qualified) array type. This can affect |
| 204 | /// how we format the output string. |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 205 | Cache ArrayCache : 2; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 206 | |
| 207 | /// Track if this node is a (possibly qualified) function type. This can |
| 208 | /// affect how we format the output string. |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 209 | Cache FunctionCache : 2; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 210 | |
| 211 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 212 | Node(Kind K_, Prec Precedence_ = Prec::Primary, |
| 213 | Cache RHSComponentCache_ = Cache::No, Cache ArrayCache_ = Cache::No, |
| 214 | Cache FunctionCache_ = Cache::No) |
| 215 | : K(K_), Precedence(Precedence_), RHSComponentCache(RHSComponentCache_), |
| 216 | ArrayCache(ArrayCache_), FunctionCache(FunctionCache_) {} |
| 217 | Node(Kind K_, Cache RHSComponentCache_, Cache ArrayCache_ = Cache::No, |
| 218 | Cache FunctionCache_ = Cache::No) |
| 219 | : Node(K_, Prec::Primary, RHSComponentCache_, ArrayCache_, |
| 220 | FunctionCache_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 221 | |
| 222 | /// Visit the most-derived object corresponding to this object. |
| 223 | template<typename Fn> void visit(Fn F) const; |
| 224 | |
| 225 | // The following function is provided by all derived classes: |
| 226 | // |
| 227 | // Call F with arguments that, when passed to the constructor of this node, |
| 228 | // would construct an equivalent node. |
| 229 | //template<typename Fn> void match(Fn F) const; |
| 230 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 231 | bool hasRHSComponent(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 232 | if (RHSComponentCache != Cache::Unknown) |
| 233 | return RHSComponentCache == Cache::Yes; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 234 | return hasRHSComponentSlow(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 235 | } |
| 236 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 237 | bool hasArray(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 238 | if (ArrayCache != Cache::Unknown) |
| 239 | return ArrayCache == Cache::Yes; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 240 | return hasArraySlow(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 241 | } |
| 242 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 243 | bool hasFunction(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 244 | if (FunctionCache != Cache::Unknown) |
| 245 | return FunctionCache == Cache::Yes; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 246 | return hasFunctionSlow(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | Kind getKind() const { return K; } |
| 250 | |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 251 | Prec getPrecedence() const { return Precedence; } |
| 252 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 253 | virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; } |
| 254 | virtual bool hasArraySlow(OutputBuffer &) const { return false; } |
| 255 | virtual bool hasFunctionSlow(OutputBuffer &) const { return false; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 256 | |
| 257 | // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to |
| 258 | // get at a node that actually represents some concrete syntax. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 259 | virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 260 | |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 261 | // Print this node as an expression operand, surrounding it in parentheses if |
| 262 | // its precedence is [Strictly] weaker than P. |
| 263 | void printAsOperand(OutputBuffer &OB, Prec P = Prec::Default, |
| 264 | bool StrictlyWorse = false) const { |
| 265 | bool Paren = |
| 266 | unsigned(getPrecedence()) >= unsigned(P) + unsigned(StrictlyWorse); |
| 267 | if (Paren) |
| 268 | OB.printOpen(); |
| 269 | print(OB); |
| 270 | if (Paren) |
| 271 | OB.printClose(); |
| 272 | } |
| 273 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 274 | void print(OutputBuffer &OB) const { |
| 275 | printLeft(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 276 | if (RHSComponentCache != Cache::No) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 277 | printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Nathan Sidwell | fd0ef6d | 2022-01-20 07:40:12 -0800 | [diff] [blame] | 280 | // Print the "left" side of this Node into OutputBuffer. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 281 | virtual void printLeft(OutputBuffer &) const = 0; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 282 | |
| 283 | // Print the "right". This distinction is necessary to represent C++ types |
| 284 | // that appear on the RHS of their subtype, such as arrays or functions. |
| 285 | // Since most types don't have such a component, provide a default |
| 286 | // implementation. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 287 | virtual void printRight(OutputBuffer &) const {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 288 | |
| 289 | virtual StringView getBaseName() const { return StringView(); } |
| 290 | |
| 291 | // Silence compiler warnings, this dtor will never be called. |
| 292 | virtual ~Node() = default; |
| 293 | |
| 294 | #ifndef NDEBUG |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 295 | DEMANGLE_DUMP_METHOD void dump() const; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 296 | #endif |
| 297 | }; |
| 298 | |
| 299 | class NodeArray { |
| 300 | Node **Elements; |
| 301 | size_t NumElements; |
| 302 | |
| 303 | public: |
| 304 | NodeArray() : Elements(nullptr), NumElements(0) {} |
| 305 | NodeArray(Node **Elements_, size_t NumElements_) |
| 306 | : Elements(Elements_), NumElements(NumElements_) {} |
| 307 | |
| 308 | bool empty() const { return NumElements == 0; } |
| 309 | size_t size() const { return NumElements; } |
| 310 | |
| 311 | Node **begin() const { return Elements; } |
| 312 | Node **end() const { return Elements + NumElements; } |
| 313 | |
| 314 | Node *operator[](size_t Idx) const { return Elements[Idx]; } |
| 315 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 316 | void printWithComma(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 317 | bool FirstElement = true; |
| 318 | for (size_t Idx = 0; Idx != NumElements; ++Idx) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 319 | size_t BeforeComma = OB.getCurrentPosition(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 320 | if (!FirstElement) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 321 | OB += ", "; |
| 322 | size_t AfterComma = OB.getCurrentPosition(); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 323 | Elements[Idx]->printAsOperand(OB, Node::Prec::Comma); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 324 | |
| 325 | // Elements[Idx] is an empty parameter pack expansion, we should erase the |
| 326 | // comma we just printed. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 327 | if (AfterComma == OB.getCurrentPosition()) { |
| 328 | OB.setCurrentPosition(BeforeComma); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 329 | continue; |
| 330 | } |
| 331 | |
| 332 | FirstElement = false; |
| 333 | } |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | struct NodeArrayNode : Node { |
| 338 | NodeArray Array; |
| 339 | NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {} |
| 340 | |
| 341 | template<typename Fn> void match(Fn F) const { F(Array); } |
| 342 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 343 | void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | class DotSuffix final : public Node { |
| 347 | const Node *Prefix; |
| 348 | const StringView Suffix; |
| 349 | |
| 350 | public: |
| 351 | DotSuffix(const Node *Prefix_, StringView Suffix_) |
| 352 | : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {} |
| 353 | |
| 354 | template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); } |
| 355 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 356 | void printLeft(OutputBuffer &OB) const override { |
| 357 | Prefix->print(OB); |
| 358 | OB += " ("; |
| 359 | OB += Suffix; |
| 360 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 361 | } |
| 362 | }; |
| 363 | |
| 364 | class VendorExtQualType final : public Node { |
| 365 | const Node *Ty; |
| 366 | StringView Ext; |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 367 | const Node *TA; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 368 | |
| 369 | public: |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 370 | VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_) |
| 371 | : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 372 | |
Nathan Sidwell | eb9fece | 2022-10-07 07:03:32 -0700 | [diff] [blame^] | 373 | const Node *getTy() const { return Ty; } |
| 374 | StringView getExt() const { return Ext; } |
| 375 | const Node *getTA() const { return TA; } |
| 376 | |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 377 | template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 378 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 379 | void printLeft(OutputBuffer &OB) const override { |
| 380 | Ty->print(OB); |
| 381 | OB += " "; |
| 382 | OB += Ext; |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 383 | if (TA != nullptr) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 384 | TA->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 385 | } |
| 386 | }; |
| 387 | |
| 388 | enum FunctionRefQual : unsigned char { |
| 389 | FrefQualNone, |
| 390 | FrefQualLValue, |
| 391 | FrefQualRValue, |
| 392 | }; |
| 393 | |
| 394 | enum Qualifiers { |
| 395 | QualNone = 0, |
| 396 | QualConst = 0x1, |
| 397 | QualVolatile = 0x2, |
| 398 | QualRestrict = 0x4, |
| 399 | }; |
| 400 | |
| 401 | inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) { |
| 402 | return Q1 = static_cast<Qualifiers>(Q1 | Q2); |
| 403 | } |
| 404 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 405 | class QualType final : public Node { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 406 | protected: |
| 407 | const Qualifiers Quals; |
| 408 | const Node *Child; |
| 409 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 410 | void printQuals(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 411 | if (Quals & QualConst) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 412 | OB += " const"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 413 | if (Quals & QualVolatile) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 414 | OB += " volatile"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 415 | if (Quals & QualRestrict) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 416 | OB += " restrict"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | public: |
| 420 | QualType(const Node *Child_, Qualifiers Quals_) |
| 421 | : Node(KQualType, Child_->RHSComponentCache, |
| 422 | Child_->ArrayCache, Child_->FunctionCache), |
| 423 | Quals(Quals_), Child(Child_) {} |
| 424 | |
Nathan Sidwell | eb9fece | 2022-10-07 07:03:32 -0700 | [diff] [blame^] | 425 | Qualifiers getQuals() const { return Quals; } |
| 426 | const Node *getChild() const { return Child; } |
| 427 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 428 | template<typename Fn> void match(Fn F) const { F(Child, Quals); } |
| 429 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 430 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
| 431 | return Child->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 432 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 433 | bool hasArraySlow(OutputBuffer &OB) const override { |
| 434 | return Child->hasArray(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 435 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 436 | bool hasFunctionSlow(OutputBuffer &OB) const override { |
| 437 | return Child->hasFunction(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 438 | } |
| 439 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 440 | void printLeft(OutputBuffer &OB) const override { |
| 441 | Child->printLeft(OB); |
| 442 | printQuals(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 443 | } |
| 444 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 445 | void printRight(OutputBuffer &OB) const override { Child->printRight(OB); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 446 | }; |
| 447 | |
| 448 | class ConversionOperatorType final : public Node { |
| 449 | const Node *Ty; |
| 450 | |
| 451 | public: |
| 452 | ConversionOperatorType(const Node *Ty_) |
| 453 | : Node(KConversionOperatorType), Ty(Ty_) {} |
| 454 | |
| 455 | template<typename Fn> void match(Fn F) const { F(Ty); } |
| 456 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 457 | void printLeft(OutputBuffer &OB) const override { |
| 458 | OB += "operator "; |
| 459 | Ty->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 460 | } |
| 461 | }; |
| 462 | |
| 463 | class PostfixQualifiedType final : public Node { |
| 464 | const Node *Ty; |
| 465 | const StringView Postfix; |
| 466 | |
| 467 | public: |
Nathan Sidwell | e9c9bdf | 2022-03-29 06:19:18 -0700 | [diff] [blame] | 468 | PostfixQualifiedType(const Node *Ty_, StringView Postfix_) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 469 | : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {} |
| 470 | |
| 471 | template<typename Fn> void match(Fn F) const { F(Ty, Postfix); } |
| 472 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 473 | void printLeft(OutputBuffer &OB) const override { |
| 474 | Ty->printLeft(OB); |
| 475 | OB += Postfix; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 476 | } |
| 477 | }; |
| 478 | |
| 479 | class NameType final : public Node { |
| 480 | const StringView Name; |
| 481 | |
| 482 | public: |
| 483 | NameType(StringView Name_) : Node(KNameType), Name(Name_) {} |
| 484 | |
| 485 | template<typename Fn> void match(Fn F) const { F(Name); } |
| 486 | |
| 487 | StringView getName() const { return Name; } |
| 488 | StringView getBaseName() const override { return Name; } |
| 489 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 490 | void printLeft(OutputBuffer &OB) const override { OB += Name; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 491 | }; |
| 492 | |
Senran Zhang | e025ba5 | 2022-03-27 00:04:23 +0800 | [diff] [blame] | 493 | class BitIntType final : public Node { |
| 494 | const Node *Size; |
| 495 | bool Signed; |
| 496 | |
| 497 | public: |
| 498 | BitIntType(const Node *Size_, bool Signed_) |
| 499 | : Node(KBitIntType), Size(Size_), Signed(Signed_) {} |
| 500 | |
| 501 | template <typename Fn> void match(Fn F) const { F(Size, Signed); } |
| 502 | |
| 503 | void printLeft(OutputBuffer &OB) const override { |
| 504 | if (!Signed) |
| 505 | OB += "unsigned "; |
| 506 | OB += "_BitInt"; |
| 507 | OB.printOpen(); |
| 508 | Size->printAsOperand(OB); |
| 509 | OB.printClose(); |
| 510 | } |
| 511 | }; |
| 512 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 513 | class ElaboratedTypeSpefType : public Node { |
| 514 | StringView Kind; |
| 515 | Node *Child; |
| 516 | public: |
| 517 | ElaboratedTypeSpefType(StringView Kind_, Node *Child_) |
| 518 | : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {} |
| 519 | |
| 520 | template<typename Fn> void match(Fn F) const { F(Kind, Child); } |
| 521 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 522 | void printLeft(OutputBuffer &OB) const override { |
| 523 | OB += Kind; |
| 524 | OB += ' '; |
| 525 | Child->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 526 | } |
| 527 | }; |
| 528 | |
| 529 | struct AbiTagAttr : Node { |
| 530 | Node *Base; |
| 531 | StringView Tag; |
| 532 | |
| 533 | AbiTagAttr(Node* Base_, StringView Tag_) |
| 534 | : Node(KAbiTagAttr, Base_->RHSComponentCache, |
| 535 | Base_->ArrayCache, Base_->FunctionCache), |
| 536 | Base(Base_), Tag(Tag_) {} |
| 537 | |
| 538 | template<typename Fn> void match(Fn F) const { F(Base, Tag); } |
| 539 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 540 | void printLeft(OutputBuffer &OB) const override { |
| 541 | Base->printLeft(OB); |
| 542 | OB += "[abi:"; |
| 543 | OB += Tag; |
| 544 | OB += "]"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 545 | } |
| 546 | }; |
| 547 | |
| 548 | class EnableIfAttr : public Node { |
| 549 | NodeArray Conditions; |
| 550 | public: |
| 551 | EnableIfAttr(NodeArray Conditions_) |
| 552 | : Node(KEnableIfAttr), Conditions(Conditions_) {} |
| 553 | |
| 554 | template<typename Fn> void match(Fn F) const { F(Conditions); } |
| 555 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 556 | void printLeft(OutputBuffer &OB) const override { |
| 557 | OB += " [enable_if:"; |
| 558 | Conditions.printWithComma(OB); |
| 559 | OB += ']'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 560 | } |
| 561 | }; |
| 562 | |
| 563 | class ObjCProtoName : public Node { |
| 564 | const Node *Ty; |
| 565 | StringView Protocol; |
| 566 | |
| 567 | friend class PointerType; |
| 568 | |
| 569 | public: |
| 570 | ObjCProtoName(const Node *Ty_, StringView Protocol_) |
| 571 | : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {} |
| 572 | |
| 573 | template<typename Fn> void match(Fn F) const { F(Ty, Protocol); } |
| 574 | |
| 575 | bool isObjCObject() const { |
| 576 | return Ty->getKind() == KNameType && |
| 577 | static_cast<const NameType *>(Ty)->getName() == "objc_object"; |
| 578 | } |
| 579 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 580 | void printLeft(OutputBuffer &OB) const override { |
| 581 | Ty->print(OB); |
| 582 | OB += "<"; |
| 583 | OB += Protocol; |
| 584 | OB += ">"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 585 | } |
| 586 | }; |
| 587 | |
| 588 | class PointerType final : public Node { |
| 589 | const Node *Pointee; |
| 590 | |
| 591 | public: |
| 592 | PointerType(const Node *Pointee_) |
| 593 | : Node(KPointerType, Pointee_->RHSComponentCache), |
| 594 | Pointee(Pointee_) {} |
| 595 | |
Nathan Sidwell | eb9fece | 2022-10-07 07:03:32 -0700 | [diff] [blame^] | 596 | const Node *getPointee() const { return Pointee; } |
| 597 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 598 | template<typename Fn> void match(Fn F) const { F(Pointee); } |
| 599 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 600 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
| 601 | return Pointee->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 602 | } |
| 603 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 604 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 605 | // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>. |
| 606 | if (Pointee->getKind() != KObjCProtoName || |
| 607 | !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 608 | Pointee->printLeft(OB); |
| 609 | if (Pointee->hasArray(OB)) |
| 610 | OB += " "; |
| 611 | if (Pointee->hasArray(OB) || Pointee->hasFunction(OB)) |
| 612 | OB += "("; |
| 613 | OB += "*"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 614 | } else { |
| 615 | const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 616 | OB += "id<"; |
| 617 | OB += objcProto->Protocol; |
| 618 | OB += ">"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 622 | void printRight(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 623 | if (Pointee->getKind() != KObjCProtoName || |
| 624 | !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 625 | if (Pointee->hasArray(OB) || Pointee->hasFunction(OB)) |
| 626 | OB += ")"; |
| 627 | Pointee->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | }; |
| 631 | |
| 632 | enum class ReferenceKind { |
| 633 | LValue, |
| 634 | RValue, |
| 635 | }; |
| 636 | |
| 637 | // Represents either a LValue or an RValue reference type. |
| 638 | class ReferenceType : public Node { |
| 639 | const Node *Pointee; |
| 640 | ReferenceKind RK; |
| 641 | |
| 642 | mutable bool Printing = false; |
| 643 | |
| 644 | // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The |
| 645 | // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any |
| 646 | // other combination collapses to a lvalue ref. |
Mikhail Borisov | 05f7722 | 2021-08-17 18:10:57 -0400 | [diff] [blame] | 647 | // |
| 648 | // A combination of a TemplateForwardReference and a back-ref Substitution |
| 649 | // from an ill-formed string may have created a cycle; use cycle detection to |
| 650 | // avoid looping forever. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 651 | std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 652 | auto SoFar = std::make_pair(RK, Pointee); |
Mikhail Borisov | 05f7722 | 2021-08-17 18:10:57 -0400 | [diff] [blame] | 653 | // Track the chain of nodes for the Floyd's 'tortoise and hare' |
| 654 | // cycle-detection algorithm, since getSyntaxNode(S) is impure |
| 655 | PODSmallVector<const Node *, 8> Prev; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 656 | for (;;) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 657 | const Node *SN = SoFar.second->getSyntaxNode(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 658 | if (SN->getKind() != KReferenceType) |
| 659 | break; |
| 660 | auto *RT = static_cast<const ReferenceType *>(SN); |
| 661 | SoFar.second = RT->Pointee; |
| 662 | SoFar.first = std::min(SoFar.first, RT->RK); |
Mikhail Borisov | 05f7722 | 2021-08-17 18:10:57 -0400 | [diff] [blame] | 663 | |
| 664 | // The middle of Prev is the 'slow' pointer moving at half speed |
| 665 | Prev.push_back(SoFar.second); |
| 666 | if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) { |
| 667 | // Cycle detected |
| 668 | SoFar.second = nullptr; |
| 669 | break; |
| 670 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 671 | } |
| 672 | return SoFar; |
| 673 | } |
| 674 | |
| 675 | public: |
| 676 | ReferenceType(const Node *Pointee_, ReferenceKind RK_) |
| 677 | : Node(KReferenceType, Pointee_->RHSComponentCache), |
| 678 | Pointee(Pointee_), RK(RK_) {} |
| 679 | |
| 680 | template<typename Fn> void match(Fn F) const { F(Pointee, RK); } |
| 681 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 682 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
| 683 | return Pointee->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 684 | } |
| 685 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 686 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 687 | if (Printing) |
| 688 | return; |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 689 | ScopedOverride<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 690 | std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB); |
Mikhail Borisov | 05f7722 | 2021-08-17 18:10:57 -0400 | [diff] [blame] | 691 | if (!Collapsed.second) |
| 692 | return; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 693 | Collapsed.second->printLeft(OB); |
| 694 | if (Collapsed.second->hasArray(OB)) |
| 695 | OB += " "; |
| 696 | if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB)) |
| 697 | OB += "("; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 698 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 699 | OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 700 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 701 | void printRight(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 702 | if (Printing) |
| 703 | return; |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 704 | ScopedOverride<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 705 | std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB); |
Mikhail Borisov | 05f7722 | 2021-08-17 18:10:57 -0400 | [diff] [blame] | 706 | if (!Collapsed.second) |
| 707 | return; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 708 | if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB)) |
| 709 | OB += ")"; |
| 710 | Collapsed.second->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 711 | } |
| 712 | }; |
| 713 | |
| 714 | class PointerToMemberType final : public Node { |
| 715 | const Node *ClassType; |
| 716 | const Node *MemberType; |
| 717 | |
| 718 | public: |
| 719 | PointerToMemberType(const Node *ClassType_, const Node *MemberType_) |
| 720 | : Node(KPointerToMemberType, MemberType_->RHSComponentCache), |
| 721 | ClassType(ClassType_), MemberType(MemberType_) {} |
| 722 | |
| 723 | template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); } |
| 724 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 725 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
| 726 | return MemberType->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 727 | } |
| 728 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 729 | void printLeft(OutputBuffer &OB) const override { |
| 730 | MemberType->printLeft(OB); |
| 731 | if (MemberType->hasArray(OB) || MemberType->hasFunction(OB)) |
| 732 | OB += "("; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 733 | else |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 734 | OB += " "; |
| 735 | ClassType->print(OB); |
| 736 | OB += "::*"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 737 | } |
| 738 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 739 | void printRight(OutputBuffer &OB) const override { |
| 740 | if (MemberType->hasArray(OB) || MemberType->hasFunction(OB)) |
| 741 | OB += ")"; |
| 742 | MemberType->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 743 | } |
| 744 | }; |
| 745 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 746 | class ArrayType final : public Node { |
| 747 | const Node *Base; |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 748 | Node *Dimension; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 749 | |
| 750 | public: |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 751 | ArrayType(const Node *Base_, Node *Dimension_) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 752 | : Node(KArrayType, |
| 753 | /*RHSComponentCache=*/Cache::Yes, |
| 754 | /*ArrayCache=*/Cache::Yes), |
| 755 | Base(Base_), Dimension(Dimension_) {} |
| 756 | |
| 757 | template<typename Fn> void match(Fn F) const { F(Base, Dimension); } |
| 758 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 759 | bool hasRHSComponentSlow(OutputBuffer &) const override { return true; } |
| 760 | bool hasArraySlow(OutputBuffer &) const override { return true; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 761 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 762 | void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 763 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 764 | void printRight(OutputBuffer &OB) const override { |
| 765 | if (OB.back() != ']') |
| 766 | OB += " "; |
| 767 | OB += "["; |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 768 | if (Dimension) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 769 | Dimension->print(OB); |
| 770 | OB += "]"; |
| 771 | Base->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 772 | } |
| 773 | }; |
| 774 | |
| 775 | class FunctionType final : public Node { |
| 776 | const Node *Ret; |
| 777 | NodeArray Params; |
| 778 | Qualifiers CVQuals; |
| 779 | FunctionRefQual RefQual; |
| 780 | const Node *ExceptionSpec; |
| 781 | |
| 782 | public: |
| 783 | FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_, |
| 784 | FunctionRefQual RefQual_, const Node *ExceptionSpec_) |
| 785 | : Node(KFunctionType, |
| 786 | /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No, |
| 787 | /*FunctionCache=*/Cache::Yes), |
| 788 | Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_), |
| 789 | ExceptionSpec(ExceptionSpec_) {} |
| 790 | |
| 791 | template<typename Fn> void match(Fn F) const { |
| 792 | F(Ret, Params, CVQuals, RefQual, ExceptionSpec); |
| 793 | } |
| 794 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 795 | bool hasRHSComponentSlow(OutputBuffer &) const override { return true; } |
| 796 | bool hasFunctionSlow(OutputBuffer &) const override { return true; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 797 | |
| 798 | // Handle C++'s ... quirky decl grammar by using the left & right |
| 799 | // distinction. Consider: |
| 800 | // int (*f(float))(char) {} |
| 801 | // f is a function that takes a float and returns a pointer to a function |
| 802 | // that takes a char and returns an int. If we're trying to print f, start |
| 803 | // by printing out the return types's left, then print our parameters, then |
| 804 | // finally print right of the return type. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 805 | void printLeft(OutputBuffer &OB) const override { |
| 806 | Ret->printLeft(OB); |
| 807 | OB += " "; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 808 | } |
| 809 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 810 | void printRight(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 811 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 812 | Params.printWithComma(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 813 | OB.printClose(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 814 | Ret->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 815 | |
| 816 | if (CVQuals & QualConst) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 817 | OB += " const"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 818 | if (CVQuals & QualVolatile) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 819 | OB += " volatile"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 820 | if (CVQuals & QualRestrict) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 821 | OB += " restrict"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 822 | |
| 823 | if (RefQual == FrefQualLValue) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 824 | OB += " &"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 825 | else if (RefQual == FrefQualRValue) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 826 | OB += " &&"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 827 | |
| 828 | if (ExceptionSpec != nullptr) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 829 | OB += ' '; |
| 830 | ExceptionSpec->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | }; |
| 834 | |
| 835 | class NoexceptSpec : public Node { |
| 836 | const Node *E; |
| 837 | public: |
| 838 | NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {} |
| 839 | |
| 840 | template<typename Fn> void match(Fn F) const { F(E); } |
| 841 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 842 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 843 | OB += "noexcept"; |
| 844 | OB.printOpen(); |
| 845 | E->printAsOperand(OB); |
| 846 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 847 | } |
| 848 | }; |
| 849 | |
| 850 | class DynamicExceptionSpec : public Node { |
| 851 | NodeArray Types; |
| 852 | public: |
| 853 | DynamicExceptionSpec(NodeArray Types_) |
| 854 | : Node(KDynamicExceptionSpec), Types(Types_) {} |
| 855 | |
| 856 | template<typename Fn> void match(Fn F) const { F(Types); } |
| 857 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 858 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 859 | OB += "throw"; |
| 860 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 861 | Types.printWithComma(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 862 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 863 | } |
| 864 | }; |
| 865 | |
| 866 | class FunctionEncoding final : public Node { |
| 867 | const Node *Ret; |
| 868 | const Node *Name; |
| 869 | NodeArray Params; |
| 870 | const Node *Attrs; |
| 871 | Qualifiers CVQuals; |
| 872 | FunctionRefQual RefQual; |
| 873 | |
| 874 | public: |
| 875 | FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_, |
| 876 | const Node *Attrs_, Qualifiers CVQuals_, |
| 877 | FunctionRefQual RefQual_) |
| 878 | : Node(KFunctionEncoding, |
| 879 | /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No, |
| 880 | /*FunctionCache=*/Cache::Yes), |
| 881 | Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_), |
| 882 | CVQuals(CVQuals_), RefQual(RefQual_) {} |
| 883 | |
| 884 | template<typename Fn> void match(Fn F) const { |
| 885 | F(Ret, Name, Params, Attrs, CVQuals, RefQual); |
| 886 | } |
| 887 | |
| 888 | Qualifiers getCVQuals() const { return CVQuals; } |
| 889 | FunctionRefQual getRefQual() const { return RefQual; } |
| 890 | NodeArray getParams() const { return Params; } |
| 891 | const Node *getReturnType() const { return Ret; } |
| 892 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 893 | bool hasRHSComponentSlow(OutputBuffer &) const override { return true; } |
| 894 | bool hasFunctionSlow(OutputBuffer &) const override { return true; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 895 | |
| 896 | const Node *getName() const { return Name; } |
| 897 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 898 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 899 | if (Ret) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 900 | Ret->printLeft(OB); |
| 901 | if (!Ret->hasRHSComponent(OB)) |
| 902 | OB += " "; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 903 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 904 | Name->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 905 | } |
| 906 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 907 | void printRight(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 908 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 909 | Params.printWithComma(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 910 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 911 | if (Ret) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 912 | Ret->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 913 | |
| 914 | if (CVQuals & QualConst) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 915 | OB += " const"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 916 | if (CVQuals & QualVolatile) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 917 | OB += " volatile"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 918 | if (CVQuals & QualRestrict) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 919 | OB += " restrict"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 920 | |
| 921 | if (RefQual == FrefQualLValue) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 922 | OB += " &"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 923 | else if (RefQual == FrefQualRValue) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 924 | OB += " &&"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 925 | |
| 926 | if (Attrs != nullptr) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 927 | Attrs->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 928 | } |
| 929 | }; |
| 930 | |
| 931 | class LiteralOperator : public Node { |
| 932 | const Node *OpName; |
| 933 | |
| 934 | public: |
| 935 | LiteralOperator(const Node *OpName_) |
| 936 | : Node(KLiteralOperator), OpName(OpName_) {} |
| 937 | |
| 938 | template<typename Fn> void match(Fn F) const { F(OpName); } |
| 939 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 940 | void printLeft(OutputBuffer &OB) const override { |
| 941 | OB += "operator\"\" "; |
| 942 | OpName->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 943 | } |
| 944 | }; |
| 945 | |
| 946 | class SpecialName final : public Node { |
| 947 | const StringView Special; |
| 948 | const Node *Child; |
| 949 | |
| 950 | public: |
| 951 | SpecialName(StringView Special_, const Node *Child_) |
| 952 | : Node(KSpecialName), Special(Special_), Child(Child_) {} |
| 953 | |
| 954 | template<typename Fn> void match(Fn F) const { F(Special, Child); } |
| 955 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 956 | void printLeft(OutputBuffer &OB) const override { |
| 957 | OB += Special; |
| 958 | Child->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 959 | } |
| 960 | }; |
| 961 | |
| 962 | class CtorVtableSpecialName final : public Node { |
| 963 | const Node *FirstType; |
| 964 | const Node *SecondType; |
| 965 | |
| 966 | public: |
| 967 | CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_) |
| 968 | : Node(KCtorVtableSpecialName), |
| 969 | FirstType(FirstType_), SecondType(SecondType_) {} |
| 970 | |
| 971 | template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); } |
| 972 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 973 | void printLeft(OutputBuffer &OB) const override { |
| 974 | OB += "construction vtable for "; |
| 975 | FirstType->print(OB); |
| 976 | OB += "-in-"; |
| 977 | SecondType->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 978 | } |
| 979 | }; |
| 980 | |
| 981 | struct NestedName : Node { |
| 982 | Node *Qual; |
| 983 | Node *Name; |
| 984 | |
| 985 | NestedName(Node *Qual_, Node *Name_) |
| 986 | : Node(KNestedName), Qual(Qual_), Name(Name_) {} |
| 987 | |
| 988 | template<typename Fn> void match(Fn F) const { F(Qual, Name); } |
| 989 | |
| 990 | StringView getBaseName() const override { return Name->getBaseName(); } |
| 991 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 992 | void printLeft(OutputBuffer &OB) const override { |
| 993 | Qual->print(OB); |
| 994 | OB += "::"; |
| 995 | Name->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 996 | } |
| 997 | }; |
| 998 | |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 999 | struct ModuleName : Node { |
| 1000 | ModuleName *Parent; |
| 1001 | Node *Name; |
| 1002 | bool IsPartition; |
| 1003 | |
| 1004 | ModuleName(ModuleName *Parent_, Node *Name_, bool IsPartition_ = false) |
| 1005 | : Node(KModuleName), Parent(Parent_), Name(Name_), |
| 1006 | IsPartition(IsPartition_) {} |
| 1007 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1008 | template <typename Fn> void match(Fn F) const { |
| 1009 | F(Parent, Name, IsPartition); |
| 1010 | } |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 1011 | |
| 1012 | void printLeft(OutputBuffer &OB) const override { |
| 1013 | if (Parent) |
| 1014 | Parent->print(OB); |
| 1015 | if (Parent || IsPartition) |
| 1016 | OB += IsPartition ? ':' : '.'; |
| 1017 | Name->print(OB); |
| 1018 | } |
| 1019 | }; |
| 1020 | |
| 1021 | struct ModuleEntity : Node { |
| 1022 | ModuleName *Module; |
| 1023 | Node *Name; |
| 1024 | |
| 1025 | ModuleEntity(ModuleName *Module_, Node *Name_) |
| 1026 | : Node(KModuleEntity), Module(Module_), Name(Name_) {} |
| 1027 | |
| 1028 | template <typename Fn> void match(Fn F) const { F(Module, Name); } |
| 1029 | |
| 1030 | StringView getBaseName() const override { return Name->getBaseName(); } |
| 1031 | |
| 1032 | void printLeft(OutputBuffer &OB) const override { |
| 1033 | Name->print(OB); |
| 1034 | OB += '@'; |
| 1035 | Module->print(OB); |
| 1036 | } |
| 1037 | }; |
| 1038 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1039 | struct LocalName : Node { |
| 1040 | Node *Encoding; |
| 1041 | Node *Entity; |
| 1042 | |
| 1043 | LocalName(Node *Encoding_, Node *Entity_) |
| 1044 | : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {} |
| 1045 | |
| 1046 | template<typename Fn> void match(Fn F) const { F(Encoding, Entity); } |
| 1047 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1048 | void printLeft(OutputBuffer &OB) const override { |
| 1049 | Encoding->print(OB); |
| 1050 | OB += "::"; |
| 1051 | Entity->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1052 | } |
| 1053 | }; |
| 1054 | |
| 1055 | class QualifiedName final : public Node { |
| 1056 | // qualifier::name |
| 1057 | const Node *Qualifier; |
| 1058 | const Node *Name; |
| 1059 | |
| 1060 | public: |
| 1061 | QualifiedName(const Node *Qualifier_, const Node *Name_) |
| 1062 | : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {} |
| 1063 | |
| 1064 | template<typename Fn> void match(Fn F) const { F(Qualifier, Name); } |
| 1065 | |
| 1066 | StringView getBaseName() const override { return Name->getBaseName(); } |
| 1067 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1068 | void printLeft(OutputBuffer &OB) const override { |
| 1069 | Qualifier->print(OB); |
| 1070 | OB += "::"; |
| 1071 | Name->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1072 | } |
| 1073 | }; |
| 1074 | |
| 1075 | class VectorType final : public Node { |
| 1076 | const Node *BaseType; |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 1077 | const Node *Dimension; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1078 | |
| 1079 | public: |
Nathan Sidwell | e9c9bdf | 2022-03-29 06:19:18 -0700 | [diff] [blame] | 1080 | VectorType(const Node *BaseType_, const Node *Dimension_) |
| 1081 | : Node(KVectorType), BaseType(BaseType_), Dimension(Dimension_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1082 | |
Nathan Sidwell | eb9fece | 2022-10-07 07:03:32 -0700 | [diff] [blame^] | 1083 | const Node *getBaseType() const { return BaseType; } |
| 1084 | const Node *getDimension() const { return Dimension; } |
| 1085 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1086 | template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); } |
| 1087 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1088 | void printLeft(OutputBuffer &OB) const override { |
| 1089 | BaseType->print(OB); |
| 1090 | OB += " vector["; |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 1091 | if (Dimension) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1092 | Dimension->print(OB); |
| 1093 | OB += "]"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1094 | } |
| 1095 | }; |
| 1096 | |
| 1097 | class PixelVectorType final : public Node { |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 1098 | const Node *Dimension; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1099 | |
| 1100 | public: |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 1101 | PixelVectorType(const Node *Dimension_) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1102 | : Node(KPixelVectorType), Dimension(Dimension_) {} |
| 1103 | |
| 1104 | template<typename Fn> void match(Fn F) const { F(Dimension); } |
| 1105 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1106 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1107 | // FIXME: This should demangle as "vector pixel". |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1108 | OB += "pixel vector["; |
| 1109 | Dimension->print(OB); |
| 1110 | OB += "]"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1111 | } |
| 1112 | }; |
| 1113 | |
Pengfei Wang | 50e90b8 | 2021-09-23 11:02:25 +0800 | [diff] [blame] | 1114 | class BinaryFPType final : public Node { |
| 1115 | const Node *Dimension; |
| 1116 | |
| 1117 | public: |
| 1118 | BinaryFPType(const Node *Dimension_) |
| 1119 | : Node(KBinaryFPType), Dimension(Dimension_) {} |
| 1120 | |
| 1121 | template<typename Fn> void match(Fn F) const { F(Dimension); } |
| 1122 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1123 | void printLeft(OutputBuffer &OB) const override { |
| 1124 | OB += "_Float"; |
| 1125 | Dimension->print(OB); |
Pengfei Wang | 50e90b8 | 2021-09-23 11:02:25 +0800 | [diff] [blame] | 1126 | } |
| 1127 | }; |
| 1128 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1129 | enum class TemplateParamKind { Type, NonType, Template }; |
| 1130 | |
| 1131 | /// An invented name for a template parameter for which we don't have a |
| 1132 | /// corresponding template argument. |
| 1133 | /// |
| 1134 | /// This node is created when parsing the <lambda-sig> for a lambda with |
| 1135 | /// explicit template arguments, which might be referenced in the parameter |
| 1136 | /// types appearing later in the <lambda-sig>. |
| 1137 | class SyntheticTemplateParamName final : public Node { |
| 1138 | TemplateParamKind Kind; |
| 1139 | unsigned Index; |
| 1140 | |
| 1141 | public: |
| 1142 | SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_) |
| 1143 | : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {} |
| 1144 | |
| 1145 | template<typename Fn> void match(Fn F) const { F(Kind, Index); } |
| 1146 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1147 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1148 | switch (Kind) { |
| 1149 | case TemplateParamKind::Type: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1150 | OB += "$T"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1151 | break; |
| 1152 | case TemplateParamKind::NonType: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1153 | OB += "$N"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1154 | break; |
| 1155 | case TemplateParamKind::Template: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1156 | OB += "$TT"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1157 | break; |
| 1158 | } |
| 1159 | if (Index > 0) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1160 | OB << Index - 1; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1161 | } |
| 1162 | }; |
| 1163 | |
| 1164 | /// A template type parameter declaration, 'typename T'. |
| 1165 | class TypeTemplateParamDecl final : public Node { |
| 1166 | Node *Name; |
| 1167 | |
| 1168 | public: |
| 1169 | TypeTemplateParamDecl(Node *Name_) |
| 1170 | : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {} |
| 1171 | |
| 1172 | template<typename Fn> void match(Fn F) const { F(Name); } |
| 1173 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1174 | void printLeft(OutputBuffer &OB) const override { OB += "typename "; } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1175 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1176 | void printRight(OutputBuffer &OB) const override { Name->print(OB); } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1177 | }; |
| 1178 | |
| 1179 | /// A non-type template parameter declaration, 'int N'. |
| 1180 | class NonTypeTemplateParamDecl final : public Node { |
| 1181 | Node *Name; |
| 1182 | Node *Type; |
| 1183 | |
| 1184 | public: |
| 1185 | NonTypeTemplateParamDecl(Node *Name_, Node *Type_) |
| 1186 | : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {} |
| 1187 | |
| 1188 | template<typename Fn> void match(Fn F) const { F(Name, Type); } |
| 1189 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1190 | void printLeft(OutputBuffer &OB) const override { |
| 1191 | Type->printLeft(OB); |
| 1192 | if (!Type->hasRHSComponent(OB)) |
| 1193 | OB += " "; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1196 | void printRight(OutputBuffer &OB) const override { |
| 1197 | Name->print(OB); |
| 1198 | Type->printRight(OB); |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1199 | } |
| 1200 | }; |
| 1201 | |
| 1202 | /// A template template parameter declaration, |
| 1203 | /// 'template<typename T> typename N'. |
| 1204 | class TemplateTemplateParamDecl final : public Node { |
| 1205 | Node *Name; |
| 1206 | NodeArray Params; |
| 1207 | |
| 1208 | public: |
| 1209 | TemplateTemplateParamDecl(Node *Name_, NodeArray Params_) |
| 1210 | : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_), |
| 1211 | Params(Params_) {} |
| 1212 | |
| 1213 | template<typename Fn> void match(Fn F) const { F(Name, Params); } |
| 1214 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1215 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1216 | ScopedOverride<unsigned> LT(OB.GtIsGt, 0); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1217 | OB += "template<"; |
| 1218 | Params.printWithComma(OB); |
| 1219 | OB += "> typename "; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1222 | void printRight(OutputBuffer &OB) const override { Name->print(OB); } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1223 | }; |
| 1224 | |
| 1225 | /// A template parameter pack declaration, 'typename ...T'. |
| 1226 | class TemplateParamPackDecl final : public Node { |
| 1227 | Node *Param; |
| 1228 | |
| 1229 | public: |
| 1230 | TemplateParamPackDecl(Node *Param_) |
| 1231 | : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {} |
| 1232 | |
| 1233 | template<typename Fn> void match(Fn F) const { F(Param); } |
| 1234 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1235 | void printLeft(OutputBuffer &OB) const override { |
| 1236 | Param->printLeft(OB); |
| 1237 | OB += "..."; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1240 | void printRight(OutputBuffer &OB) const override { Param->printRight(OB); } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1241 | }; |
| 1242 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1243 | /// An unexpanded parameter pack (either in the expression or type context). If |
| 1244 | /// this AST is correct, this node will have a ParameterPackExpansion node above |
| 1245 | /// it. |
| 1246 | /// |
| 1247 | /// This node is created when some <template-args> are found that apply to an |
| 1248 | /// <encoding>, and is stored in the TemplateParams table. In order for this to |
| 1249 | /// appear in the final AST, it has to referenced via a <template-param> (ie, |
| 1250 | /// T_). |
| 1251 | class ParameterPack final : public Node { |
| 1252 | NodeArray Data; |
| 1253 | |
Nathan Sidwell | fd0ef6d | 2022-01-20 07:40:12 -0800 | [diff] [blame] | 1254 | // Setup OutputBuffer for a pack expansion, unless we're already expanding |
| 1255 | // one. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1256 | void initializePackExpansion(OutputBuffer &OB) const { |
| 1257 | if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) { |
| 1258 | OB.CurrentPackMax = static_cast<unsigned>(Data.size()); |
| 1259 | OB.CurrentPackIndex = 0; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | public: |
| 1264 | ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) { |
| 1265 | ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown; |
| 1266 | if (std::all_of(Data.begin(), Data.end(), [](Node* P) { |
| 1267 | return P->ArrayCache == Cache::No; |
| 1268 | })) |
| 1269 | ArrayCache = Cache::No; |
| 1270 | if (std::all_of(Data.begin(), Data.end(), [](Node* P) { |
| 1271 | return P->FunctionCache == Cache::No; |
| 1272 | })) |
| 1273 | FunctionCache = Cache::No; |
| 1274 | if (std::all_of(Data.begin(), Data.end(), [](Node* P) { |
| 1275 | return P->RHSComponentCache == Cache::No; |
| 1276 | })) |
| 1277 | RHSComponentCache = Cache::No; |
| 1278 | } |
| 1279 | |
| 1280 | template<typename Fn> void match(Fn F) const { F(Data); } |
| 1281 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1282 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
| 1283 | initializePackExpansion(OB); |
| 1284 | size_t Idx = OB.CurrentPackIndex; |
| 1285 | return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1286 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1287 | bool hasArraySlow(OutputBuffer &OB) const override { |
| 1288 | initializePackExpansion(OB); |
| 1289 | size_t Idx = OB.CurrentPackIndex; |
| 1290 | return Idx < Data.size() && Data[Idx]->hasArray(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1291 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1292 | bool hasFunctionSlow(OutputBuffer &OB) const override { |
| 1293 | initializePackExpansion(OB); |
| 1294 | size_t Idx = OB.CurrentPackIndex; |
| 1295 | return Idx < Data.size() && Data[Idx]->hasFunction(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1296 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1297 | const Node *getSyntaxNode(OutputBuffer &OB) const override { |
| 1298 | initializePackExpansion(OB); |
| 1299 | size_t Idx = OB.CurrentPackIndex; |
| 1300 | return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1303 | void printLeft(OutputBuffer &OB) const override { |
| 1304 | initializePackExpansion(OB); |
| 1305 | size_t Idx = OB.CurrentPackIndex; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1306 | if (Idx < Data.size()) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1307 | Data[Idx]->printLeft(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1308 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1309 | void printRight(OutputBuffer &OB) const override { |
| 1310 | initializePackExpansion(OB); |
| 1311 | size_t Idx = OB.CurrentPackIndex; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1312 | if (Idx < Data.size()) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1313 | Data[Idx]->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1314 | } |
| 1315 | }; |
| 1316 | |
| 1317 | /// A variadic template argument. This node represents an occurrence of |
| 1318 | /// J<something>E in some <template-args>. It isn't itself unexpanded, unless |
| 1319 | /// one of it's Elements is. The parser inserts a ParameterPack into the |
| 1320 | /// TemplateParams table if the <template-args> this pack belongs to apply to an |
| 1321 | /// <encoding>. |
| 1322 | class TemplateArgumentPack final : public Node { |
| 1323 | NodeArray Elements; |
| 1324 | public: |
| 1325 | TemplateArgumentPack(NodeArray Elements_) |
| 1326 | : Node(KTemplateArgumentPack), Elements(Elements_) {} |
| 1327 | |
| 1328 | template<typename Fn> void match(Fn F) const { F(Elements); } |
| 1329 | |
| 1330 | NodeArray getElements() const { return Elements; } |
| 1331 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1332 | void printLeft(OutputBuffer &OB) const override { |
| 1333 | Elements.printWithComma(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1334 | } |
| 1335 | }; |
| 1336 | |
| 1337 | /// A pack expansion. Below this node, there are some unexpanded ParameterPacks |
| 1338 | /// which each have Child->ParameterPackSize elements. |
| 1339 | class ParameterPackExpansion final : public Node { |
| 1340 | const Node *Child; |
| 1341 | |
| 1342 | public: |
| 1343 | ParameterPackExpansion(const Node *Child_) |
| 1344 | : Node(KParameterPackExpansion), Child(Child_) {} |
| 1345 | |
| 1346 | template<typename Fn> void match(Fn F) const { F(Child); } |
| 1347 | |
| 1348 | const Node *getChild() const { return Child; } |
| 1349 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1350 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1351 | constexpr unsigned Max = std::numeric_limits<unsigned>::max(); |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1352 | ScopedOverride<unsigned> SavePackIdx(OB.CurrentPackIndex, Max); |
| 1353 | ScopedOverride<unsigned> SavePackMax(OB.CurrentPackMax, Max); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1354 | size_t StreamPos = OB.getCurrentPosition(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Print the first element in the pack. If Child contains a ParameterPack, |
| 1357 | // it will set up S.CurrentPackMax and print the first element. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1358 | Child->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1359 | |
| 1360 | // No ParameterPack was found in Child. This can occur if we've found a pack |
| 1361 | // expansion on a <function-param>. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1362 | if (OB.CurrentPackMax == Max) { |
| 1363 | OB += "..."; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1364 | return; |
| 1365 | } |
| 1366 | |
| 1367 | // We found a ParameterPack, but it has no elements. Erase whatever we may |
| 1368 | // of printed. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1369 | if (OB.CurrentPackMax == 0) { |
| 1370 | OB.setCurrentPosition(StreamPos); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | // Else, iterate through the rest of the elements in the pack. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1375 | for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) { |
| 1376 | OB += ", "; |
| 1377 | OB.CurrentPackIndex = I; |
| 1378 | Child->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | }; |
| 1382 | |
| 1383 | class TemplateArgs final : public Node { |
| 1384 | NodeArray Params; |
| 1385 | |
| 1386 | public: |
| 1387 | TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {} |
| 1388 | |
| 1389 | template<typename Fn> void match(Fn F) const { F(Params); } |
| 1390 | |
| 1391 | NodeArray getParams() { return Params; } |
| 1392 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1393 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1394 | ScopedOverride<unsigned> LT(OB.GtIsGt, 0); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1395 | OB += "<"; |
| 1396 | Params.printWithComma(OB); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1397 | OB += ">"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1398 | } |
| 1399 | }; |
| 1400 | |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 1401 | /// A forward-reference to a template argument that was not known at the point |
| 1402 | /// where the template parameter name was parsed in a mangling. |
| 1403 | /// |
| 1404 | /// This is created when demangling the name of a specialization of a |
| 1405 | /// conversion function template: |
| 1406 | /// |
| 1407 | /// \code |
| 1408 | /// struct A { |
| 1409 | /// template<typename T> operator T*(); |
| 1410 | /// }; |
| 1411 | /// \endcode |
| 1412 | /// |
| 1413 | /// When demangling a specialization of the conversion function template, we |
| 1414 | /// encounter the name of the template (including the \c T) before we reach |
| 1415 | /// the template argument list, so we cannot substitute the parameter name |
| 1416 | /// for the corresponding argument while parsing. Instead, we create a |
| 1417 | /// \c ForwardTemplateReference node that is resolved after we parse the |
| 1418 | /// template arguments. |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1419 | struct ForwardTemplateReference : Node { |
| 1420 | size_t Index; |
| 1421 | Node *Ref = nullptr; |
| 1422 | |
| 1423 | // If we're currently printing this node. It is possible (though invalid) for |
| 1424 | // a forward template reference to refer to itself via a substitution. This |
| 1425 | // creates a cyclic AST, which will stack overflow printing. To fix this, bail |
| 1426 | // out if more than one print* function is active. |
| 1427 | mutable bool Printing = false; |
| 1428 | |
| 1429 | ForwardTemplateReference(size_t Index_) |
| 1430 | : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown, |
| 1431 | Cache::Unknown), |
| 1432 | Index(Index_) {} |
| 1433 | |
| 1434 | // We don't provide a matcher for these, because the value of the node is |
| 1435 | // not determined by its construction parameters, and it generally needs |
| 1436 | // special handling. |
| 1437 | template<typename Fn> void match(Fn F) const = delete; |
| 1438 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1439 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1440 | if (Printing) |
| 1441 | return false; |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1442 | ScopedOverride<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1443 | return Ref->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1444 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1445 | bool hasArraySlow(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1446 | if (Printing) |
| 1447 | return false; |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1448 | ScopedOverride<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1449 | return Ref->hasArray(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1450 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1451 | bool hasFunctionSlow(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1452 | if (Printing) |
| 1453 | return false; |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1454 | ScopedOverride<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1455 | return Ref->hasFunction(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1456 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1457 | const Node *getSyntaxNode(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1458 | if (Printing) |
| 1459 | return this; |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1460 | ScopedOverride<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1461 | return Ref->getSyntaxNode(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1464 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1465 | if (Printing) |
| 1466 | return; |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1467 | ScopedOverride<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1468 | Ref->printLeft(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1469 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1470 | void printRight(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1471 | if (Printing) |
| 1472 | return; |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1473 | ScopedOverride<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1474 | Ref->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1475 | } |
| 1476 | }; |
| 1477 | |
| 1478 | struct NameWithTemplateArgs : Node { |
| 1479 | // name<template_args> |
| 1480 | Node *Name; |
| 1481 | Node *TemplateArgs; |
| 1482 | |
| 1483 | NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_) |
| 1484 | : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {} |
| 1485 | |
| 1486 | template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); } |
| 1487 | |
| 1488 | StringView getBaseName() const override { return Name->getBaseName(); } |
| 1489 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1490 | void printLeft(OutputBuffer &OB) const override { |
| 1491 | Name->print(OB); |
| 1492 | TemplateArgs->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1493 | } |
| 1494 | }; |
| 1495 | |
| 1496 | class GlobalQualifiedName final : public Node { |
| 1497 | Node *Child; |
| 1498 | |
| 1499 | public: |
| 1500 | GlobalQualifiedName(Node* Child_) |
| 1501 | : Node(KGlobalQualifiedName), Child(Child_) {} |
| 1502 | |
| 1503 | template<typename Fn> void match(Fn F) const { F(Child); } |
| 1504 | |
| 1505 | StringView getBaseName() const override { return Child->getBaseName(); } |
| 1506 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1507 | void printLeft(OutputBuffer &OB) const override { |
| 1508 | OB += "::"; |
| 1509 | Child->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1510 | } |
| 1511 | }; |
| 1512 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1513 | enum class SpecialSubKind { |
| 1514 | allocator, |
| 1515 | basic_string, |
| 1516 | string, |
| 1517 | istream, |
| 1518 | ostream, |
| 1519 | iostream, |
| 1520 | }; |
| 1521 | |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1522 | class SpecialSubstitution; |
| 1523 | class ExpandedSpecialSubstitution : public Node { |
| 1524 | protected: |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1525 | SpecialSubKind SSK; |
| 1526 | |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1527 | ExpandedSpecialSubstitution(SpecialSubKind SSK_, Kind K_) |
| 1528 | : Node(K_), SSK(SSK_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1529 | public: |
| 1530 | ExpandedSpecialSubstitution(SpecialSubKind SSK_) |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1531 | : ExpandedSpecialSubstitution(SSK_, KExpandedSpecialSubstitution) {} |
| 1532 | inline ExpandedSpecialSubstitution(SpecialSubstitution const *); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1533 | |
| 1534 | template<typename Fn> void match(Fn F) const { F(SSK); } |
| 1535 | |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1536 | protected: |
| 1537 | bool isInstantiation() const { |
| 1538 | return unsigned(SSK) >= unsigned(SpecialSubKind::string); |
| 1539 | } |
| 1540 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1541 | StringView getBaseName() const override { |
| 1542 | switch (SSK) { |
| 1543 | case SpecialSubKind::allocator: |
| 1544 | return StringView("allocator"); |
| 1545 | case SpecialSubKind::basic_string: |
| 1546 | return StringView("basic_string"); |
| 1547 | case SpecialSubKind::string: |
| 1548 | return StringView("basic_string"); |
| 1549 | case SpecialSubKind::istream: |
| 1550 | return StringView("basic_istream"); |
| 1551 | case SpecialSubKind::ostream: |
| 1552 | return StringView("basic_ostream"); |
| 1553 | case SpecialSubKind::iostream: |
| 1554 | return StringView("basic_iostream"); |
| 1555 | } |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 1556 | DEMANGLE_UNREACHABLE; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1559 | private: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1560 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1561 | OB << "std::" << getBaseName(); |
| 1562 | if (isInstantiation()) { |
| 1563 | OB << "<char, std::char_traits<char>"; |
| 1564 | if (SSK == SpecialSubKind::string) |
| 1565 | OB << ", std::allocator<char>"; |
| 1566 | OB << ">"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | }; |
| 1570 | |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1571 | class SpecialSubstitution final : public ExpandedSpecialSubstitution { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1572 | public: |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1573 | SpecialSubstitution(SpecialSubKind SSK_) |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1574 | : ExpandedSpecialSubstitution(SSK_, KSpecialSubstitution) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1575 | |
| 1576 | template<typename Fn> void match(Fn F) const { F(SSK); } |
| 1577 | |
| 1578 | StringView getBaseName() const override { |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1579 | auto SV = ExpandedSpecialSubstitution::getBaseName (); |
| 1580 | if (isInstantiation()) { |
| 1581 | // The instantiations are typedefs that drop the "basic_" prefix. |
| 1582 | assert(SV.startsWith("basic_")); |
| 1583 | SV = SV.dropFront(sizeof("basic_") - 1); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1584 | } |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1585 | return SV; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1588 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1589 | OB << "std::" << getBaseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1590 | } |
| 1591 | }; |
| 1592 | |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 1593 | inline ExpandedSpecialSubstitution::ExpandedSpecialSubstitution( |
| 1594 | SpecialSubstitution const *SS) |
| 1595 | : ExpandedSpecialSubstitution(SS->SSK) {} |
| 1596 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1597 | class CtorDtorName final : public Node { |
| 1598 | const Node *Basename; |
| 1599 | const bool IsDtor; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 1600 | const int Variant; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1601 | |
| 1602 | public: |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 1603 | CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_) |
| 1604 | : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_), |
| 1605 | Variant(Variant_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1606 | |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 1607 | template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1608 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1609 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1610 | if (IsDtor) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1611 | OB += "~"; |
| 1612 | OB += Basename->getBaseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1613 | } |
| 1614 | }; |
| 1615 | |
| 1616 | class DtorName : public Node { |
| 1617 | const Node *Base; |
| 1618 | |
| 1619 | public: |
| 1620 | DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {} |
| 1621 | |
| 1622 | template<typename Fn> void match(Fn F) const { F(Base); } |
| 1623 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1624 | void printLeft(OutputBuffer &OB) const override { |
| 1625 | OB += "~"; |
| 1626 | Base->printLeft(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1627 | } |
| 1628 | }; |
| 1629 | |
| 1630 | class UnnamedTypeName : public Node { |
| 1631 | const StringView Count; |
| 1632 | |
| 1633 | public: |
| 1634 | UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {} |
| 1635 | |
| 1636 | template<typename Fn> void match(Fn F) const { F(Count); } |
| 1637 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1638 | void printLeft(OutputBuffer &OB) const override { |
| 1639 | OB += "'unnamed"; |
| 1640 | OB += Count; |
| 1641 | OB += "\'"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1642 | } |
| 1643 | }; |
| 1644 | |
| 1645 | class ClosureTypeName : public Node { |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1646 | NodeArray TemplateParams; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1647 | NodeArray Params; |
| 1648 | StringView Count; |
| 1649 | |
| 1650 | public: |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1651 | ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_, |
| 1652 | StringView Count_) |
| 1653 | : Node(KClosureTypeName), TemplateParams(TemplateParams_), |
| 1654 | Params(Params_), Count(Count_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1655 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1656 | template<typename Fn> void match(Fn F) const { |
| 1657 | F(TemplateParams, Params, Count); |
| 1658 | } |
| 1659 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1660 | void printDeclarator(OutputBuffer &OB) const { |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1661 | if (!TemplateParams.empty()) { |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1662 | ScopedOverride<unsigned> LT(OB.GtIsGt, 0); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1663 | OB += "<"; |
| 1664 | TemplateParams.printWithComma(OB); |
| 1665 | OB += ">"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1666 | } |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1667 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1668 | Params.printWithComma(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1669 | OB.printClose(); |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1670 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1671 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1672 | void printLeft(OutputBuffer &OB) const override { |
| 1673 | OB += "\'lambda"; |
| 1674 | OB += Count; |
| 1675 | OB += "\'"; |
| 1676 | printDeclarator(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1677 | } |
| 1678 | }; |
| 1679 | |
| 1680 | class StructuredBindingName : public Node { |
| 1681 | NodeArray Bindings; |
| 1682 | public: |
| 1683 | StructuredBindingName(NodeArray Bindings_) |
| 1684 | : Node(KStructuredBindingName), Bindings(Bindings_) {} |
| 1685 | |
| 1686 | template<typename Fn> void match(Fn F) const { F(Bindings); } |
| 1687 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1688 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1689 | OB.printOpen('['); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1690 | Bindings.printWithComma(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1691 | OB.printClose(']'); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1692 | } |
| 1693 | }; |
| 1694 | |
| 1695 | // -- Expression Nodes -- |
| 1696 | |
| 1697 | class BinaryExpr : public Node { |
| 1698 | const Node *LHS; |
| 1699 | const StringView InfixOperator; |
| 1700 | const Node *RHS; |
| 1701 | |
| 1702 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1703 | BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_, |
| 1704 | Prec Prec_) |
| 1705 | : Node(KBinaryExpr, Prec_), LHS(LHS_), InfixOperator(InfixOperator_), |
| 1706 | RHS(RHS_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1707 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1708 | template <typename Fn> void match(Fn F) const { |
| 1709 | F(LHS, InfixOperator, RHS, getPrecedence()); |
| 1710 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1711 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1712 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 1850510 | 2022-03-25 04:34:19 -0700 | [diff] [blame] | 1713 | bool ParenAll = OB.isGtInsideTemplateArgs() && |
| 1714 | (InfixOperator == ">" || InfixOperator == ">>"); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1715 | if (ParenAll) |
| 1716 | OB.printOpen(); |
| 1717 | // Assignment is right associative, with special LHS precedence. |
| 1718 | bool IsAssign = getPrecedence() == Prec::Assign; |
| 1719 | LHS->printAsOperand(OB, IsAssign ? Prec::OrIf : getPrecedence(), !IsAssign); |
| 1720 | // No space before comma operator |
| 1721 | if (!(InfixOperator == ",")) |
| 1722 | OB += " "; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1723 | OB += InfixOperator; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1724 | OB += " "; |
| 1725 | RHS->printAsOperand(OB, getPrecedence(), IsAssign); |
| 1726 | if (ParenAll) |
| 1727 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1728 | } |
| 1729 | }; |
| 1730 | |
| 1731 | class ArraySubscriptExpr : public Node { |
| 1732 | const Node *Op1; |
| 1733 | const Node *Op2; |
| 1734 | |
| 1735 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1736 | ArraySubscriptExpr(const Node *Op1_, const Node *Op2_, Prec Prec_) |
| 1737 | : Node(KArraySubscriptExpr, Prec_), Op1(Op1_), Op2(Op2_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1738 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1739 | template <typename Fn> void match(Fn F) const { |
| 1740 | F(Op1, Op2, getPrecedence()); |
| 1741 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1742 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1743 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1744 | Op1->printAsOperand(OB, getPrecedence()); |
| 1745 | OB.printOpen('['); |
| 1746 | Op2->printAsOperand(OB); |
| 1747 | OB.printClose(']'); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1748 | } |
| 1749 | }; |
| 1750 | |
| 1751 | class PostfixExpr : public Node { |
| 1752 | const Node *Child; |
| 1753 | const StringView Operator; |
| 1754 | |
| 1755 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1756 | PostfixExpr(const Node *Child_, StringView Operator_, Prec Prec_) |
| 1757 | : Node(KPostfixExpr, Prec_), Child(Child_), Operator(Operator_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1758 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1759 | template <typename Fn> void match(Fn F) const { |
| 1760 | F(Child, Operator, getPrecedence()); |
| 1761 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1762 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1763 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1764 | Child->printAsOperand(OB, getPrecedence(), true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1765 | OB += Operator; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1766 | } |
| 1767 | }; |
| 1768 | |
| 1769 | class ConditionalExpr : public Node { |
| 1770 | const Node *Cond; |
| 1771 | const Node *Then; |
| 1772 | const Node *Else; |
| 1773 | |
| 1774 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1775 | ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_, |
| 1776 | Prec Prec_) |
| 1777 | : Node(KConditionalExpr, Prec_), Cond(Cond_), Then(Then_), Else(Else_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1778 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1779 | template <typename Fn> void match(Fn F) const { |
| 1780 | F(Cond, Then, Else, getPrecedence()); |
| 1781 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1782 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1783 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1784 | Cond->printAsOperand(OB, getPrecedence()); |
| 1785 | OB += " ? "; |
| 1786 | Then->printAsOperand(OB); |
| 1787 | OB += " : "; |
| 1788 | Else->printAsOperand(OB, Prec::Assign, true); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1789 | } |
| 1790 | }; |
| 1791 | |
| 1792 | class MemberExpr : public Node { |
| 1793 | const Node *LHS; |
| 1794 | const StringView Kind; |
| 1795 | const Node *RHS; |
| 1796 | |
| 1797 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1798 | MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_, Prec Prec_) |
| 1799 | : Node(KMemberExpr, Prec_), LHS(LHS_), Kind(Kind_), RHS(RHS_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1800 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1801 | template <typename Fn> void match(Fn F) const { |
| 1802 | F(LHS, Kind, RHS, getPrecedence()); |
| 1803 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1804 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1805 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1806 | LHS->printAsOperand(OB, getPrecedence(), true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1807 | OB += Kind; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1808 | RHS->printAsOperand(OB, getPrecedence(), false); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1809 | } |
| 1810 | }; |
| 1811 | |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1812 | class SubobjectExpr : public Node { |
| 1813 | const Node *Type; |
| 1814 | const Node *SubExpr; |
| 1815 | StringView Offset; |
| 1816 | NodeArray UnionSelectors; |
| 1817 | bool OnePastTheEnd; |
| 1818 | |
| 1819 | public: |
| 1820 | SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_, |
| 1821 | NodeArray UnionSelectors_, bool OnePastTheEnd_) |
| 1822 | : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_), |
| 1823 | UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {} |
| 1824 | |
| 1825 | template<typename Fn> void match(Fn F) const { |
| 1826 | F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd); |
| 1827 | } |
| 1828 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1829 | void printLeft(OutputBuffer &OB) const override { |
| 1830 | SubExpr->print(OB); |
| 1831 | OB += ".<"; |
| 1832 | Type->print(OB); |
| 1833 | OB += " at offset "; |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1834 | if (Offset.empty()) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1835 | OB += "0"; |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1836 | } else if (Offset[0] == 'n') { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1837 | OB += "-"; |
| 1838 | OB += Offset.dropFront(); |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1839 | } else { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1840 | OB += Offset; |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1841 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1842 | OB += ">"; |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1843 | } |
| 1844 | }; |
| 1845 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1846 | class EnclosingExpr : public Node { |
| 1847 | const StringView Prefix; |
| 1848 | const Node *Infix; |
| 1849 | const StringView Postfix; |
| 1850 | |
| 1851 | public: |
Nathan Sidwell | e9c9bdf | 2022-03-29 06:19:18 -0700 | [diff] [blame] | 1852 | EnclosingExpr(StringView Prefix_, const Node *Infix_, |
| 1853 | Prec Prec_ = Prec::Primary) |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1854 | : Node(KEnclosingExpr, Prec_), Prefix(Prefix_), Infix(Infix_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1855 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1856 | template <typename Fn> void match(Fn F) const { |
| 1857 | F(Prefix, Infix, getPrecedence()); |
| 1858 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1859 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1860 | void printLeft(OutputBuffer &OB) const override { |
| 1861 | OB += Prefix; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1862 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1863 | Infix->print(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1864 | OB.printClose(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1865 | OB += Postfix; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1866 | } |
| 1867 | }; |
| 1868 | |
| 1869 | class CastExpr : public Node { |
| 1870 | // cast_kind<to>(from) |
| 1871 | const StringView CastKind; |
| 1872 | const Node *To; |
| 1873 | const Node *From; |
| 1874 | |
| 1875 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1876 | CastExpr(StringView CastKind_, const Node *To_, const Node *From_, Prec Prec_) |
| 1877 | : Node(KCastExpr, Prec_), CastKind(CastKind_), To(To_), From(From_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1878 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1879 | template <typename Fn> void match(Fn F) const { |
| 1880 | F(CastKind, To, From, getPrecedence()); |
| 1881 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1882 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1883 | void printLeft(OutputBuffer &OB) const override { |
| 1884 | OB += CastKind; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1885 | { |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 1886 | ScopedOverride<unsigned> LT(OB.GtIsGt, 0); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1887 | OB += "<"; |
| 1888 | To->printLeft(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1889 | OB += ">"; |
| 1890 | } |
| 1891 | OB.printOpen(); |
| 1892 | From->printAsOperand(OB); |
| 1893 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1894 | } |
| 1895 | }; |
| 1896 | |
| 1897 | class SizeofParamPackExpr : public Node { |
| 1898 | const Node *Pack; |
| 1899 | |
| 1900 | public: |
| 1901 | SizeofParamPackExpr(const Node *Pack_) |
| 1902 | : Node(KSizeofParamPackExpr), Pack(Pack_) {} |
| 1903 | |
| 1904 | template<typename Fn> void match(Fn F) const { F(Pack); } |
| 1905 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1906 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1907 | OB += "sizeof..."; |
| 1908 | OB.printOpen(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1909 | ParameterPackExpansion PPE(Pack); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1910 | PPE.printLeft(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1911 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1912 | } |
| 1913 | }; |
| 1914 | |
| 1915 | class CallExpr : public Node { |
| 1916 | const Node *Callee; |
| 1917 | NodeArray Args; |
| 1918 | |
| 1919 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1920 | CallExpr(const Node *Callee_, NodeArray Args_, Prec Prec_) |
| 1921 | : Node(KCallExpr, Prec_), Callee(Callee_), Args(Args_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1922 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1923 | template <typename Fn> void match(Fn F) const { |
| 1924 | F(Callee, Args, getPrecedence()); |
| 1925 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1926 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1927 | void printLeft(OutputBuffer &OB) const override { |
| 1928 | Callee->print(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1929 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1930 | Args.printWithComma(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1931 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1932 | } |
| 1933 | }; |
| 1934 | |
| 1935 | class NewExpr : public Node { |
| 1936 | // new (expr_list) type(init_list) |
| 1937 | NodeArray ExprList; |
| 1938 | Node *Type; |
| 1939 | NodeArray InitList; |
| 1940 | bool IsGlobal; // ::operator new ? |
| 1941 | bool IsArray; // new[] ? |
| 1942 | public: |
| 1943 | NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_, |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1944 | bool IsArray_, Prec Prec_) |
| 1945 | : Node(KNewExpr, Prec_), ExprList(ExprList_), Type(Type_), |
| 1946 | InitList(InitList_), IsGlobal(IsGlobal_), IsArray(IsArray_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1947 | |
| 1948 | template<typename Fn> void match(Fn F) const { |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1949 | F(ExprList, Type, InitList, IsGlobal, IsArray, getPrecedence()); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1952 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1953 | if (IsGlobal) |
Nathan Sidwell | c69bde2 | 2022-01-28 07:09:38 -0800 | [diff] [blame] | 1954 | OB += "::"; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1955 | OB += "new"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1956 | if (IsArray) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1957 | OB += "[]"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1958 | if (!ExprList.empty()) { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1959 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1960 | ExprList.printWithComma(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1961 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1962 | } |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1963 | OB += " "; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1964 | Type->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1965 | if (!InitList.empty()) { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1966 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1967 | InitList.printWithComma(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1968 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1969 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1970 | } |
| 1971 | }; |
| 1972 | |
| 1973 | class DeleteExpr : public Node { |
| 1974 | Node *Op; |
| 1975 | bool IsGlobal; |
| 1976 | bool IsArray; |
| 1977 | |
| 1978 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 1979 | DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_, Prec Prec_) |
| 1980 | : Node(KDeleteExpr, Prec_), Op(Op_), IsGlobal(IsGlobal_), |
| 1981 | IsArray(IsArray_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1982 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 1983 | template <typename Fn> void match(Fn F) const { |
| 1984 | F(Op, IsGlobal, IsArray, getPrecedence()); |
| 1985 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1986 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1987 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1988 | if (IsGlobal) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1989 | OB += "::"; |
| 1990 | OB += "delete"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1991 | if (IsArray) |
Nathan Sidwell | c69bde2 | 2022-01-28 07:09:38 -0800 | [diff] [blame] | 1992 | OB += "[]"; |
| 1993 | OB += ' '; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1994 | Op->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1995 | } |
| 1996 | }; |
| 1997 | |
| 1998 | class PrefixExpr : public Node { |
| 1999 | StringView Prefix; |
| 2000 | Node *Child; |
| 2001 | |
| 2002 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2003 | PrefixExpr(StringView Prefix_, Node *Child_, Prec Prec_) |
| 2004 | : Node(KPrefixExpr, Prec_), Prefix(Prefix_), Child(Child_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2005 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 2006 | template <typename Fn> void match(Fn F) const { |
| 2007 | F(Prefix, Child, getPrecedence()); |
| 2008 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2009 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2010 | void printLeft(OutputBuffer &OB) const override { |
| 2011 | OB += Prefix; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2012 | Child->printAsOperand(OB, getPrecedence()); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2013 | } |
| 2014 | }; |
| 2015 | |
| 2016 | class FunctionParam : public Node { |
| 2017 | StringView Number; |
| 2018 | |
| 2019 | public: |
| 2020 | FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {} |
| 2021 | |
| 2022 | template<typename Fn> void match(Fn F) const { F(Number); } |
| 2023 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2024 | void printLeft(OutputBuffer &OB) const override { |
| 2025 | OB += "fp"; |
| 2026 | OB += Number; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2027 | } |
| 2028 | }; |
| 2029 | |
| 2030 | class ConversionExpr : public Node { |
| 2031 | const Node *Type; |
| 2032 | NodeArray Expressions; |
| 2033 | |
| 2034 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2035 | ConversionExpr(const Node *Type_, NodeArray Expressions_, Prec Prec_) |
| 2036 | : Node(KConversionExpr, Prec_), Type(Type_), Expressions(Expressions_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2037 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 2038 | template <typename Fn> void match(Fn F) const { |
| 2039 | F(Type, Expressions, getPrecedence()); |
| 2040 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2041 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2042 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2043 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2044 | Type->print(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2045 | OB.printClose(); |
| 2046 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2047 | Expressions.printWithComma(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2048 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2049 | } |
| 2050 | }; |
| 2051 | |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 2052 | class PointerToMemberConversionExpr : public Node { |
| 2053 | const Node *Type; |
| 2054 | const Node *SubExpr; |
| 2055 | StringView Offset; |
| 2056 | |
| 2057 | public: |
| 2058 | PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_, |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2059 | StringView Offset_, Prec Prec_) |
| 2060 | : Node(KPointerToMemberConversionExpr, Prec_), Type(Type_), |
| 2061 | SubExpr(SubExpr_), Offset(Offset_) {} |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 2062 | |
Nathan Sidwell | dafcca2 | 2022-03-29 04:43:16 -0700 | [diff] [blame] | 2063 | template <typename Fn> void match(Fn F) const { |
| 2064 | F(Type, SubExpr, Offset, getPrecedence()); |
| 2065 | } |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 2066 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2067 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2068 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2069 | Type->print(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2070 | OB.printClose(); |
| 2071 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2072 | SubExpr->print(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2073 | OB.printClose(); |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 2074 | } |
| 2075 | }; |
| 2076 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2077 | class InitListExpr : public Node { |
| 2078 | const Node *Ty; |
| 2079 | NodeArray Inits; |
| 2080 | public: |
| 2081 | InitListExpr(const Node *Ty_, NodeArray Inits_) |
| 2082 | : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {} |
| 2083 | |
| 2084 | template<typename Fn> void match(Fn F) const { F(Ty, Inits); } |
| 2085 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2086 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2087 | if (Ty) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2088 | Ty->print(OB); |
| 2089 | OB += '{'; |
| 2090 | Inits.printWithComma(OB); |
| 2091 | OB += '}'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2092 | } |
| 2093 | }; |
| 2094 | |
| 2095 | class BracedExpr : public Node { |
| 2096 | const Node *Elem; |
| 2097 | const Node *Init; |
| 2098 | bool IsArray; |
| 2099 | public: |
| 2100 | BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_) |
| 2101 | : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {} |
| 2102 | |
| 2103 | template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); } |
| 2104 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2105 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2106 | if (IsArray) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2107 | OB += '['; |
| 2108 | Elem->print(OB); |
| 2109 | OB += ']'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2110 | } else { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2111 | OB += '.'; |
| 2112 | Elem->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2113 | } |
| 2114 | if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2115 | OB += " = "; |
| 2116 | Init->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2117 | } |
| 2118 | }; |
| 2119 | |
| 2120 | class BracedRangeExpr : public Node { |
| 2121 | const Node *First; |
| 2122 | const Node *Last; |
| 2123 | const Node *Init; |
| 2124 | public: |
| 2125 | BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_) |
| 2126 | : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {} |
| 2127 | |
| 2128 | template<typename Fn> void match(Fn F) const { F(First, Last, Init); } |
| 2129 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2130 | void printLeft(OutputBuffer &OB) const override { |
| 2131 | OB += '['; |
| 2132 | First->print(OB); |
| 2133 | OB += " ... "; |
| 2134 | Last->print(OB); |
| 2135 | OB += ']'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2136 | if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2137 | OB += " = "; |
| 2138 | Init->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2139 | } |
| 2140 | }; |
| 2141 | |
| 2142 | class FoldExpr : public Node { |
| 2143 | const Node *Pack, *Init; |
| 2144 | StringView OperatorName; |
| 2145 | bool IsLeftFold; |
| 2146 | |
| 2147 | public: |
| 2148 | FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_, |
| 2149 | const Node *Init_) |
| 2150 | : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_), |
| 2151 | IsLeftFold(IsLeftFold_) {} |
| 2152 | |
| 2153 | template<typename Fn> void match(Fn F) const { |
| 2154 | F(IsLeftFold, OperatorName, Pack, Init); |
| 2155 | } |
| 2156 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2157 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2158 | auto PrintPack = [&] { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2159 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2160 | ParameterPackExpansion(Pack).print(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2161 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2162 | }; |
| 2163 | |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2164 | OB.printOpen(); |
| 2165 | // Either '[init op ]... op pack' or 'pack op ...[ op init]' |
| 2166 | // Refactored to '[(init|pack) op ]...[ op (pack|init)]' |
| 2167 | // Fold expr operands are cast-expressions |
| 2168 | if (!IsLeftFold || Init != nullptr) { |
| 2169 | // '(init|pack) op ' |
| 2170 | if (IsLeftFold) |
| 2171 | Init->printAsOperand(OB, Prec::Cast, true); |
| 2172 | else |
| 2173 | PrintPack(); |
| 2174 | OB << " " << OperatorName << " "; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2175 | } |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2176 | OB << "..."; |
| 2177 | if (IsLeftFold || Init != nullptr) { |
| 2178 | // ' op (init|pack)' |
| 2179 | OB << " " << OperatorName << " "; |
| 2180 | if (IsLeftFold) |
| 2181 | PrintPack(); |
| 2182 | else |
| 2183 | Init->printAsOperand(OB, Prec::Cast, true); |
| 2184 | } |
| 2185 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2186 | } |
| 2187 | }; |
| 2188 | |
| 2189 | class ThrowExpr : public Node { |
| 2190 | const Node *Op; |
| 2191 | |
| 2192 | public: |
| 2193 | ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {} |
| 2194 | |
| 2195 | template<typename Fn> void match(Fn F) const { F(Op); } |
| 2196 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2197 | void printLeft(OutputBuffer &OB) const override { |
| 2198 | OB += "throw "; |
| 2199 | Op->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2200 | } |
| 2201 | }; |
| 2202 | |
| 2203 | class BoolExpr : public Node { |
| 2204 | bool Value; |
| 2205 | |
| 2206 | public: |
| 2207 | BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {} |
| 2208 | |
| 2209 | template<typename Fn> void match(Fn F) const { F(Value); } |
| 2210 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2211 | void printLeft(OutputBuffer &OB) const override { |
| 2212 | OB += Value ? StringView("true") : StringView("false"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2213 | } |
| 2214 | }; |
| 2215 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2216 | class StringLiteral : public Node { |
| 2217 | const Node *Type; |
| 2218 | |
| 2219 | public: |
| 2220 | StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {} |
| 2221 | |
| 2222 | template<typename Fn> void match(Fn F) const { F(Type); } |
| 2223 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2224 | void printLeft(OutputBuffer &OB) const override { |
| 2225 | OB += "\"<"; |
| 2226 | Type->print(OB); |
| 2227 | OB += ">\""; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2228 | } |
| 2229 | }; |
| 2230 | |
| 2231 | class LambdaExpr : public Node { |
| 2232 | const Node *Type; |
| 2233 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2234 | public: |
| 2235 | LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {} |
| 2236 | |
| 2237 | template<typename Fn> void match(Fn F) const { F(Type); } |
| 2238 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2239 | void printLeft(OutputBuffer &OB) const override { |
| 2240 | OB += "[]"; |
Richard Smith | fb91746 | 2019-09-09 22:26:04 +0000 | [diff] [blame] | 2241 | if (Type->getKind() == KClosureTypeName) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2242 | static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB); |
| 2243 | OB += "{...}"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2244 | } |
| 2245 | }; |
| 2246 | |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 2247 | class EnumLiteral : public Node { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2248 | // ty(integer) |
| 2249 | const Node *Ty; |
| 2250 | StringView Integer; |
| 2251 | |
| 2252 | public: |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 2253 | EnumLiteral(const Node *Ty_, StringView Integer_) |
| 2254 | : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2255 | |
| 2256 | template<typename Fn> void match(Fn F) const { F(Ty, Integer); } |
| 2257 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2258 | void printLeft(OutputBuffer &OB) const override { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2259 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2260 | Ty->print(OB); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2261 | OB.printClose(); |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 2262 | |
| 2263 | if (Integer[0] == 'n') |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2264 | OB << "-" << Integer.dropFront(1); |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 2265 | else |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2266 | OB << Integer; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2267 | } |
| 2268 | }; |
| 2269 | |
| 2270 | class IntegerLiteral : public Node { |
| 2271 | StringView Type; |
| 2272 | StringView Value; |
| 2273 | |
| 2274 | public: |
| 2275 | IntegerLiteral(StringView Type_, StringView Value_) |
| 2276 | : Node(KIntegerLiteral), Type(Type_), Value(Value_) {} |
| 2277 | |
| 2278 | template<typename Fn> void match(Fn F) const { F(Type, Value); } |
| 2279 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2280 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2281 | if (Type.size() > 3) { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2282 | OB.printOpen(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2283 | OB += Type; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2284 | OB.printClose(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | if (Value[0] == 'n') { |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2288 | OB += '-'; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2289 | OB += Value.dropFront(1); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2290 | } else |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2291 | OB += Value; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2292 | |
| 2293 | if (Type.size() <= 3) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2294 | OB += Type; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2295 | } |
| 2296 | }; |
| 2297 | |
| 2298 | template <class Float> struct FloatData; |
| 2299 | |
| 2300 | namespace float_literal_impl { |
| 2301 | constexpr Node::Kind getFloatLiteralKind(float *) { |
| 2302 | return Node::KFloatLiteral; |
| 2303 | } |
| 2304 | constexpr Node::Kind getFloatLiteralKind(double *) { |
| 2305 | return Node::KDoubleLiteral; |
| 2306 | } |
| 2307 | constexpr Node::Kind getFloatLiteralKind(long double *) { |
| 2308 | return Node::KLongDoubleLiteral; |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | template <class Float> class FloatLiteralImpl : public Node { |
| 2313 | const StringView Contents; |
| 2314 | |
| 2315 | static constexpr Kind KindForClass = |
| 2316 | float_literal_impl::getFloatLiteralKind((Float *)nullptr); |
| 2317 | |
| 2318 | public: |
| 2319 | FloatLiteralImpl(StringView Contents_) |
| 2320 | : Node(KindForClass), Contents(Contents_) {} |
| 2321 | |
| 2322 | template<typename Fn> void match(Fn F) const { F(Contents); } |
| 2323 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2324 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2325 | const char *first = Contents.begin(); |
| 2326 | const char *last = Contents.end() + 1; |
| 2327 | |
| 2328 | const size_t N = FloatData<Float>::mangled_size; |
| 2329 | if (static_cast<std::size_t>(last - first) > N) { |
| 2330 | last = first + N; |
| 2331 | union { |
| 2332 | Float value; |
| 2333 | char buf[sizeof(Float)]; |
| 2334 | }; |
| 2335 | const char *t = first; |
| 2336 | char *e = buf; |
| 2337 | for (; t != last; ++t, ++e) { |
| 2338 | unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0') |
| 2339 | : static_cast<unsigned>(*t - 'a' + 10); |
| 2340 | ++t; |
| 2341 | unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0') |
| 2342 | : static_cast<unsigned>(*t - 'a' + 10); |
| 2343 | *e = static_cast<char>((d1 << 4) + d0); |
| 2344 | } |
| 2345 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 2346 | std::reverse(buf, e); |
| 2347 | #endif |
| 2348 | char num[FloatData<Float>::max_demangled_size] = {0}; |
| 2349 | int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2350 | OB += StringView(num, num + n); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2351 | } |
| 2352 | } |
| 2353 | }; |
| 2354 | |
| 2355 | using FloatLiteral = FloatLiteralImpl<float>; |
| 2356 | using DoubleLiteral = FloatLiteralImpl<double>; |
| 2357 | using LongDoubleLiteral = FloatLiteralImpl<long double>; |
| 2358 | |
| 2359 | /// Visit the node. Calls \c F(P), where \c P is the node cast to the |
| 2360 | /// appropriate derived class. |
| 2361 | template<typename Fn> |
| 2362 | void Node::visit(Fn F) const { |
| 2363 | switch (K) { |
Nathan Sidwell | 8b55cc0 | 2022-03-30 05:59:16 -0700 | [diff] [blame] | 2364 | #define NODE(X) \ |
| 2365 | case K##X: \ |
| 2366 | return F(static_cast<const X *>(this)); |
| 2367 | #include "ItaniumNodes.def" |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2368 | } |
| 2369 | assert(0 && "unknown mangling node kind"); |
| 2370 | } |
| 2371 | |
| 2372 | /// Determine the kind of a node from its type. |
| 2373 | template<typename NodeT> struct NodeKind; |
Nathan Sidwell | 8b55cc0 | 2022-03-30 05:59:16 -0700 | [diff] [blame] | 2374 | #define NODE(X) \ |
| 2375 | template <> struct NodeKind<X> { \ |
| 2376 | static constexpr Node::Kind Kind = Node::K##X; \ |
| 2377 | static constexpr const char *name() { return #X; } \ |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2378 | }; |
Nathan Sidwell | 8b55cc0 | 2022-03-30 05:59:16 -0700 | [diff] [blame] | 2379 | #include "ItaniumNodes.def" |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2380 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2381 | template <typename Derived, typename Alloc> struct AbstractManglingParser { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2382 | const char *First; |
| 2383 | const char *Last; |
| 2384 | |
| 2385 | // Name stack, this is used by the parser to hold temporary names that were |
| 2386 | // parsed. The parser collapses multiple names into new nodes to construct |
| 2387 | // the AST. Once the parser is finished, names.size() == 1. |
| 2388 | PODSmallVector<Node *, 32> Names; |
| 2389 | |
| 2390 | // Substitution table. Itanium supports name substitutions as a means of |
| 2391 | // compression. The string "S42_" refers to the 44nd entry (base-36) in this |
| 2392 | // table. |
| 2393 | PODSmallVector<Node *, 32> Subs; |
| 2394 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2395 | using TemplateParamList = PODSmallVector<Node *, 8>; |
| 2396 | |
| 2397 | class ScopedTemplateParamList { |
| 2398 | AbstractManglingParser *Parser; |
| 2399 | size_t OldNumTemplateParamLists; |
| 2400 | TemplateParamList Params; |
| 2401 | |
| 2402 | public: |
Louis Dionne | c1fe867 | 2020-10-30 17:33:02 -0400 | [diff] [blame] | 2403 | ScopedTemplateParamList(AbstractManglingParser *TheParser) |
| 2404 | : Parser(TheParser), |
| 2405 | OldNumTemplateParamLists(TheParser->TemplateParams.size()) { |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2406 | Parser->TemplateParams.push_back(&Params); |
| 2407 | } |
| 2408 | ~ScopedTemplateParamList() { |
| 2409 | assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists); |
| 2410 | Parser->TemplateParams.dropBack(OldNumTemplateParamLists); |
| 2411 | } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2412 | }; |
| 2413 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2414 | // Template parameter table. Like the above, but referenced like "T42_". |
| 2415 | // This has a smaller size compared to Subs and Names because it can be |
| 2416 | // stored on the stack. |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2417 | TemplateParamList OuterTemplateParams; |
| 2418 | |
| 2419 | // Lists of template parameters indexed by template parameter depth, |
| 2420 | // referenced like "TL2_4_". If nonempty, element 0 is always |
| 2421 | // OuterTemplateParams; inner elements are always template parameter lists of |
| 2422 | // lambda expressions. For a generic lambda with no explicit template |
| 2423 | // parameter list, the corresponding parameter list pointer will be null. |
| 2424 | PODSmallVector<TemplateParamList *, 4> TemplateParams; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2425 | |
| 2426 | // Set of unresolved forward <template-param> references. These can occur in a |
| 2427 | // conversion operator's type, and are resolved in the enclosing <encoding>. |
| 2428 | PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs; |
| 2429 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2430 | bool TryToParseTemplateArgs = true; |
| 2431 | bool PermitForwardTemplateReferences = false; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2432 | size_t ParsingLambdaParamsAtLevel = (size_t)-1; |
| 2433 | |
| 2434 | unsigned NumSyntheticTemplateParameters[3] = {}; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2435 | |
| 2436 | Alloc ASTAllocator; |
| 2437 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2438 | AbstractManglingParser(const char *First_, const char *Last_) |
| 2439 | : First(First_), Last(Last_) {} |
| 2440 | |
| 2441 | Derived &getDerived() { return static_cast<Derived &>(*this); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2442 | |
| 2443 | void reset(const char *First_, const char *Last_) { |
| 2444 | First = First_; |
| 2445 | Last = Last_; |
| 2446 | Names.clear(); |
| 2447 | Subs.clear(); |
| 2448 | TemplateParams.clear(); |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2449 | ParsingLambdaParamsAtLevel = (size_t)-1; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2450 | TryToParseTemplateArgs = true; |
| 2451 | PermitForwardTemplateReferences = false; |
Richard Smith | 9a2307a | 2019-09-07 00:11:53 +0000 | [diff] [blame] | 2452 | for (int I = 0; I != 3; ++I) |
| 2453 | NumSyntheticTemplateParameters[I] = 0; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2454 | ASTAllocator.reset(); |
| 2455 | } |
| 2456 | |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 2457 | template <class T, class... Args> Node *make(Args &&... args) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2458 | return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...); |
| 2459 | } |
| 2460 | |
| 2461 | template <class It> NodeArray makeNodeArray(It begin, It end) { |
| 2462 | size_t sz = static_cast<size_t>(end - begin); |
| 2463 | void *mem = ASTAllocator.allocateNodeArray(sz); |
| 2464 | Node **data = new (mem) Node *[sz]; |
| 2465 | std::copy(begin, end, data); |
| 2466 | return NodeArray(data, sz); |
| 2467 | } |
| 2468 | |
| 2469 | NodeArray popTrailingNodeArray(size_t FromPosition) { |
| 2470 | assert(FromPosition <= Names.size()); |
| 2471 | NodeArray res = |
| 2472 | makeNodeArray(Names.begin() + (long)FromPosition, Names.end()); |
| 2473 | Names.dropBack(FromPosition); |
| 2474 | return res; |
| 2475 | } |
| 2476 | |
| 2477 | bool consumeIf(StringView S) { |
| 2478 | if (StringView(First, Last).startsWith(S)) { |
| 2479 | First += S.size(); |
| 2480 | return true; |
| 2481 | } |
| 2482 | return false; |
| 2483 | } |
| 2484 | |
| 2485 | bool consumeIf(char C) { |
| 2486 | if (First != Last && *First == C) { |
| 2487 | ++First; |
| 2488 | return true; |
| 2489 | } |
| 2490 | return false; |
| 2491 | } |
| 2492 | |
| 2493 | char consume() { return First != Last ? *First++ : '\0'; } |
| 2494 | |
Nathan Sidwell | fd0ef6d | 2022-01-20 07:40:12 -0800 | [diff] [blame] | 2495 | char look(unsigned Lookahead = 0) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2496 | if (static_cast<size_t>(Last - First) <= Lookahead) |
| 2497 | return '\0'; |
| 2498 | return First[Lookahead]; |
| 2499 | } |
| 2500 | |
| 2501 | size_t numLeft() const { return static_cast<size_t>(Last - First); } |
| 2502 | |
| 2503 | StringView parseNumber(bool AllowNegative = false); |
| 2504 | Qualifiers parseCVQualifiers(); |
| 2505 | bool parsePositiveInteger(size_t *Out); |
| 2506 | StringView parseBareSourceName(); |
| 2507 | |
| 2508 | bool parseSeqId(size_t *Out); |
| 2509 | Node *parseSubstitution(); |
| 2510 | Node *parseTemplateParam(); |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2511 | Node *parseTemplateParamDecl(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2512 | Node *parseTemplateArgs(bool TagTemplates = false); |
| 2513 | Node *parseTemplateArg(); |
| 2514 | |
| 2515 | /// Parse the <expr> production. |
| 2516 | Node *parseExpr(); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2517 | Node *parsePrefixExpr(StringView Kind, Node::Prec Prec); |
| 2518 | Node *parseBinaryExpr(StringView Kind, Node::Prec Prec); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2519 | Node *parseIntegerLiteral(StringView Lit); |
| 2520 | Node *parseExprPrimary(); |
| 2521 | template <class Float> Node *parseFloatingLiteral(); |
| 2522 | Node *parseFunctionParam(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2523 | Node *parseConversionExpr(); |
| 2524 | Node *parseBracedExpr(); |
| 2525 | Node *parseFoldExpr(); |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2526 | Node *parsePointerToMemberConversionExpr(Node::Prec Prec); |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 2527 | Node *parseSubobjectExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2528 | |
| 2529 | /// Parse the <type> production. |
| 2530 | Node *parseType(); |
| 2531 | Node *parseFunctionType(); |
| 2532 | Node *parseVectorType(); |
| 2533 | Node *parseDecltype(); |
| 2534 | Node *parseArrayType(); |
| 2535 | Node *parsePointerToMemberType(); |
| 2536 | Node *parseClassEnumType(); |
| 2537 | Node *parseQualifiedType(); |
| 2538 | |
| 2539 | Node *parseEncoding(); |
| 2540 | bool parseCallOffset(); |
| 2541 | Node *parseSpecialName(); |
| 2542 | |
| 2543 | /// Holds some extra information about a <name> that is being parsed. This |
| 2544 | /// information is only pertinent if the <name> refers to an <encoding>. |
| 2545 | struct NameState { |
| 2546 | bool CtorDtorConversion = false; |
| 2547 | bool EndsWithTemplateArgs = false; |
| 2548 | Qualifiers CVQualifiers = QualNone; |
| 2549 | FunctionRefQual ReferenceQualifier = FrefQualNone; |
| 2550 | size_t ForwardTemplateRefsBegin; |
| 2551 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2552 | NameState(AbstractManglingParser *Enclosing) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2553 | : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {} |
| 2554 | }; |
| 2555 | |
| 2556 | bool resolveForwardTemplateRefs(NameState &State) { |
| 2557 | size_t I = State.ForwardTemplateRefsBegin; |
| 2558 | size_t E = ForwardTemplateRefs.size(); |
| 2559 | for (; I < E; ++I) { |
| 2560 | size_t Idx = ForwardTemplateRefs[I]->Index; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2561 | if (TemplateParams.empty() || !TemplateParams[0] || |
| 2562 | Idx >= TemplateParams[0]->size()) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2563 | return true; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2564 | ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx]; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2565 | } |
| 2566 | ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin); |
| 2567 | return false; |
| 2568 | } |
| 2569 | |
| 2570 | /// Parse the <name> production> |
| 2571 | Node *parseName(NameState *State = nullptr); |
| 2572 | Node *parseLocalName(NameState *State); |
| 2573 | Node *parseOperatorName(NameState *State); |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2574 | bool parseModuleNameOpt(ModuleName *&Module); |
| 2575 | Node *parseUnqualifiedName(NameState *State, Node *Scope, ModuleName *Module); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2576 | Node *parseUnnamedTypeName(NameState *State); |
| 2577 | Node *parseSourceName(NameState *State); |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2578 | Node *parseUnscopedName(NameState *State, bool *isSubstName); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2579 | Node *parseNestedName(NameState *State); |
| 2580 | Node *parseCtorDtorName(Node *&SoFar, NameState *State); |
| 2581 | |
| 2582 | Node *parseAbiTags(Node *N); |
| 2583 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 2584 | struct OperatorInfo { |
| 2585 | enum OIKind : unsigned char { |
| 2586 | Prefix, // Prefix unary: @ expr |
| 2587 | Postfix, // Postfix unary: expr @ |
| 2588 | Binary, // Binary: lhs @ rhs |
| 2589 | Array, // Array index: lhs [ rhs ] |
| 2590 | Member, // Member access: lhs @ rhs |
| 2591 | New, // New |
| 2592 | Del, // Delete |
| 2593 | Call, // Function call: expr (expr*) |
| 2594 | CCast, // C cast: (type)expr |
| 2595 | Conditional, // Conditional: expr ? expr : expr |
Nathan Sidwell | 0dda3d4 | 2022-02-18 09:51:24 -0800 | [diff] [blame] | 2596 | NameOnly, // Overload only, not allowed in expression. |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 2597 | // Below do not have operator names |
| 2598 | NamedCast, // Named cast, @<type>(expr) |
| 2599 | OfIdOp, // alignof, sizeof, typeid |
| 2600 | |
| 2601 | Unnameable = NamedCast, |
| 2602 | }; |
| 2603 | char Enc[2]; // Encoding |
| 2604 | OIKind Kind; // Kind of operator |
| 2605 | bool Flag : 1; // Entry-specific flag |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2606 | Node::Prec Prec : 7; // Precedence |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 2607 | const char *Name; // Spelling |
| 2608 | |
| 2609 | public: |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2610 | constexpr OperatorInfo(const char (&E)[3], OIKind K, bool F, Node::Prec P, |
| 2611 | const char *N) |
| 2612 | : Enc{E[0], E[1]}, Kind{K}, Flag{F}, Prec{P}, Name{N} {} |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 2613 | |
| 2614 | public: |
| 2615 | bool operator<(const OperatorInfo &Other) const { |
| 2616 | return *this < Other.Enc; |
| 2617 | } |
| 2618 | bool operator<(const char *Peek) const { |
| 2619 | return Enc[0] < Peek[0] || (Enc[0] == Peek[0] && Enc[1] < Peek[1]); |
| 2620 | } |
| 2621 | bool operator==(const char *Peek) const { |
| 2622 | return Enc[0] == Peek[0] && Enc[1] == Peek[1]; |
| 2623 | } |
| 2624 | bool operator!=(const char *Peek) const { return !this->operator==(Peek); } |
| 2625 | |
| 2626 | public: |
| 2627 | StringView getSymbol() const { |
| 2628 | StringView Res = Name; |
| 2629 | if (Kind < Unnameable) { |
| 2630 | assert(Res.startsWith("operator") && |
| 2631 | "operator name does not start with 'operator'"); |
| 2632 | Res = Res.dropFront(sizeof("operator") - 1); |
| 2633 | Res.consumeFront(' '); |
| 2634 | } |
| 2635 | return Res; |
| 2636 | } |
| 2637 | StringView getName() const { return Name; } |
| 2638 | OIKind getKind() const { return Kind; } |
| 2639 | bool getFlag() const { return Flag; } |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 2640 | Node::Prec getPrecedence() const { return Prec; } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 2641 | }; |
Nathan Sidwell | 7d9bbeb | 2022-04-08 06:55:31 -0700 | [diff] [blame] | 2642 | static const OperatorInfo Ops[]; |
| 2643 | static const size_t NumOps; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 2644 | const OperatorInfo *parseOperatorEncoding(); |
| 2645 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2646 | /// Parse the <unresolved-name> production. |
Nathan Sidwell | 77c52e2 | 2022-01-28 11:59:03 -0800 | [diff] [blame] | 2647 | Node *parseUnresolvedName(bool Global); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2648 | Node *parseSimpleId(); |
| 2649 | Node *parseBaseUnresolvedName(); |
| 2650 | Node *parseUnresolvedType(); |
| 2651 | Node *parseDestructorName(); |
| 2652 | |
| 2653 | /// Top-level entry point into the parser. |
| 2654 | Node *parse(); |
| 2655 | }; |
| 2656 | |
| 2657 | const char* parse_discriminator(const char* first, const char* last); |
| 2658 | |
| 2659 | // <name> ::= <nested-name> // N |
| 2660 | // ::= <local-name> # See Scope Encoding below // Z |
| 2661 | // ::= <unscoped-template-name> <template-args> |
| 2662 | // ::= <unscoped-name> |
| 2663 | // |
| 2664 | // <unscoped-template-name> ::= <unscoped-name> |
| 2665 | // ::= <substitution> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2666 | template <typename Derived, typename Alloc> |
| 2667 | Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2668 | if (look() == 'N') |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2669 | return getDerived().parseNestedName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2670 | if (look() == 'Z') |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2671 | return getDerived().parseLocalName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2672 | |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 2673 | Node *Result = nullptr; |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2674 | bool IsSubst = false; |
| 2675 | |
| 2676 | Result = getDerived().parseUnscopedName(State, &IsSubst); |
| 2677 | if (!Result) |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 2678 | return nullptr; |
| 2679 | |
| 2680 | if (look() == 'I') { |
| 2681 | // ::= <unscoped-template-name> <template-args> |
| 2682 | if (!IsSubst) |
| 2683 | // An unscoped-template-name is substitutable. |
| 2684 | Subs.push_back(Result); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2685 | Node *TA = getDerived().parseTemplateArgs(State != nullptr); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2686 | if (TA == nullptr) |
| 2687 | return nullptr; |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 2688 | if (State) |
| 2689 | State->EndsWithTemplateArgs = true; |
| 2690 | Result = make<NameWithTemplateArgs>(Result, TA); |
| 2691 | } else if (IsSubst) { |
| 2692 | // The substitution case must be followed by <template-args>. |
| 2693 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 2696 | return Result; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | // <local-name> := Z <function encoding> E <entity name> [<discriminator>] |
| 2700 | // := Z <function encoding> E s [<discriminator>] |
| 2701 | // := Z <function encoding> Ed [ <parameter number> ] _ <entity name> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2702 | template <typename Derived, typename Alloc> |
| 2703 | Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2704 | if (!consumeIf('Z')) |
| 2705 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2706 | Node *Encoding = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2707 | if (Encoding == nullptr || !consumeIf('E')) |
| 2708 | return nullptr; |
| 2709 | |
| 2710 | if (consumeIf('s')) { |
| 2711 | First = parse_discriminator(First, Last); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 2712 | auto *StringLitName = make<NameType>("string literal"); |
| 2713 | if (!StringLitName) |
| 2714 | return nullptr; |
| 2715 | return make<LocalName>(Encoding, StringLitName); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | if (consumeIf('d')) { |
| 2719 | parseNumber(true); |
| 2720 | if (!consumeIf('_')) |
| 2721 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2722 | Node *N = getDerived().parseName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2723 | if (N == nullptr) |
| 2724 | return nullptr; |
| 2725 | return make<LocalName>(Encoding, N); |
| 2726 | } |
| 2727 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2728 | Node *Entity = getDerived().parseName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2729 | if (Entity == nullptr) |
| 2730 | return nullptr; |
| 2731 | First = parse_discriminator(First, Last); |
| 2732 | return make<LocalName>(Encoding, Entity); |
| 2733 | } |
| 2734 | |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 2735 | // <unscoped-name> ::= <unqualified-name> |
| 2736 | // ::= St <unqualified-name> # ::std:: |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 2737 | // [*] extension |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2738 | template <typename Derived, typename Alloc> |
| 2739 | Node * |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2740 | AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State, |
| 2741 | bool *IsSubst) { |
| 2742 | |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2743 | Node *Std = nullptr; |
| 2744 | if (consumeIf("St")) { |
| 2745 | Std = make<NameType>("std"); |
| 2746 | if (Std == nullptr) |
Nathan Sidwell | 200e97c | 2022-01-21 11:37:01 -0800 | [diff] [blame] | 2747 | return nullptr; |
| 2748 | } |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 2749 | |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2750 | Node *Res = nullptr; |
| 2751 | ModuleName *Module = nullptr; |
| 2752 | if (look() == 'S') { |
| 2753 | Node *S = getDerived().parseSubstitution(); |
| 2754 | if (!S) |
| 2755 | return nullptr; |
| 2756 | if (S->getKind() == Node::KModuleName) |
| 2757 | Module = static_cast<ModuleName *>(S); |
| 2758 | else if (IsSubst && Std == nullptr) { |
| 2759 | Res = S; |
| 2760 | *IsSubst = true; |
| 2761 | } else { |
| 2762 | return nullptr; |
| 2763 | } |
| 2764 | } |
| 2765 | |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 2766 | if (Res == nullptr || Std != nullptr) { |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2767 | Res = getDerived().parseUnqualifiedName(State, Std, Module); |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 2768 | } |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2769 | |
| 2770 | return Res; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 2773 | // <unqualified-name> ::= [<module-name>] L? <operator-name> [<abi-tags>] |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2774 | // ::= [<module-name>] <ctor-dtor-name> [<abi-tags>] |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 2775 | // ::= [<module-name>] L? <source-name> [<abi-tags>] |
| 2776 | // ::= [<module-name>] L? <unnamed-type-name> [<abi-tags>] |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2777 | // # structured binding declaration |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 2778 | // ::= [<module-name>] L? DC <source-name>+ E |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2779 | template <typename Derived, typename Alloc> |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2780 | Node *AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName( |
| 2781 | NameState *State, Node *Scope, ModuleName *Module) { |
| 2782 | if (getDerived().parseModuleNameOpt(Module)) |
| 2783 | return nullptr; |
| 2784 | |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 2785 | consumeIf('L'); |
| 2786 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2787 | Node *Result; |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 2788 | if (look() >= '1' && look() <= '9') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2789 | Result = getDerived().parseSourceName(State); |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 2790 | } else if (look() == 'U') { |
| 2791 | Result = getDerived().parseUnnamedTypeName(State); |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2792 | } else if (consumeIf("DC")) { |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2793 | // Structured binding |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2794 | size_t BindingsBegin = Names.size(); |
| 2795 | do { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2796 | Node *Binding = getDerived().parseSourceName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2797 | if (Binding == nullptr) |
| 2798 | return nullptr; |
| 2799 | Names.push_back(Binding); |
| 2800 | } while (!consumeIf('E')); |
| 2801 | Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin)); |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2802 | } else if (look() == 'C' || look() == 'D') { |
| 2803 | // A <ctor-dtor-name>. |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2804 | if (Scope == nullptr || Module != nullptr) |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2805 | return nullptr; |
| 2806 | Result = getDerived().parseCtorDtorName(Scope, State); |
| 2807 | } else { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2808 | Result = getDerived().parseOperatorName(State); |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2809 | } |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2810 | |
David Blaikie | 019fb1b | 2022-03-30 20:18:40 +0000 | [diff] [blame] | 2811 | if (Result != nullptr && Module != nullptr) |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2812 | Result = make<ModuleEntity>(Module, Result); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2813 | if (Result != nullptr) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2814 | Result = getDerived().parseAbiTags(Result); |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2815 | if (Result != nullptr && Scope != nullptr) |
| 2816 | Result = make<NestedName>(Scope, Result); |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2817 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2818 | return Result; |
| 2819 | } |
| 2820 | |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 2821 | // <module-name> ::= <module-subname> |
| 2822 | // ::= <module-name> <module-subname> |
| 2823 | // ::= <substitution> # passed in by caller |
| 2824 | // <module-subname> ::= W <source-name> |
| 2825 | // ::= W P <source-name> |
| 2826 | template <typename Derived, typename Alloc> |
| 2827 | bool AbstractManglingParser<Derived, Alloc>::parseModuleNameOpt( |
| 2828 | ModuleName *&Module) { |
| 2829 | while (consumeIf('W')) { |
| 2830 | bool IsPartition = consumeIf('P'); |
| 2831 | Node *Sub = getDerived().parseSourceName(nullptr); |
| 2832 | if (!Sub) |
| 2833 | return true; |
| 2834 | Module = |
| 2835 | static_cast<ModuleName *>(make<ModuleName>(Module, Sub, IsPartition)); |
| 2836 | Subs.push_back(Module); |
| 2837 | } |
| 2838 | |
| 2839 | return false; |
| 2840 | } |
| 2841 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2842 | // <unnamed-type-name> ::= Ut [<nonnegative number>] _ |
| 2843 | // ::= <closure-type-name> |
| 2844 | // |
| 2845 | // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ |
| 2846 | // |
| 2847 | // <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2848 | template <typename Derived, typename Alloc> |
| 2849 | Node * |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2850 | AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) { |
| 2851 | // <template-params> refer to the innermost <template-args>. Clear out any |
| 2852 | // outer args that we may have inserted into TemplateParams. |
| 2853 | if (State != nullptr) |
| 2854 | TemplateParams.clear(); |
| 2855 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2856 | if (consumeIf("Ut")) { |
| 2857 | StringView Count = parseNumber(); |
| 2858 | if (!consumeIf('_')) |
| 2859 | return nullptr; |
| 2860 | return make<UnnamedTypeName>(Count); |
| 2861 | } |
| 2862 | if (consumeIf("Ul")) { |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 2863 | ScopedOverride<size_t> SwapParams(ParsingLambdaParamsAtLevel, |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2864 | TemplateParams.size()); |
| 2865 | ScopedTemplateParamList LambdaTemplateParams(this); |
| 2866 | |
| 2867 | size_t ParamsBegin = Names.size(); |
| 2868 | while (look() == 'T' && |
| 2869 | StringView("yptn").find(look(1)) != StringView::npos) { |
| 2870 | Node *T = parseTemplateParamDecl(); |
| 2871 | if (!T) |
| 2872 | return nullptr; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2873 | Names.push_back(T); |
| 2874 | } |
| 2875 | NodeArray TempParams = popTrailingNodeArray(ParamsBegin); |
| 2876 | |
| 2877 | // FIXME: If TempParams is empty and none of the function parameters |
| 2878 | // includes 'auto', we should remove LambdaTemplateParams from the |
| 2879 | // TemplateParams list. Unfortunately, we don't find out whether there are |
| 2880 | // any 'auto' parameters until too late in an example such as: |
| 2881 | // |
| 2882 | // template<typename T> void f( |
| 2883 | // decltype([](decltype([]<typename T>(T v) {}), |
| 2884 | // auto) {})) {} |
| 2885 | // template<typename T> void f( |
| 2886 | // decltype([](decltype([]<typename T>(T w) {}), |
| 2887 | // int) {})) {} |
| 2888 | // |
| 2889 | // Here, the type of v is at level 2 but the type of w is at level 1. We |
| 2890 | // don't find this out until we encounter the type of the next parameter. |
| 2891 | // |
| 2892 | // However, compilers can't actually cope with the former example in |
| 2893 | // practice, and it's likely to be made ill-formed in future, so we don't |
| 2894 | // need to support it here. |
| 2895 | // |
| 2896 | // If we encounter an 'auto' in the function parameter types, we will |
| 2897 | // recreate a template parameter scope for it, but any intervening lambdas |
| 2898 | // will be parsed in the 'wrong' template parameter depth. |
| 2899 | if (TempParams.empty()) |
| 2900 | TemplateParams.pop_back(); |
| 2901 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2902 | if (!consumeIf("vE")) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2903 | do { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2904 | Node *P = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2905 | if (P == nullptr) |
| 2906 | return nullptr; |
| 2907 | Names.push_back(P); |
| 2908 | } while (!consumeIf('E')); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2909 | } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2910 | NodeArray Params = popTrailingNodeArray(ParamsBegin); |
| 2911 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2912 | StringView Count = parseNumber(); |
| 2913 | if (!consumeIf('_')) |
| 2914 | return nullptr; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2915 | return make<ClosureTypeName>(TempParams, Params, Count); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2916 | } |
Erik Pilkington | 974b654 | 2019-01-17 21:37:51 +0000 | [diff] [blame] | 2917 | if (consumeIf("Ub")) { |
| 2918 | (void)parseNumber(); |
| 2919 | if (!consumeIf('_')) |
| 2920 | return nullptr; |
| 2921 | return make<NameType>("'block-literal'"); |
| 2922 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2923 | return nullptr; |
| 2924 | } |
| 2925 | |
| 2926 | // <source-name> ::= <positive length number> <identifier> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2927 | template <typename Derived, typename Alloc> |
| 2928 | Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2929 | size_t Length = 0; |
| 2930 | if (parsePositiveInteger(&Length)) |
| 2931 | return nullptr; |
| 2932 | if (numLeft() < Length || Length == 0) |
| 2933 | return nullptr; |
| 2934 | StringView Name(First, First + Length); |
| 2935 | First += Length; |
| 2936 | if (Name.startsWith("_GLOBAL__N")) |
| 2937 | return make<NameType>("(anonymous namespace)"); |
| 2938 | return make<NameType>(Name); |
| 2939 | } |
| 2940 | |
Nathan Sidwell | 7d9bbeb | 2022-04-08 06:55:31 -0700 | [diff] [blame] | 2941 | // Operator encodings |
| 2942 | template <typename Derived, typename Alloc> |
| 2943 | const typename AbstractManglingParser< |
| 2944 | Derived, Alloc>::OperatorInfo AbstractManglingParser<Derived, |
| 2945 | Alloc>::Ops[] = { |
| 2946 | // Keep ordered by encoding |
| 2947 | {"aN", OperatorInfo::Binary, false, Node::Prec::Assign, "operator&="}, |
| 2948 | {"aS", OperatorInfo::Binary, false, Node::Prec::Assign, "operator="}, |
| 2949 | {"aa", OperatorInfo::Binary, false, Node::Prec::AndIf, "operator&&"}, |
| 2950 | {"ad", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator&"}, |
| 2951 | {"an", OperatorInfo::Binary, false, Node::Prec::And, "operator&"}, |
| 2952 | {"at", OperatorInfo::OfIdOp, /*Type*/ true, Node::Prec::Unary, "alignof "}, |
| 2953 | {"aw", OperatorInfo::NameOnly, false, Node::Prec::Primary, |
| 2954 | "operator co_await"}, |
| 2955 | {"az", OperatorInfo::OfIdOp, /*Type*/ false, Node::Prec::Unary, "alignof "}, |
| 2956 | {"cc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, "const_cast"}, |
| 2957 | {"cl", OperatorInfo::Call, false, Node::Prec::Postfix, "operator()"}, |
| 2958 | {"cm", OperatorInfo::Binary, false, Node::Prec::Comma, "operator,"}, |
| 2959 | {"co", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator~"}, |
| 2960 | {"cv", OperatorInfo::CCast, false, Node::Prec::Cast, "operator"}, // C Cast |
| 2961 | {"dV", OperatorInfo::Binary, false, Node::Prec::Assign, "operator/="}, |
| 2962 | {"da", OperatorInfo::Del, /*Ary*/ true, Node::Prec::Unary, |
| 2963 | "operator delete[]"}, |
| 2964 | {"dc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, "dynamic_cast"}, |
| 2965 | {"de", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator*"}, |
| 2966 | {"dl", OperatorInfo::Del, /*Ary*/ false, Node::Prec::Unary, |
| 2967 | "operator delete"}, |
| 2968 | {"ds", OperatorInfo::Member, /*Named*/ false, Node::Prec::PtrMem, |
| 2969 | "operator.*"}, |
| 2970 | {"dt", OperatorInfo::Member, /*Named*/ false, Node::Prec::Postfix, |
| 2971 | "operator."}, |
| 2972 | {"dv", OperatorInfo::Binary, false, Node::Prec::Assign, "operator/"}, |
| 2973 | {"eO", OperatorInfo::Binary, false, Node::Prec::Assign, "operator^="}, |
| 2974 | {"eo", OperatorInfo::Binary, false, Node::Prec::Xor, "operator^"}, |
| 2975 | {"eq", OperatorInfo::Binary, false, Node::Prec::Equality, "operator=="}, |
| 2976 | {"ge", OperatorInfo::Binary, false, Node::Prec::Relational, "operator>="}, |
| 2977 | {"gt", OperatorInfo::Binary, false, Node::Prec::Relational, "operator>"}, |
| 2978 | {"ix", OperatorInfo::Array, false, Node::Prec::Postfix, "operator[]"}, |
| 2979 | {"lS", OperatorInfo::Binary, false, Node::Prec::Assign, "operator<<="}, |
| 2980 | {"le", OperatorInfo::Binary, false, Node::Prec::Relational, "operator<="}, |
| 2981 | {"ls", OperatorInfo::Binary, false, Node::Prec::Shift, "operator<<"}, |
| 2982 | {"lt", OperatorInfo::Binary, false, Node::Prec::Relational, "operator<"}, |
| 2983 | {"mI", OperatorInfo::Binary, false, Node::Prec::Assign, "operator-="}, |
| 2984 | {"mL", OperatorInfo::Binary, false, Node::Prec::Assign, "operator*="}, |
| 2985 | {"mi", OperatorInfo::Binary, false, Node::Prec::Additive, "operator-"}, |
| 2986 | {"ml", OperatorInfo::Binary, false, Node::Prec::Multiplicative, |
| 2987 | "operator*"}, |
| 2988 | {"mm", OperatorInfo::Postfix, false, Node::Prec::Postfix, "operator--"}, |
| 2989 | {"na", OperatorInfo::New, /*Ary*/ true, Node::Prec::Unary, |
| 2990 | "operator new[]"}, |
| 2991 | {"ne", OperatorInfo::Binary, false, Node::Prec::Equality, "operator!="}, |
| 2992 | {"ng", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator-"}, |
| 2993 | {"nt", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator!"}, |
| 2994 | {"nw", OperatorInfo::New, /*Ary*/ false, Node::Prec::Unary, "operator new"}, |
| 2995 | {"oR", OperatorInfo::Binary, false, Node::Prec::Assign, "operator|="}, |
| 2996 | {"oo", OperatorInfo::Binary, false, Node::Prec::OrIf, "operator||"}, |
| 2997 | {"or", OperatorInfo::Binary, false, Node::Prec::Ior, "operator|"}, |
| 2998 | {"pL", OperatorInfo::Binary, false, Node::Prec::Assign, "operator+="}, |
| 2999 | {"pl", OperatorInfo::Binary, false, Node::Prec::Additive, "operator+"}, |
| 3000 | {"pm", OperatorInfo::Member, /*Named*/ false, Node::Prec::PtrMem, |
| 3001 | "operator->*"}, |
| 3002 | {"pp", OperatorInfo::Postfix, false, Node::Prec::Postfix, "operator++"}, |
| 3003 | {"ps", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator+"}, |
| 3004 | {"pt", OperatorInfo::Member, /*Named*/ true, Node::Prec::Postfix, |
| 3005 | "operator->"}, |
| 3006 | {"qu", OperatorInfo::Conditional, false, Node::Prec::Conditional, |
| 3007 | "operator?"}, |
| 3008 | {"rM", OperatorInfo::Binary, false, Node::Prec::Assign, "operator%="}, |
| 3009 | {"rS", OperatorInfo::Binary, false, Node::Prec::Assign, "operator>>="}, |
| 3010 | {"rc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, |
| 3011 | "reinterpret_cast"}, |
| 3012 | {"rm", OperatorInfo::Binary, false, Node::Prec::Multiplicative, |
| 3013 | "operator%"}, |
| 3014 | {"rs", OperatorInfo::Binary, false, Node::Prec::Shift, "operator>>"}, |
| 3015 | {"sc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, "static_cast"}, |
| 3016 | {"ss", OperatorInfo::Binary, false, Node::Prec::Spaceship, "operator<=>"}, |
| 3017 | {"st", OperatorInfo::OfIdOp, /*Type*/ true, Node::Prec::Unary, "sizeof "}, |
| 3018 | {"sz", OperatorInfo::OfIdOp, /*Type*/ false, Node::Prec::Unary, "sizeof "}, |
| 3019 | {"te", OperatorInfo::OfIdOp, /*Type*/ false, Node::Prec::Postfix, |
| 3020 | "typeid "}, |
| 3021 | {"ti", OperatorInfo::OfIdOp, /*Type*/ true, Node::Prec::Postfix, "typeid "}, |
| 3022 | }; |
| 3023 | template <typename Derived, typename Alloc> |
| 3024 | const size_t AbstractManglingParser<Derived, Alloc>::NumOps = sizeof(Ops) / |
| 3025 | sizeof(Ops[0]); |
| 3026 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 3027 | // If the next 2 chars are an operator encoding, consume them and return their |
| 3028 | // OperatorInfo. Otherwise return nullptr. |
| 3029 | template <typename Derived, typename Alloc> |
| 3030 | const typename AbstractManglingParser<Derived, Alloc>::OperatorInfo * |
| 3031 | AbstractManglingParser<Derived, Alloc>::parseOperatorEncoding() { |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 3032 | if (numLeft() < 2) |
| 3033 | return nullptr; |
| 3034 | |
| 3035 | auto Op = std::lower_bound( |
| 3036 | &Ops[0], &Ops[NumOps], First, |
| 3037 | [](const OperatorInfo &Op_, const char *Enc_) { return Op_ < Enc_; }); |
| 3038 | if (Op == &Ops[NumOps] || *Op != First) |
| 3039 | return nullptr; |
| 3040 | |
| 3041 | First += 2; |
| 3042 | return Op; |
| 3043 | } |
| 3044 | |
| 3045 | // <operator-name> ::= See parseOperatorEncoding() |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3046 | // ::= li <source-name> # operator "" |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 3047 | // ::= v <digit> <source-name> # vendor extended operator |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3048 | template <typename Derived, typename Alloc> |
| 3049 | Node * |
| 3050 | AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) { |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 3051 | if (const auto *Op = parseOperatorEncoding()) { |
| 3052 | if (Op->getKind() == OperatorInfo::CCast) { |
| 3053 | // ::= cv <type> # (cast) |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 3054 | ScopedOverride<bool> SaveTemplate(TryToParseTemplateArgs, false); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3055 | // If we're parsing an encoding, State != nullptr and the conversion |
| 3056 | // operators' <type> could have a <template-param> that refers to some |
| 3057 | // <template-arg>s further ahead in the mangled name. |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 3058 | ScopedOverride<bool> SavePermit(PermitForwardTemplateReferences, |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3059 | PermitForwardTemplateReferences || |
| 3060 | State != nullptr); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3061 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3062 | if (Ty == nullptr) |
| 3063 | return nullptr; |
| 3064 | if (State) State->CtorDtorConversion = true; |
| 3065 | return make<ConversionOperatorType>(Ty); |
| 3066 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 3067 | |
| 3068 | if (Op->getKind() >= OperatorInfo::Unnameable) |
| 3069 | /* Not a nameable operator. */ |
| 3070 | return nullptr; |
| 3071 | if (Op->getKind() == OperatorInfo::Member && !Op->getFlag()) |
| 3072 | /* Not a nameable MemberExpr */ |
| 3073 | return nullptr; |
| 3074 | |
| 3075 | return make<NameType>(Op->getName()); |
| 3076 | } |
| 3077 | |
| 3078 | if (consumeIf("li")) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3079 | // ::= li <source-name> # operator "" |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 3080 | Node *SN = getDerived().parseSourceName(State); |
| 3081 | if (SN == nullptr) |
| 3082 | return nullptr; |
| 3083 | return make<LiteralOperator>(SN); |
| 3084 | } |
| 3085 | |
| 3086 | if (consumeIf('v')) { |
| 3087 | // ::= v <digit> <source-name> # vendor extended operator |
| 3088 | if (look() >= '0' && look() <= '9') { |
| 3089 | First++; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3090 | Node *SN = getDerived().parseSourceName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3091 | if (SN == nullptr) |
| 3092 | return nullptr; |
| 3093 | return make<ConversionOperatorType>(SN); |
| 3094 | } |
| 3095 | return nullptr; |
| 3096 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 3097 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3098 | return nullptr; |
| 3099 | } |
| 3100 | |
| 3101 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 3102 | // ::= C2 # base object constructor |
| 3103 | // ::= C3 # complete object allocating constructor |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3104 | // extension ::= C4 # gcc old-style "[unified]" constructor |
| 3105 | // extension ::= C5 # the COMDAT used for ctors |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3106 | // ::= D0 # deleting destructor |
| 3107 | // ::= D1 # complete object destructor |
| 3108 | // ::= D2 # base object destructor |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3109 | // extension ::= D4 # gcc old-style "[unified]" destructor |
| 3110 | // extension ::= D5 # the COMDAT used for dtors |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3111 | template <typename Derived, typename Alloc> |
| 3112 | Node * |
| 3113 | AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar, |
| 3114 | NameState *State) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3115 | if (SoFar->getKind() == Node::KSpecialSubstitution) { |
Nathan Sidwell | 2820149 | 2022-03-28 12:55:45 -0700 | [diff] [blame] | 3116 | // Expand the special substitution. |
| 3117 | SoFar = make<ExpandedSpecialSubstitution>( |
| 3118 | static_cast<SpecialSubstitution *>(SoFar)); |
| 3119 | if (!SoFar) |
| 3120 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | if (consumeIf('C')) { |
| 3124 | bool IsInherited = consumeIf('I'); |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3125 | if (look() != '1' && look() != '2' && look() != '3' && look() != '4' && |
| 3126 | look() != '5') |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3127 | return nullptr; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3128 | int Variant = look() - '0'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3129 | ++First; |
| 3130 | if (State) State->CtorDtorConversion = true; |
| 3131 | if (IsInherited) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3132 | if (getDerived().parseName(State) == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3133 | return nullptr; |
| 3134 | } |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3135 | return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3138 | if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' || |
| 3139 | look(1) == '4' || look(1) == '5')) { |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3140 | int Variant = look(1) - '0'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3141 | First += 2; |
| 3142 | if (State) State->CtorDtorConversion = true; |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3143 | return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | return nullptr; |
| 3147 | } |
| 3148 | |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 3149 | // <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> |
| 3150 | // <unqualified-name> E |
| 3151 | // ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> |
| 3152 | // <template-args> E |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3153 | // |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 3154 | // <prefix> ::= <prefix> <unqualified-name> |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3155 | // ::= <template-prefix> <template-args> |
| 3156 | // ::= <template-param> |
| 3157 | // ::= <decltype> |
| 3158 | // ::= # empty |
| 3159 | // ::= <substitution> |
| 3160 | // ::= <prefix> <data-member-prefix> |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3161 | // [*] extension |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3162 | // |
| 3163 | // <data-member-prefix> := <member source-name> [<template-args>] M |
| 3164 | // |
| 3165 | // <template-prefix> ::= <prefix> <template unqualified-name> |
| 3166 | // ::= <template-param> |
| 3167 | // ::= <substitution> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3168 | template <typename Derived, typename Alloc> |
| 3169 | Node * |
| 3170 | AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3171 | if (!consumeIf('N')) |
| 3172 | return nullptr; |
| 3173 | |
| 3174 | Qualifiers CVTmp = parseCVQualifiers(); |
| 3175 | if (State) State->CVQualifiers = CVTmp; |
| 3176 | |
| 3177 | if (consumeIf('O')) { |
| 3178 | if (State) State->ReferenceQualifier = FrefQualRValue; |
| 3179 | } else if (consumeIf('R')) { |
| 3180 | if (State) State->ReferenceQualifier = FrefQualLValue; |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3181 | } else { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3182 | if (State) State->ReferenceQualifier = FrefQualNone; |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3183 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3184 | |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3185 | Node *SoFar = nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3186 | while (!consumeIf('E')) { |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3187 | if (State) |
| 3188 | // Only set end-with-template on the case that does that. |
| 3189 | State->EndsWithTemplateArgs = false; |
| 3190 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3191 | if (look() == 'T') { |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3192 | // ::= <template-param> |
| 3193 | if (SoFar != nullptr) |
| 3194 | return nullptr; // Cannot have a prefix. |
| 3195 | SoFar = getDerived().parseTemplateParam(); |
| 3196 | } else if (look() == 'I') { |
| 3197 | // ::= <template-prefix> <template-args> |
| 3198 | if (SoFar == nullptr) |
| 3199 | return nullptr; // Must have a prefix. |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3200 | Node *TA = getDerived().parseTemplateArgs(State != nullptr); |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3201 | if (TA == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3202 | return nullptr; |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3203 | if (SoFar->getKind() == Node::KNameWithTemplateArgs) |
| 3204 | // Semantically <template-args> <template-args> cannot be generated by a |
| 3205 | // C++ entity. There will always be [something like] a name between |
| 3206 | // them. |
| 3207 | return nullptr; |
| 3208 | if (State) |
| 3209 | State->EndsWithTemplateArgs = true; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3210 | SoFar = make<NameWithTemplateArgs>(SoFar, TA); |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3211 | } else if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) { |
| 3212 | // ::= <decltype> |
| 3213 | if (SoFar != nullptr) |
| 3214 | return nullptr; // Cannot have a prefix. |
| 3215 | SoFar = getDerived().parseDecltype(); |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3216 | } else { |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 3217 | ModuleName *Module = nullptr; |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 3218 | |
| 3219 | if (look() == 'S') { |
| 3220 | // ::= <substitution> |
| 3221 | Node *S = nullptr; |
| 3222 | if (look(1) == 't') { |
| 3223 | First += 2; |
| 3224 | S = make<NameType>("std"); |
| 3225 | } else { |
| 3226 | S = getDerived().parseSubstitution(); |
| 3227 | } |
| 3228 | if (!S) |
| 3229 | return nullptr; |
| 3230 | if (S->getKind() == Node::KModuleName) { |
| 3231 | Module = static_cast<ModuleName *>(S); |
Nathan Sidwell | ac492da | 2022-04-05 09:25:47 -0700 | [diff] [blame] | 3232 | } else if (SoFar != nullptr) { |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 3233 | return nullptr; // Cannot have a prefix. |
| 3234 | } else { |
| 3235 | SoFar = S; |
| 3236 | continue; // Do not push a new substitution. |
| 3237 | } |
| 3238 | } |
| 3239 | |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 3240 | // ::= [<prefix>] <unqualified-name> |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 3241 | SoFar = getDerived().parseUnqualifiedName(State, SoFar, Module); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3242 | } |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 3243 | |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3244 | if (SoFar == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3245 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3246 | Subs.push_back(SoFar); |
Nathan Sidwell | e654529 | 2022-01-25 12:31:01 -0800 | [diff] [blame] | 3247 | |
| 3248 | // No longer used. |
| 3249 | // <data-member-prefix> := <member source-name> [<template-args>] M |
| 3250 | consumeIf('M'); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | if (SoFar == nullptr || Subs.empty()) |
| 3254 | return nullptr; |
| 3255 | |
| 3256 | Subs.pop_back(); |
| 3257 | return SoFar; |
| 3258 | } |
| 3259 | |
| 3260 | // <simple-id> ::= <source-name> [ <template-args> ] |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3261 | template <typename Derived, typename Alloc> |
| 3262 | Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() { |
| 3263 | Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3264 | if (SN == nullptr) |
| 3265 | return nullptr; |
| 3266 | if (look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3267 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3268 | if (TA == nullptr) |
| 3269 | return nullptr; |
| 3270 | return make<NameWithTemplateArgs>(SN, TA); |
| 3271 | } |
| 3272 | return SN; |
| 3273 | } |
| 3274 | |
| 3275 | // <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f()) |
| 3276 | // ::= <simple-id> # e.g., ~A<2*N> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3277 | template <typename Derived, typename Alloc> |
| 3278 | Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3279 | Node *Result; |
| 3280 | if (std::isdigit(look())) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3281 | Result = getDerived().parseSimpleId(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3282 | else |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3283 | Result = getDerived().parseUnresolvedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3284 | if (Result == nullptr) |
| 3285 | return nullptr; |
| 3286 | return make<DtorName>(Result); |
| 3287 | } |
| 3288 | |
| 3289 | // <unresolved-type> ::= <template-param> |
| 3290 | // ::= <decltype> |
| 3291 | // ::= <substitution> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3292 | template <typename Derived, typename Alloc> |
| 3293 | Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3294 | if (look() == 'T') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3295 | Node *TP = getDerived().parseTemplateParam(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3296 | if (TP == nullptr) |
| 3297 | return nullptr; |
| 3298 | Subs.push_back(TP); |
| 3299 | return TP; |
| 3300 | } |
| 3301 | if (look() == 'D') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3302 | Node *DT = getDerived().parseDecltype(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3303 | if (DT == nullptr) |
| 3304 | return nullptr; |
| 3305 | Subs.push_back(DT); |
| 3306 | return DT; |
| 3307 | } |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3308 | return getDerived().parseSubstitution(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | // <base-unresolved-name> ::= <simple-id> # unresolved name |
| 3312 | // extension ::= <operator-name> # unresolved operator-function-id |
| 3313 | // extension ::= <operator-name> <template-args> # unresolved operator template-id |
| 3314 | // ::= on <operator-name> # unresolved operator-function-id |
| 3315 | // ::= on <operator-name> <template-args> # unresolved operator template-id |
| 3316 | // ::= dn <destructor-name> # destructor or pseudo-destructor; |
| 3317 | // # e.g. ~X or ~X<N-1> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3318 | template <typename Derived, typename Alloc> |
| 3319 | Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3320 | if (std::isdigit(look())) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3321 | return getDerived().parseSimpleId(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3322 | |
| 3323 | if (consumeIf("dn")) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3324 | return getDerived().parseDestructorName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3325 | |
| 3326 | consumeIf("on"); |
| 3327 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3328 | Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3329 | if (Oper == nullptr) |
| 3330 | return nullptr; |
| 3331 | if (look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3332 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3333 | if (TA == nullptr) |
| 3334 | return nullptr; |
| 3335 | return make<NameWithTemplateArgs>(Oper, TA); |
| 3336 | } |
| 3337 | return Oper; |
| 3338 | } |
| 3339 | |
| 3340 | // <unresolved-name> |
| 3341 | // extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name> |
| 3342 | // ::= [gs] <base-unresolved-name> # x or (with "gs") ::x |
| 3343 | // ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name> |
| 3344 | // # A::x, N::y, A<T>::z; "gs" means leading "::" |
Nathan Sidwell | 77c52e2 | 2022-01-28 11:59:03 -0800 | [diff] [blame] | 3345 | // [gs] has been parsed by caller. |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3346 | // ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x |
| 3347 | // extension ::= sr <unresolved-type> <template-args> <base-unresolved-name> |
| 3348 | // # T::N::x /decltype(p)::N::x |
| 3349 | // (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name> |
| 3350 | // |
| 3351 | // <unresolved-qualifier-level> ::= <simple-id> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3352 | template <typename Derived, typename Alloc> |
Nathan Sidwell | 77c52e2 | 2022-01-28 11:59:03 -0800 | [diff] [blame] | 3353 | Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3354 | Node *SoFar = nullptr; |
| 3355 | |
| 3356 | // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name> |
| 3357 | // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name> |
| 3358 | if (consumeIf("srN")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3359 | SoFar = getDerived().parseUnresolvedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3360 | if (SoFar == nullptr) |
| 3361 | return nullptr; |
| 3362 | |
| 3363 | if (look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3364 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3365 | if (TA == nullptr) |
| 3366 | return nullptr; |
| 3367 | SoFar = make<NameWithTemplateArgs>(SoFar, TA); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3368 | if (!SoFar) |
| 3369 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | while (!consumeIf('E')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3373 | Node *Qual = getDerived().parseSimpleId(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3374 | if (Qual == nullptr) |
| 3375 | return nullptr; |
| 3376 | SoFar = make<QualifiedName>(SoFar, Qual); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3377 | if (!SoFar) |
| 3378 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3381 | Node *Base = getDerived().parseBaseUnresolvedName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3382 | if (Base == nullptr) |
| 3383 | return nullptr; |
| 3384 | return make<QualifiedName>(SoFar, Base); |
| 3385 | } |
| 3386 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3387 | // [gs] <base-unresolved-name> # x or (with "gs") ::x |
| 3388 | if (!consumeIf("sr")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3389 | SoFar = getDerived().parseBaseUnresolvedName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3390 | if (SoFar == nullptr) |
| 3391 | return nullptr; |
| 3392 | if (Global) |
| 3393 | SoFar = make<GlobalQualifiedName>(SoFar); |
| 3394 | return SoFar; |
| 3395 | } |
| 3396 | |
| 3397 | // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name> |
| 3398 | if (std::isdigit(look())) { |
| 3399 | do { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3400 | Node *Qual = getDerived().parseSimpleId(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3401 | if (Qual == nullptr) |
| 3402 | return nullptr; |
| 3403 | if (SoFar) |
| 3404 | SoFar = make<QualifiedName>(SoFar, Qual); |
| 3405 | else if (Global) |
| 3406 | SoFar = make<GlobalQualifiedName>(Qual); |
| 3407 | else |
| 3408 | SoFar = Qual; |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3409 | if (!SoFar) |
| 3410 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3411 | } while (!consumeIf('E')); |
| 3412 | } |
| 3413 | // sr <unresolved-type> <base-unresolved-name> |
| 3414 | // sr <unresolved-type> <template-args> <base-unresolved-name> |
| 3415 | else { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3416 | SoFar = getDerived().parseUnresolvedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3417 | if (SoFar == nullptr) |
| 3418 | return nullptr; |
| 3419 | |
| 3420 | if (look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3421 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3422 | if (TA == nullptr) |
| 3423 | return nullptr; |
| 3424 | SoFar = make<NameWithTemplateArgs>(SoFar, TA); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3425 | if (!SoFar) |
| 3426 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3427 | } |
| 3428 | } |
| 3429 | |
| 3430 | assert(SoFar != nullptr); |
| 3431 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3432 | Node *Base = getDerived().parseBaseUnresolvedName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3433 | if (Base == nullptr) |
| 3434 | return nullptr; |
| 3435 | return make<QualifiedName>(SoFar, Base); |
| 3436 | } |
| 3437 | |
| 3438 | // <abi-tags> ::= <abi-tag> [<abi-tags>] |
| 3439 | // <abi-tag> ::= B <source-name> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3440 | template <typename Derived, typename Alloc> |
| 3441 | Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3442 | while (consumeIf('B')) { |
| 3443 | StringView SN = parseBareSourceName(); |
| 3444 | if (SN.empty()) |
| 3445 | return nullptr; |
| 3446 | N = make<AbiTagAttr>(N, SN); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3447 | if (!N) |
| 3448 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3449 | } |
| 3450 | return N; |
| 3451 | } |
| 3452 | |
| 3453 | // <number> ::= [n] <non-negative decimal integer> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3454 | template <typename Alloc, typename Derived> |
| 3455 | StringView |
| 3456 | AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3457 | const char *Tmp = First; |
| 3458 | if (AllowNegative) |
| 3459 | consumeIf('n'); |
| 3460 | if (numLeft() == 0 || !std::isdigit(*First)) |
| 3461 | return StringView(); |
| 3462 | while (numLeft() != 0 && std::isdigit(*First)) |
| 3463 | ++First; |
| 3464 | return StringView(Tmp, First); |
| 3465 | } |
| 3466 | |
| 3467 | // <positive length number> ::= [0-9]* |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3468 | template <typename Alloc, typename Derived> |
| 3469 | bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3470 | *Out = 0; |
| 3471 | if (look() < '0' || look() > '9') |
| 3472 | return true; |
| 3473 | while (look() >= '0' && look() <= '9') { |
| 3474 | *Out *= 10; |
| 3475 | *Out += static_cast<size_t>(consume() - '0'); |
| 3476 | } |
| 3477 | return false; |
| 3478 | } |
| 3479 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3480 | template <typename Alloc, typename Derived> |
| 3481 | StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3482 | size_t Int = 0; |
| 3483 | if (parsePositiveInteger(&Int) || numLeft() < Int) |
| 3484 | return StringView(); |
| 3485 | StringView R(First, First + Int); |
| 3486 | First += Int; |
| 3487 | return R; |
| 3488 | } |
| 3489 | |
| 3490 | // <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E |
| 3491 | // |
| 3492 | // <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw()) |
| 3493 | // ::= DO <expression> E # computed (instantiation-dependent) noexcept |
| 3494 | // ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types |
| 3495 | // |
| 3496 | // <ref-qualifier> ::= R # & ref-qualifier |
| 3497 | // <ref-qualifier> ::= O # && ref-qualifier |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3498 | template <typename Derived, typename Alloc> |
| 3499 | Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3500 | Qualifiers CVQuals = parseCVQualifiers(); |
| 3501 | |
| 3502 | Node *ExceptionSpec = nullptr; |
| 3503 | if (consumeIf("Do")) { |
| 3504 | ExceptionSpec = make<NameType>("noexcept"); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3505 | if (!ExceptionSpec) |
| 3506 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3507 | } else if (consumeIf("DO")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3508 | Node *E = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3509 | if (E == nullptr || !consumeIf('E')) |
| 3510 | return nullptr; |
| 3511 | ExceptionSpec = make<NoexceptSpec>(E); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3512 | if (!ExceptionSpec) |
| 3513 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3514 | } else if (consumeIf("Dw")) { |
| 3515 | size_t SpecsBegin = Names.size(); |
| 3516 | while (!consumeIf('E')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3517 | Node *T = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3518 | if (T == nullptr) |
| 3519 | return nullptr; |
| 3520 | Names.push_back(T); |
| 3521 | } |
| 3522 | ExceptionSpec = |
| 3523 | make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin)); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3524 | if (!ExceptionSpec) |
| 3525 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | consumeIf("Dx"); // transaction safe |
| 3529 | |
| 3530 | if (!consumeIf('F')) |
| 3531 | return nullptr; |
| 3532 | consumeIf('Y'); // extern "C" |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3533 | Node *ReturnType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3534 | if (ReturnType == nullptr) |
| 3535 | return nullptr; |
| 3536 | |
| 3537 | FunctionRefQual ReferenceQualifier = FrefQualNone; |
| 3538 | size_t ParamsBegin = Names.size(); |
| 3539 | while (true) { |
| 3540 | if (consumeIf('E')) |
| 3541 | break; |
| 3542 | if (consumeIf('v')) |
| 3543 | continue; |
| 3544 | if (consumeIf("RE")) { |
| 3545 | ReferenceQualifier = FrefQualLValue; |
| 3546 | break; |
| 3547 | } |
| 3548 | if (consumeIf("OE")) { |
| 3549 | ReferenceQualifier = FrefQualRValue; |
| 3550 | break; |
| 3551 | } |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3552 | Node *T = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3553 | if (T == nullptr) |
| 3554 | return nullptr; |
| 3555 | Names.push_back(T); |
| 3556 | } |
| 3557 | |
| 3558 | NodeArray Params = popTrailingNodeArray(ParamsBegin); |
| 3559 | return make<FunctionType>(ReturnType, Params, CVQuals, |
| 3560 | ReferenceQualifier, ExceptionSpec); |
| 3561 | } |
| 3562 | |
| 3563 | // extension: |
| 3564 | // <vector-type> ::= Dv <positive dimension number> _ <extended element type> |
| 3565 | // ::= Dv [<dimension expression>] _ <element type> |
| 3566 | // <extended element type> ::= <element type> |
| 3567 | // ::= p # AltiVec vector pixel |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3568 | template <typename Derived, typename Alloc> |
| 3569 | Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3570 | if (!consumeIf("Dv")) |
| 3571 | return nullptr; |
| 3572 | if (look() >= '1' && look() <= '9') { |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 3573 | Node *DimensionNumber = make<NameType>(parseNumber()); |
| 3574 | if (!DimensionNumber) |
| 3575 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3576 | if (!consumeIf('_')) |
| 3577 | return nullptr; |
| 3578 | if (consumeIf('p')) |
| 3579 | return make<PixelVectorType>(DimensionNumber); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3580 | Node *ElemType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3581 | if (ElemType == nullptr) |
| 3582 | return nullptr; |
| 3583 | return make<VectorType>(ElemType, DimensionNumber); |
| 3584 | } |
| 3585 | |
| 3586 | if (!consumeIf('_')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3587 | Node *DimExpr = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3588 | if (!DimExpr) |
| 3589 | return nullptr; |
| 3590 | if (!consumeIf('_')) |
| 3591 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3592 | Node *ElemType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3593 | if (!ElemType) |
| 3594 | return nullptr; |
| 3595 | return make<VectorType>(ElemType, DimExpr); |
| 3596 | } |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3597 | Node *ElemType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3598 | if (!ElemType) |
| 3599 | return nullptr; |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 3600 | return make<VectorType>(ElemType, /*Dimension=*/nullptr); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | // <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x) |
| 3604 | // ::= DT <expression> E # decltype of an expression (C++0x) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3605 | template <typename Derived, typename Alloc> |
| 3606 | Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3607 | if (!consumeIf('D')) |
| 3608 | return nullptr; |
| 3609 | if (!consumeIf('t') && !consumeIf('T')) |
| 3610 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3611 | Node *E = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3612 | if (E == nullptr) |
| 3613 | return nullptr; |
| 3614 | if (!consumeIf('E')) |
| 3615 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 3616 | return make<EnclosingExpr>("decltype", E); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 3620 | // ::= A [<dimension expression>] _ <element type> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3621 | template <typename Derived, typename Alloc> |
| 3622 | Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3623 | if (!consumeIf('A')) |
| 3624 | return nullptr; |
| 3625 | |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 3626 | Node *Dimension = nullptr; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3627 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3628 | if (std::isdigit(look())) { |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 3629 | Dimension = make<NameType>(parseNumber()); |
| 3630 | if (!Dimension) |
| 3631 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3632 | if (!consumeIf('_')) |
| 3633 | return nullptr; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3634 | } else if (!consumeIf('_')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3635 | Node *DimExpr = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3636 | if (DimExpr == nullptr) |
| 3637 | return nullptr; |
| 3638 | if (!consumeIf('_')) |
| 3639 | return nullptr; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3640 | Dimension = DimExpr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3643 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3644 | if (Ty == nullptr) |
| 3645 | return nullptr; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3646 | return make<ArrayType>(Ty, Dimension); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | // <pointer-to-member-type> ::= M <class type> <member type> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3650 | template <typename Derived, typename Alloc> |
| 3651 | Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3652 | if (!consumeIf('M')) |
| 3653 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3654 | Node *ClassType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3655 | if (ClassType == nullptr) |
| 3656 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3657 | Node *MemberType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3658 | if (MemberType == nullptr) |
| 3659 | return nullptr; |
| 3660 | return make<PointerToMemberType>(ClassType, MemberType); |
| 3661 | } |
| 3662 | |
| 3663 | // <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier |
| 3664 | // ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class' |
| 3665 | // ::= Tu <name> # dependent elaborated type specifier using 'union' |
| 3666 | // ::= Te <name> # dependent elaborated type specifier using 'enum' |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3667 | template <typename Derived, typename Alloc> |
| 3668 | Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3669 | StringView ElabSpef; |
| 3670 | if (consumeIf("Ts")) |
| 3671 | ElabSpef = "struct"; |
| 3672 | else if (consumeIf("Tu")) |
| 3673 | ElabSpef = "union"; |
| 3674 | else if (consumeIf("Te")) |
| 3675 | ElabSpef = "enum"; |
| 3676 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3677 | Node *Name = getDerived().parseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3678 | if (Name == nullptr) |
| 3679 | return nullptr; |
| 3680 | |
| 3681 | if (!ElabSpef.empty()) |
| 3682 | return make<ElaboratedTypeSpefType>(ElabSpef, Name); |
| 3683 | |
| 3684 | return Name; |
| 3685 | } |
| 3686 | |
| 3687 | // <qualified-type> ::= <qualifiers> <type> |
| 3688 | // <qualifiers> ::= <extended-qualifier>* <CV-qualifiers> |
| 3689 | // <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3690 | template <typename Derived, typename Alloc> |
| 3691 | Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3692 | if (consumeIf('U')) { |
| 3693 | StringView Qual = parseBareSourceName(); |
| 3694 | if (Qual.empty()) |
| 3695 | return nullptr; |
| 3696 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3697 | // extension ::= U <objc-name> <objc-type> # objc-type<identifier> |
| 3698 | if (Qual.startsWith("objcproto")) { |
| 3699 | StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto")); |
| 3700 | StringView Proto; |
| 3701 | { |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 3702 | ScopedOverride<const char *> SaveFirst(First, ProtoSourceName.begin()), |
| 3703 | SaveLast(Last, ProtoSourceName.end()); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3704 | Proto = parseBareSourceName(); |
| 3705 | } |
| 3706 | if (Proto.empty()) |
| 3707 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3708 | Node *Child = getDerived().parseQualifiedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3709 | if (Child == nullptr) |
| 3710 | return nullptr; |
| 3711 | return make<ObjCProtoName>(Child, Proto); |
| 3712 | } |
| 3713 | |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 3714 | Node *TA = nullptr; |
| 3715 | if (look() == 'I') { |
| 3716 | TA = getDerived().parseTemplateArgs(); |
| 3717 | if (TA == nullptr) |
| 3718 | return nullptr; |
| 3719 | } |
| 3720 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3721 | Node *Child = getDerived().parseQualifiedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3722 | if (Child == nullptr) |
| 3723 | return nullptr; |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 3724 | return make<VendorExtQualType>(Child, Qual, TA); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | Qualifiers Quals = parseCVQualifiers(); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3728 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3729 | if (Ty == nullptr) |
| 3730 | return nullptr; |
| 3731 | if (Quals != QualNone) |
| 3732 | Ty = make<QualType>(Ty, Quals); |
| 3733 | return Ty; |
| 3734 | } |
| 3735 | |
| 3736 | // <type> ::= <builtin-type> |
| 3737 | // ::= <qualified-type> |
| 3738 | // ::= <function-type> |
| 3739 | // ::= <class-enum-type> |
| 3740 | // ::= <array-type> |
| 3741 | // ::= <pointer-to-member-type> |
| 3742 | // ::= <template-param> |
| 3743 | // ::= <template-template-param> <template-args> |
| 3744 | // ::= <decltype> |
| 3745 | // ::= P <type> # pointer |
| 3746 | // ::= R <type> # l-value reference |
| 3747 | // ::= O <type> # r-value reference (C++11) |
| 3748 | // ::= C <type> # complex pair (C99) |
| 3749 | // ::= G <type> # imaginary (C99) |
| 3750 | // ::= <substitution> # See Compression below |
| 3751 | // extension ::= U <objc-name> <objc-type> # objc-type<identifier> |
| 3752 | // extension ::= <vector-type> # <vector-type> starts with Dv |
| 3753 | // |
| 3754 | // <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1 |
| 3755 | // <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3756 | template <typename Derived, typename Alloc> |
| 3757 | Node *AbstractManglingParser<Derived, Alloc>::parseType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3758 | Node *Result = nullptr; |
| 3759 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3760 | switch (look()) { |
| 3761 | // ::= <qualified-type> |
| 3762 | case 'r': |
| 3763 | case 'V': |
| 3764 | case 'K': { |
| 3765 | unsigned AfterQuals = 0; |
| 3766 | if (look(AfterQuals) == 'r') ++AfterQuals; |
| 3767 | if (look(AfterQuals) == 'V') ++AfterQuals; |
| 3768 | if (look(AfterQuals) == 'K') ++AfterQuals; |
| 3769 | |
| 3770 | if (look(AfterQuals) == 'F' || |
| 3771 | (look(AfterQuals) == 'D' && |
| 3772 | (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' || |
| 3773 | look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3774 | Result = getDerived().parseFunctionType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3775 | break; |
| 3776 | } |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 3777 | DEMANGLE_FALLTHROUGH; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3778 | } |
| 3779 | case 'U': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3780 | Result = getDerived().parseQualifiedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3781 | break; |
| 3782 | } |
| 3783 | // <builtin-type> ::= v # void |
| 3784 | case 'v': |
| 3785 | ++First; |
| 3786 | return make<NameType>("void"); |
| 3787 | // ::= w # wchar_t |
| 3788 | case 'w': |
| 3789 | ++First; |
| 3790 | return make<NameType>("wchar_t"); |
| 3791 | // ::= b # bool |
| 3792 | case 'b': |
| 3793 | ++First; |
| 3794 | return make<NameType>("bool"); |
| 3795 | // ::= c # char |
| 3796 | case 'c': |
| 3797 | ++First; |
| 3798 | return make<NameType>("char"); |
| 3799 | // ::= a # signed char |
| 3800 | case 'a': |
| 3801 | ++First; |
| 3802 | return make<NameType>("signed char"); |
| 3803 | // ::= h # unsigned char |
| 3804 | case 'h': |
| 3805 | ++First; |
| 3806 | return make<NameType>("unsigned char"); |
| 3807 | // ::= s # short |
| 3808 | case 's': |
| 3809 | ++First; |
| 3810 | return make<NameType>("short"); |
| 3811 | // ::= t # unsigned short |
| 3812 | case 't': |
| 3813 | ++First; |
| 3814 | return make<NameType>("unsigned short"); |
| 3815 | // ::= i # int |
| 3816 | case 'i': |
| 3817 | ++First; |
| 3818 | return make<NameType>("int"); |
| 3819 | // ::= j # unsigned int |
| 3820 | case 'j': |
| 3821 | ++First; |
| 3822 | return make<NameType>("unsigned int"); |
| 3823 | // ::= l # long |
| 3824 | case 'l': |
| 3825 | ++First; |
| 3826 | return make<NameType>("long"); |
| 3827 | // ::= m # unsigned long |
| 3828 | case 'm': |
| 3829 | ++First; |
| 3830 | return make<NameType>("unsigned long"); |
| 3831 | // ::= x # long long, __int64 |
| 3832 | case 'x': |
| 3833 | ++First; |
| 3834 | return make<NameType>("long long"); |
| 3835 | // ::= y # unsigned long long, __int64 |
| 3836 | case 'y': |
| 3837 | ++First; |
| 3838 | return make<NameType>("unsigned long long"); |
| 3839 | // ::= n # __int128 |
| 3840 | case 'n': |
| 3841 | ++First; |
| 3842 | return make<NameType>("__int128"); |
| 3843 | // ::= o # unsigned __int128 |
| 3844 | case 'o': |
| 3845 | ++First; |
| 3846 | return make<NameType>("unsigned __int128"); |
| 3847 | // ::= f # float |
| 3848 | case 'f': |
| 3849 | ++First; |
| 3850 | return make<NameType>("float"); |
| 3851 | // ::= d # double |
| 3852 | case 'd': |
| 3853 | ++First; |
| 3854 | return make<NameType>("double"); |
| 3855 | // ::= e # long double, __float80 |
| 3856 | case 'e': |
| 3857 | ++First; |
| 3858 | return make<NameType>("long double"); |
| 3859 | // ::= g # __float128 |
| 3860 | case 'g': |
| 3861 | ++First; |
| 3862 | return make<NameType>("__float128"); |
| 3863 | // ::= z # ellipsis |
| 3864 | case 'z': |
| 3865 | ++First; |
| 3866 | return make<NameType>("..."); |
| 3867 | |
| 3868 | // <builtin-type> ::= u <source-name> # vendor extended type |
| 3869 | case 'u': { |
| 3870 | ++First; |
| 3871 | StringView Res = parseBareSourceName(); |
| 3872 | if (Res.empty()) |
| 3873 | return nullptr; |
Erik Pilkington | b94a1f4 | 2019-06-10 21:02:39 +0000 | [diff] [blame] | 3874 | // Typically, <builtin-type>s are not considered substitution candidates, |
| 3875 | // but the exception to that exception is vendor extended types (Itanium C++ |
| 3876 | // ABI 5.9.1). |
| 3877 | Result = make<NameType>(Res); |
| 3878 | break; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3879 | } |
| 3880 | case 'D': |
| 3881 | switch (look(1)) { |
| 3882 | // ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 3883 | case 'd': |
| 3884 | First += 2; |
| 3885 | return make<NameType>("decimal64"); |
| 3886 | // ::= De # IEEE 754r decimal floating point (128 bits) |
| 3887 | case 'e': |
| 3888 | First += 2; |
| 3889 | return make<NameType>("decimal128"); |
| 3890 | // ::= Df # IEEE 754r decimal floating point (32 bits) |
| 3891 | case 'f': |
| 3892 | First += 2; |
| 3893 | return make<NameType>("decimal32"); |
| 3894 | // ::= Dh # IEEE 754r half-precision floating point (16 bits) |
| 3895 | case 'h': |
| 3896 | First += 2; |
Stuart Brady | e8bf577 | 2021-06-07 16:30:22 +0100 | [diff] [blame] | 3897 | return make<NameType>("half"); |
Pengfei Wang | 50e90b8 | 2021-09-23 11:02:25 +0800 | [diff] [blame] | 3898 | // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits) |
| 3899 | case 'F': { |
| 3900 | First += 2; |
| 3901 | Node *DimensionNumber = make<NameType>(parseNumber()); |
| 3902 | if (!DimensionNumber) |
| 3903 | return nullptr; |
| 3904 | if (!consumeIf('_')) |
| 3905 | return nullptr; |
| 3906 | return make<BinaryFPType>(DimensionNumber); |
| 3907 | } |
Senran Zhang | e025ba5 | 2022-03-27 00:04:23 +0800 | [diff] [blame] | 3908 | // ::= DB <number> _ # C23 signed _BitInt(N) |
| 3909 | // ::= DB <instantiation-dependent expression> _ # C23 signed _BitInt(N) |
| 3910 | // ::= DU <number> _ # C23 unsigned _BitInt(N) |
| 3911 | // ::= DU <instantiation-dependent expression> _ # C23 unsigned _BitInt(N) |
| 3912 | case 'B': |
| 3913 | case 'U': { |
| 3914 | bool Signed = look(1) == 'B'; |
| 3915 | First += 2; |
| 3916 | Node *Size = std::isdigit(look()) ? make<NameType>(parseNumber()) |
| 3917 | : getDerived().parseExpr(); |
| 3918 | if (!Size) |
| 3919 | return nullptr; |
| 3920 | if (!consumeIf('_')) |
| 3921 | return nullptr; |
| 3922 | return make<BitIntType>(Size, Signed); |
| 3923 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3924 | // ::= Di # char32_t |
| 3925 | case 'i': |
| 3926 | First += 2; |
| 3927 | return make<NameType>("char32_t"); |
| 3928 | // ::= Ds # char16_t |
| 3929 | case 's': |
| 3930 | First += 2; |
| 3931 | return make<NameType>("char16_t"); |
Erik Pilkington | c3780e8 | 2019-06-28 19:54:19 +0000 | [diff] [blame] | 3932 | // ::= Du # char8_t (C++2a, not yet in the Itanium spec) |
| 3933 | case 'u': |
| 3934 | First += 2; |
| 3935 | return make<NameType>("char8_t"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3936 | // ::= Da # auto (in dependent new-expressions) |
| 3937 | case 'a': |
| 3938 | First += 2; |
| 3939 | return make<NameType>("auto"); |
| 3940 | // ::= Dc # decltype(auto) |
| 3941 | case 'c': |
| 3942 | First += 2; |
| 3943 | return make<NameType>("decltype(auto)"); |
| 3944 | // ::= Dn # std::nullptr_t (i.e., decltype(nullptr)) |
| 3945 | case 'n': |
| 3946 | First += 2; |
| 3947 | return make<NameType>("std::nullptr_t"); |
| 3948 | |
| 3949 | // ::= <decltype> |
| 3950 | case 't': |
| 3951 | case 'T': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3952 | Result = getDerived().parseDecltype(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3953 | break; |
| 3954 | } |
| 3955 | // extension ::= <vector-type> # <vector-type> starts with Dv |
| 3956 | case 'v': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3957 | Result = getDerived().parseVectorType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3958 | break; |
| 3959 | } |
| 3960 | // ::= Dp <type> # pack expansion (C++0x) |
| 3961 | case 'p': { |
| 3962 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3963 | Node *Child = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3964 | if (!Child) |
| 3965 | return nullptr; |
| 3966 | Result = make<ParameterPackExpansion>(Child); |
| 3967 | break; |
| 3968 | } |
| 3969 | // Exception specifier on a function type. |
| 3970 | case 'o': |
| 3971 | case 'O': |
| 3972 | case 'w': |
| 3973 | // Transaction safe function type. |
| 3974 | case 'x': |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3975 | Result = getDerived().parseFunctionType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3976 | break; |
| 3977 | } |
| 3978 | break; |
| 3979 | // ::= <function-type> |
| 3980 | case 'F': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3981 | Result = getDerived().parseFunctionType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3982 | break; |
| 3983 | } |
| 3984 | // ::= <array-type> |
| 3985 | case 'A': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3986 | Result = getDerived().parseArrayType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3987 | break; |
| 3988 | } |
| 3989 | // ::= <pointer-to-member-type> |
| 3990 | case 'M': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3991 | Result = getDerived().parsePointerToMemberType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3992 | break; |
| 3993 | } |
| 3994 | // ::= <template-param> |
| 3995 | case 'T': { |
| 3996 | // This could be an elaborate type specifier on a <class-enum-type>. |
| 3997 | if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3998 | Result = getDerived().parseClassEnumType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3999 | break; |
| 4000 | } |
| 4001 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4002 | Result = getDerived().parseTemplateParam(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4003 | if (Result == nullptr) |
| 4004 | return nullptr; |
| 4005 | |
| 4006 | // Result could be either of: |
| 4007 | // <type> ::= <template-param> |
| 4008 | // <type> ::= <template-template-param> <template-args> |
| 4009 | // |
| 4010 | // <template-template-param> ::= <template-param> |
| 4011 | // ::= <substitution> |
| 4012 | // |
| 4013 | // If this is followed by some <template-args>, and we're permitted to |
| 4014 | // parse them, take the second production. |
| 4015 | |
| 4016 | if (TryToParseTemplateArgs && look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4017 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4018 | if (TA == nullptr) |
| 4019 | return nullptr; |
| 4020 | Result = make<NameWithTemplateArgs>(Result, TA); |
| 4021 | } |
| 4022 | break; |
| 4023 | } |
| 4024 | // ::= P <type> # pointer |
| 4025 | case 'P': { |
| 4026 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4027 | Node *Ptr = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4028 | if (Ptr == nullptr) |
| 4029 | return nullptr; |
| 4030 | Result = make<PointerType>(Ptr); |
| 4031 | break; |
| 4032 | } |
| 4033 | // ::= R <type> # l-value reference |
| 4034 | case 'R': { |
| 4035 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4036 | Node *Ref = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4037 | if (Ref == nullptr) |
| 4038 | return nullptr; |
| 4039 | Result = make<ReferenceType>(Ref, ReferenceKind::LValue); |
| 4040 | break; |
| 4041 | } |
| 4042 | // ::= O <type> # r-value reference (C++11) |
| 4043 | case 'O': { |
| 4044 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4045 | Node *Ref = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4046 | if (Ref == nullptr) |
| 4047 | return nullptr; |
| 4048 | Result = make<ReferenceType>(Ref, ReferenceKind::RValue); |
| 4049 | break; |
| 4050 | } |
| 4051 | // ::= C <type> # complex pair (C99) |
| 4052 | case 'C': { |
| 4053 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4054 | Node *P = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4055 | if (P == nullptr) |
| 4056 | return nullptr; |
| 4057 | Result = make<PostfixQualifiedType>(P, " complex"); |
| 4058 | break; |
| 4059 | } |
| 4060 | // ::= G <type> # imaginary (C99) |
| 4061 | case 'G': { |
| 4062 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4063 | Node *P = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4064 | if (P == nullptr) |
| 4065 | return P; |
| 4066 | Result = make<PostfixQualifiedType>(P, " imaginary"); |
| 4067 | break; |
| 4068 | } |
| 4069 | // ::= <substitution> # See Compression below |
| 4070 | case 'S': { |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 4071 | if (look(1) != 't') { |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 4072 | bool IsSubst = false; |
| 4073 | Result = getDerived().parseUnscopedName(nullptr, &IsSubst); |
| 4074 | if (!Result) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4075 | return nullptr; |
| 4076 | |
| 4077 | // Sub could be either of: |
| 4078 | // <type> ::= <substitution> |
| 4079 | // <type> ::= <template-template-param> <template-args> |
| 4080 | // |
| 4081 | // <template-template-param> ::= <template-param> |
| 4082 | // ::= <substitution> |
| 4083 | // |
| 4084 | // If this is followed by some <template-args>, and we're permitted to |
| 4085 | // parse them, take the second production. |
| 4086 | |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 4087 | if (look() == 'I' && (!IsSubst || TryToParseTemplateArgs)) { |
| 4088 | if (!IsSubst) |
| 4089 | Subs.push_back(Result); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4090 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4091 | if (TA == nullptr) |
| 4092 | return nullptr; |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 4093 | Result = make<NameWithTemplateArgs>(Result, TA); |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 4094 | } else if (IsSubst) { |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 4095 | // If all we parsed was a substitution, don't re-insert into the |
| 4096 | // substitution table. |
| 4097 | return Result; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4098 | } |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 4099 | break; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4100 | } |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 4101 | DEMANGLE_FALLTHROUGH; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4102 | } |
| 4103 | // ::= <class-enum-type> |
| 4104 | default: { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4105 | Result = getDerived().parseClassEnumType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4106 | break; |
| 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | // If we parsed a type, insert it into the substitution table. Note that all |
| 4111 | // <builtin-type>s and <substitution>s have already bailed out, because they |
| 4112 | // don't get substitutions. |
| 4113 | if (Result != nullptr) |
| 4114 | Subs.push_back(Result); |
| 4115 | return Result; |
| 4116 | } |
| 4117 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4118 | template <typename Derived, typename Alloc> |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4119 | Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind, |
| 4120 | Node::Prec Prec) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4121 | Node *E = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4122 | if (E == nullptr) |
| 4123 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4124 | return make<PrefixExpr>(Kind, E, Prec); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4127 | template <typename Derived, typename Alloc> |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4128 | Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind, |
| 4129 | Node::Prec Prec) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4130 | Node *LHS = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4131 | if (LHS == nullptr) |
| 4132 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4133 | Node *RHS = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4134 | if (RHS == nullptr) |
| 4135 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4136 | return make<BinaryExpr>(LHS, Kind, RHS, Prec); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4139 | template <typename Derived, typename Alloc> |
| 4140 | Node * |
| 4141 | AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4142 | StringView Tmp = parseNumber(true); |
| 4143 | if (!Tmp.empty() && consumeIf('E')) |
| 4144 | return make<IntegerLiteral>(Lit, Tmp); |
| 4145 | return nullptr; |
| 4146 | } |
| 4147 | |
| 4148 | // <CV-Qualifiers> ::= [r] [V] [K] |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4149 | template <typename Alloc, typename Derived> |
| 4150 | Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4151 | Qualifiers CVR = QualNone; |
| 4152 | if (consumeIf('r')) |
| 4153 | CVR |= QualRestrict; |
| 4154 | if (consumeIf('V')) |
| 4155 | CVR |= QualVolatile; |
| 4156 | if (consumeIf('K')) |
| 4157 | CVR |= QualConst; |
| 4158 | return CVR; |
| 4159 | } |
| 4160 | |
| 4161 | // <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter |
| 4162 | // ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters |
| 4163 | // ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter |
| 4164 | // ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters |
Erik Pilkington | 91c24af | 2020-05-13 22:19:45 -0400 | [diff] [blame] | 4165 | // ::= fpT # 'this' expression (not part of standard?) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4166 | template <typename Derived, typename Alloc> |
| 4167 | Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() { |
Erik Pilkington | 91c24af | 2020-05-13 22:19:45 -0400 | [diff] [blame] | 4168 | if (consumeIf("fpT")) |
| 4169 | return make<NameType>("this"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4170 | if (consumeIf("fp")) { |
| 4171 | parseCVQualifiers(); |
| 4172 | StringView Num = parseNumber(); |
| 4173 | if (!consumeIf('_')) |
| 4174 | return nullptr; |
| 4175 | return make<FunctionParam>(Num); |
| 4176 | } |
| 4177 | if (consumeIf("fL")) { |
| 4178 | if (parseNumber().empty()) |
| 4179 | return nullptr; |
| 4180 | if (!consumeIf('p')) |
| 4181 | return nullptr; |
| 4182 | parseCVQualifiers(); |
| 4183 | StringView Num = parseNumber(); |
| 4184 | if (!consumeIf('_')) |
| 4185 | return nullptr; |
| 4186 | return make<FunctionParam>(Num); |
| 4187 | } |
| 4188 | return nullptr; |
| 4189 | } |
| 4190 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4191 | // cv <type> <expression> # conversion with one argument |
| 4192 | // cv <type> _ <expression>* E # conversion with a different number of arguments |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4193 | template <typename Derived, typename Alloc> |
| 4194 | Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4195 | if (!consumeIf("cv")) |
| 4196 | return nullptr; |
| 4197 | Node *Ty; |
| 4198 | { |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 4199 | ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4200 | Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | if (Ty == nullptr) |
| 4204 | return nullptr; |
| 4205 | |
| 4206 | if (consumeIf('_')) { |
| 4207 | size_t ExprsBegin = Names.size(); |
| 4208 | while (!consumeIf('E')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4209 | Node *E = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4210 | if (E == nullptr) |
| 4211 | return E; |
| 4212 | Names.push_back(E); |
| 4213 | } |
| 4214 | NodeArray Exprs = popTrailingNodeArray(ExprsBegin); |
| 4215 | return make<ConversionExpr>(Ty, Exprs); |
| 4216 | } |
| 4217 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4218 | Node *E[1] = {getDerived().parseExpr()}; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4219 | if (E[0] == nullptr) |
| 4220 | return nullptr; |
| 4221 | return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1)); |
| 4222 | } |
| 4223 | |
| 4224 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 4225 | // ::= L <type> <value float> E # floating literal |
| 4226 | // ::= L <string type> E # string literal |
| 4227 | // ::= L <nullptr type> E # nullptr literal (i.e., "LDnE") |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 4228 | // ::= L <lambda type> E # lambda expression |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4229 | // FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000) |
| 4230 | // ::= L <mangled-name> E # external name |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4231 | template <typename Derived, typename Alloc> |
| 4232 | Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4233 | if (!consumeIf('L')) |
| 4234 | return nullptr; |
| 4235 | switch (look()) { |
| 4236 | case 'w': |
| 4237 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4238 | return getDerived().parseIntegerLiteral("wchar_t"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4239 | case 'b': |
| 4240 | if (consumeIf("b0E")) |
| 4241 | return make<BoolExpr>(0); |
| 4242 | if (consumeIf("b1E")) |
| 4243 | return make<BoolExpr>(1); |
| 4244 | return nullptr; |
| 4245 | case 'c': |
| 4246 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4247 | return getDerived().parseIntegerLiteral("char"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4248 | case 'a': |
| 4249 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4250 | return getDerived().parseIntegerLiteral("signed char"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4251 | case 'h': |
| 4252 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4253 | return getDerived().parseIntegerLiteral("unsigned char"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4254 | case 's': |
| 4255 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4256 | return getDerived().parseIntegerLiteral("short"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4257 | case 't': |
| 4258 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4259 | return getDerived().parseIntegerLiteral("unsigned short"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4260 | case 'i': |
| 4261 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4262 | return getDerived().parseIntegerLiteral(""); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4263 | case 'j': |
| 4264 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4265 | return getDerived().parseIntegerLiteral("u"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4266 | case 'l': |
| 4267 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4268 | return getDerived().parseIntegerLiteral("l"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4269 | case 'm': |
| 4270 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4271 | return getDerived().parseIntegerLiteral("ul"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4272 | case 'x': |
| 4273 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4274 | return getDerived().parseIntegerLiteral("ll"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4275 | case 'y': |
| 4276 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4277 | return getDerived().parseIntegerLiteral("ull"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4278 | case 'n': |
| 4279 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4280 | return getDerived().parseIntegerLiteral("__int128"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4281 | case 'o': |
| 4282 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4283 | return getDerived().parseIntegerLiteral("unsigned __int128"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4284 | case 'f': |
| 4285 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4286 | return getDerived().template parseFloatingLiteral<float>(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4287 | case 'd': |
| 4288 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4289 | return getDerived().template parseFloatingLiteral<double>(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4290 | case 'e': |
| 4291 | ++First; |
Xing Xue | 3dc5e08 | 2020-04-15 09:59:06 -0400 | [diff] [blame] | 4292 | #if defined(__powerpc__) || defined(__s390__) |
| 4293 | // Handle cases where long doubles encoded with e have the same size |
| 4294 | // and representation as doubles. |
| 4295 | return getDerived().template parseFloatingLiteral<double>(); |
| 4296 | #else |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4297 | return getDerived().template parseFloatingLiteral<long double>(); |
Xing Xue | 3dc5e08 | 2020-04-15 09:59:06 -0400 | [diff] [blame] | 4298 | #endif |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4299 | case '_': |
| 4300 | if (consumeIf("_Z")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4301 | Node *R = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4302 | if (R != nullptr && consumeIf('E')) |
| 4303 | return R; |
| 4304 | } |
| 4305 | return nullptr; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 4306 | case 'A': { |
| 4307 | Node *T = getDerived().parseType(); |
| 4308 | if (T == nullptr) |
| 4309 | return nullptr; |
| 4310 | // FIXME: We need to include the string contents in the mangling. |
| 4311 | if (consumeIf('E')) |
| 4312 | return make<StringLiteral>(T); |
| 4313 | return nullptr; |
| 4314 | } |
| 4315 | case 'D': |
gbreynoo | c055932 | 2022-04-28 15:55:26 +0100 | [diff] [blame] | 4316 | if (consumeIf("Dn") && (consumeIf('0'), consumeIf('E'))) |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 4317 | return make<NameType>("nullptr"); |
| 4318 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4319 | case 'T': |
| 4320 | // Invalid mangled name per |
| 4321 | // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html |
| 4322 | return nullptr; |
Richard Smith | fb91746 | 2019-09-09 22:26:04 +0000 | [diff] [blame] | 4323 | case 'U': { |
| 4324 | // FIXME: Should we support LUb... for block literals? |
| 4325 | if (look(1) != 'l') |
| 4326 | return nullptr; |
| 4327 | Node *T = parseUnnamedTypeName(nullptr); |
| 4328 | if (!T || !consumeIf('E')) |
| 4329 | return nullptr; |
| 4330 | return make<LambdaExpr>(T); |
| 4331 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4332 | default: { |
| 4333 | // might be named type |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4334 | Node *T = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4335 | if (T == nullptr) |
| 4336 | return nullptr; |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 4337 | StringView N = parseNumber(/*AllowNegative=*/true); |
Richard Smith | fb91746 | 2019-09-09 22:26:04 +0000 | [diff] [blame] | 4338 | if (N.empty()) |
| 4339 | return nullptr; |
| 4340 | if (!consumeIf('E')) |
| 4341 | return nullptr; |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 4342 | return make<EnumLiteral>(T, N); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4343 | } |
| 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | // <braced-expression> ::= <expression> |
| 4348 | // ::= di <field source-name> <braced-expression> # .name = expr |
| 4349 | // ::= dx <index expression> <braced-expression> # [expr] = expr |
| 4350 | // ::= dX <range begin expression> <range end expression> <braced-expression> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4351 | template <typename Derived, typename Alloc> |
| 4352 | Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4353 | if (look() == 'd') { |
| 4354 | switch (look(1)) { |
| 4355 | case 'i': { |
| 4356 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4357 | Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4358 | if (Field == nullptr) |
| 4359 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4360 | Node *Init = getDerived().parseBracedExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4361 | if (Init == nullptr) |
| 4362 | return nullptr; |
| 4363 | return make<BracedExpr>(Field, Init, /*isArray=*/false); |
| 4364 | } |
| 4365 | case 'x': { |
| 4366 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4367 | Node *Index = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4368 | if (Index == nullptr) |
| 4369 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4370 | Node *Init = getDerived().parseBracedExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4371 | if (Init == nullptr) |
| 4372 | return nullptr; |
| 4373 | return make<BracedExpr>(Index, Init, /*isArray=*/true); |
| 4374 | } |
| 4375 | case 'X': { |
| 4376 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4377 | Node *RangeBegin = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4378 | if (RangeBegin == nullptr) |
| 4379 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4380 | Node *RangeEnd = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4381 | if (RangeEnd == nullptr) |
| 4382 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4383 | Node *Init = getDerived().parseBracedExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4384 | if (Init == nullptr) |
| 4385 | return nullptr; |
| 4386 | return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init); |
| 4387 | } |
| 4388 | } |
| 4389 | } |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4390 | return getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | // (not yet in the spec) |
| 4394 | // <fold-expr> ::= fL <binary-operator-name> <expression> <expression> |
| 4395 | // ::= fR <binary-operator-name> <expression> <expression> |
| 4396 | // ::= fl <binary-operator-name> <expression> |
| 4397 | // ::= fr <binary-operator-name> <expression> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4398 | template <typename Derived, typename Alloc> |
| 4399 | Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4400 | if (!consumeIf('f')) |
| 4401 | return nullptr; |
| 4402 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4403 | bool IsLeftFold = false, HasInitializer = false; |
| 4404 | switch (look()) { |
| 4405 | default: |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4406 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4407 | case 'L': |
| 4408 | IsLeftFold = true; |
| 4409 | HasInitializer = true; |
| 4410 | break; |
| 4411 | case 'R': |
| 4412 | HasInitializer = true; |
| 4413 | break; |
| 4414 | case 'l': |
| 4415 | IsLeftFold = true; |
| 4416 | break; |
| 4417 | case 'r': |
| 4418 | break; |
| 4419 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4420 | ++First; |
| 4421 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4422 | const auto *Op = parseOperatorEncoding(); |
Nathan Sidwell | ce96725 | 2022-02-18 11:06:58 -0800 | [diff] [blame] | 4423 | if (!Op) |
| 4424 | return nullptr; |
| 4425 | if (!(Op->getKind() == OperatorInfo::Binary |
| 4426 | || (Op->getKind() == OperatorInfo::Member |
| 4427 | && Op->getName().back() == '*'))) |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4428 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4429 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4430 | Node *Pack = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4431 | if (Pack == nullptr) |
| 4432 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4433 | |
| 4434 | Node *Init = nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4435 | if (HasInitializer) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4436 | Init = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4437 | if (Init == nullptr) |
| 4438 | return nullptr; |
| 4439 | } |
| 4440 | |
| 4441 | if (IsLeftFold && Init) |
| 4442 | std::swap(Pack, Init); |
| 4443 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4444 | return make<FoldExpr>(IsLeftFold, Op->getSymbol(), Pack, Init); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 4447 | // <expression> ::= mc <parameter type> <expr> [<offset number>] E |
| 4448 | // |
| 4449 | // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47 |
| 4450 | template <typename Derived, typename Alloc> |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4451 | Node * |
| 4452 | AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr( |
| 4453 | Node::Prec Prec) { |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 4454 | Node *Ty = getDerived().parseType(); |
| 4455 | if (!Ty) |
| 4456 | return nullptr; |
| 4457 | Node *Expr = getDerived().parseExpr(); |
| 4458 | if (!Expr) |
| 4459 | return nullptr; |
| 4460 | StringView Offset = getDerived().parseNumber(true); |
| 4461 | if (!consumeIf('E')) |
| 4462 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4463 | return make<PointerToMemberConversionExpr>(Ty, Expr, Offset, Prec); |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | // <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E |
| 4467 | // <union-selector> ::= _ [<number>] |
| 4468 | // |
| 4469 | // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47 |
| 4470 | template <typename Derived, typename Alloc> |
| 4471 | Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() { |
| 4472 | Node *Ty = getDerived().parseType(); |
| 4473 | if (!Ty) |
| 4474 | return nullptr; |
| 4475 | Node *Expr = getDerived().parseExpr(); |
| 4476 | if (!Expr) |
| 4477 | return nullptr; |
| 4478 | StringView Offset = getDerived().parseNumber(true); |
| 4479 | size_t SelectorsBegin = Names.size(); |
| 4480 | while (consumeIf('_')) { |
| 4481 | Node *Selector = make<NameType>(parseNumber()); |
| 4482 | if (!Selector) |
| 4483 | return nullptr; |
| 4484 | Names.push_back(Selector); |
| 4485 | } |
| 4486 | bool OnePastTheEnd = consumeIf('p'); |
| 4487 | if (!consumeIf('E')) |
| 4488 | return nullptr; |
| 4489 | return make<SubobjectExpr>( |
| 4490 | Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd); |
| 4491 | } |
| 4492 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4493 | // <expression> ::= <unary operator-name> <expression> |
| 4494 | // ::= <binary operator-name> <expression> <expression> |
| 4495 | // ::= <ternary operator-name> <expression> <expression> <expression> |
| 4496 | // ::= cl <expression>+ E # call |
| 4497 | // ::= cv <type> <expression> # conversion with one argument |
| 4498 | // ::= cv <type> _ <expression>* E # conversion with a different number of arguments |
| 4499 | // ::= [gs] nw <expression>* _ <type> E # new (expr-list) type |
| 4500 | // ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init) |
| 4501 | // ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type |
| 4502 | // ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init) |
| 4503 | // ::= [gs] dl <expression> # delete expression |
| 4504 | // ::= [gs] da <expression> # delete[] expression |
| 4505 | // ::= pp_ <expression> # prefix ++ |
| 4506 | // ::= mm_ <expression> # prefix -- |
| 4507 | // ::= ti <type> # typeid (type) |
| 4508 | // ::= te <expression> # typeid (expression) |
| 4509 | // ::= dc <type> <expression> # dynamic_cast<type> (expression) |
| 4510 | // ::= sc <type> <expression> # static_cast<type> (expression) |
| 4511 | // ::= cc <type> <expression> # const_cast<type> (expression) |
| 4512 | // ::= rc <type> <expression> # reinterpret_cast<type> (expression) |
| 4513 | // ::= st <type> # sizeof (a type) |
| 4514 | // ::= sz <expression> # sizeof (an expression) |
| 4515 | // ::= at <type> # alignof (a type) |
| 4516 | // ::= az <expression> # alignof (an expression) |
| 4517 | // ::= nx <expression> # noexcept (expression) |
| 4518 | // ::= <template-param> |
| 4519 | // ::= <function-param> |
| 4520 | // ::= dt <expression> <unresolved-name> # expr.name |
| 4521 | // ::= pt <expression> <unresolved-name> # expr->name |
| 4522 | // ::= ds <expression> <expression> # expr.*expr |
| 4523 | // ::= sZ <template-param> # size of a parameter pack |
| 4524 | // ::= sZ <function-param> # size of a function parameter pack |
| 4525 | // ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template |
| 4526 | // ::= sp <expression> # pack expansion |
| 4527 | // ::= tw <expression> # throw expression |
| 4528 | // ::= tr # throw with no operand (rethrow) |
| 4529 | // ::= <unresolved-name> # f(p), N::f(p), ::f(p), |
| 4530 | // # freestanding dependent name (e.g., T::x), |
| 4531 | // # objectless nonstatic member reference |
| 4532 | // ::= fL <binary-operator-name> <expression> <expression> |
| 4533 | // ::= fR <binary-operator-name> <expression> <expression> |
| 4534 | // ::= fl <binary-operator-name> <expression> |
| 4535 | // ::= fr <binary-operator-name> <expression> |
| 4536 | // ::= <expr-primary> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4537 | template <typename Derived, typename Alloc> |
| 4538 | Node *AbstractManglingParser<Derived, Alloc>::parseExpr() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4539 | bool Global = consumeIf("gs"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4540 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4541 | const auto *Op = parseOperatorEncoding(); |
| 4542 | if (Op) { |
| 4543 | auto Sym = Op->getSymbol(); |
| 4544 | switch (Op->getKind()) { |
| 4545 | case OperatorInfo::Binary: |
| 4546 | // Binary operator: lhs @ rhs |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4547 | return getDerived().parseBinaryExpr(Sym, Op->getPrecedence()); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4548 | case OperatorInfo::Prefix: |
| 4549 | // Prefix unary operator: @ expr |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4550 | return getDerived().parsePrefixExpr(Sym, Op->getPrecedence()); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4551 | case OperatorInfo::Postfix: { |
| 4552 | // Postfix unary operator: expr @ |
| 4553 | if (consumeIf('_')) |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4554 | return getDerived().parsePrefixExpr(Sym, Op->getPrecedence()); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4555 | Node *Ex = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4556 | if (Ex == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4557 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4558 | return make<PostfixExpr>(Ex, Sym, Op->getPrecedence()); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4559 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4560 | case OperatorInfo::Array: { |
| 4561 | // Array Index: lhs [ rhs ] |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4562 | Node *Base = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4563 | if (Base == nullptr) |
| 4564 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4565 | Node *Index = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4566 | if (Index == nullptr) |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4567 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4568 | return make<ArraySubscriptExpr>(Base, Index, Op->getPrecedence()); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4569 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4570 | case OperatorInfo::Member: { |
| 4571 | // Member access lhs @ rhs |
| 4572 | Node *LHS = getDerived().parseExpr(); |
| 4573 | if (LHS == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4574 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4575 | Node *RHS = getDerived().parseExpr(); |
| 4576 | if (RHS == nullptr) |
| 4577 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4578 | return make<MemberExpr>(LHS, Sym, RHS, Op->getPrecedence()); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4579 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4580 | case OperatorInfo::New: { |
| 4581 | // New |
| 4582 | // # new (expr-list) type [(init)] |
| 4583 | // [gs] nw <expression>* _ <type> [pi <expression>*] E |
| 4584 | // # new[] (expr-list) type [(init)] |
| 4585 | // [gs] na <expression>* _ <type> [pi <expression>*] E |
Nathan Sidwell | c69bde2 | 2022-01-28 07:09:38 -0800 | [diff] [blame] | 4586 | size_t Exprs = Names.size(); |
| 4587 | while (!consumeIf('_')) { |
| 4588 | Node *Ex = getDerived().parseExpr(); |
| 4589 | if (Ex == nullptr) |
| 4590 | return nullptr; |
| 4591 | Names.push_back(Ex); |
| 4592 | } |
| 4593 | NodeArray ExprList = popTrailingNodeArray(Exprs); |
| 4594 | Node *Ty = getDerived().parseType(); |
| 4595 | if (Ty == nullptr) |
| 4596 | return nullptr; |
| 4597 | bool HaveInits = consumeIf("pi"); |
| 4598 | size_t InitsBegin = Names.size(); |
| 4599 | while (!consumeIf('E')) { |
| 4600 | if (!HaveInits) |
| 4601 | return nullptr; |
| 4602 | Node *Init = getDerived().parseExpr(); |
| 4603 | if (Init == nullptr) |
| 4604 | return Init; |
| 4605 | Names.push_back(Init); |
| 4606 | } |
| 4607 | NodeArray Inits = popTrailingNodeArray(InitsBegin); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4608 | return make<NewExpr>(ExprList, Ty, Inits, Global, |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4609 | /*IsArray=*/Op->getFlag(), Op->getPrecedence()); |
Nathan Sidwell | c69bde2 | 2022-01-28 07:09:38 -0800 | [diff] [blame] | 4610 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4611 | case OperatorInfo::Del: { |
| 4612 | // Delete |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4613 | Node *Ex = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4614 | if (Ex == nullptr) |
Nathan Sidwell | c648304 | 2022-01-28 09:27:28 -0800 | [diff] [blame] | 4615 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4616 | return make<DeleteExpr>(Ex, Global, /*IsArray=*/Op->getFlag(), |
| 4617 | Op->getPrecedence()); |
Nathan Sidwell | c648304 | 2022-01-28 09:27:28 -0800 | [diff] [blame] | 4618 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4619 | case OperatorInfo::Call: { |
| 4620 | // Function Call |
| 4621 | Node *Callee = getDerived().parseExpr(); |
| 4622 | if (Callee == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4623 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4624 | size_t ExprsBegin = Names.size(); |
| 4625 | while (!consumeIf('E')) { |
| 4626 | Node *E = getDerived().parseExpr(); |
| 4627 | if (E == nullptr) |
| 4628 | return nullptr; |
| 4629 | Names.push_back(E); |
| 4630 | } |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4631 | return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin), |
| 4632 | Op->getPrecedence()); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4633 | } |
| 4634 | case OperatorInfo::CCast: { |
| 4635 | // C Cast: (type)expr |
| 4636 | Node *Ty; |
| 4637 | { |
Nathan Sidwell | f6358c4 | 2022-02-28 10:18:14 -0800 | [diff] [blame] | 4638 | ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4639 | Ty = getDerived().parseType(); |
| 4640 | } |
| 4641 | if (Ty == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4642 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4643 | |
| 4644 | size_t ExprsBegin = Names.size(); |
| 4645 | bool IsMany = consumeIf('_'); |
| 4646 | while (!consumeIf('E')) { |
| 4647 | Node *E = getDerived().parseExpr(); |
| 4648 | if (E == nullptr) |
| 4649 | return E; |
| 4650 | Names.push_back(E); |
| 4651 | if (!IsMany) |
| 4652 | break; |
| 4653 | } |
| 4654 | NodeArray Exprs = popTrailingNodeArray(ExprsBegin); |
| 4655 | if (!IsMany && Exprs.size() != 1) |
| 4656 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4657 | return make<ConversionExpr>(Ty, Exprs, Op->getPrecedence()); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4658 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4659 | case OperatorInfo::Conditional: { |
| 4660 | // Conditional operator: expr ? expr : expr |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4661 | Node *Cond = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4662 | if (Cond == nullptr) |
| 4663 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4664 | Node *LHS = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4665 | if (LHS == nullptr) |
| 4666 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4667 | Node *RHS = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4668 | if (RHS == nullptr) |
| 4669 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4670 | return make<ConditionalExpr>(Cond, LHS, RHS, Op->getPrecedence()); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4671 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4672 | case OperatorInfo::NamedCast: { |
| 4673 | // Named cast operation, @<type>(expr) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4674 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4675 | if (Ty == nullptr) |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4676 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4677 | Node *Ex = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4678 | if (Ex == nullptr) |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4679 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4680 | return make<CastExpr>(Sym, Ty, Ex, Op->getPrecedence()); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4681 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4682 | case OperatorInfo::OfIdOp: { |
| 4683 | // [sizeof/alignof/typeid] ( <type>|<expr> ) |
| 4684 | Node *Arg = |
| 4685 | Op->getFlag() ? getDerived().parseType() : getDerived().parseExpr(); |
| 4686 | if (!Arg) |
| 4687 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4688 | return make<EnclosingExpr>(Sym, Arg, Op->getPrecedence()); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4689 | } |
Nathan Sidwell | 0dda3d4 | 2022-02-18 09:51:24 -0800 | [diff] [blame] | 4690 | case OperatorInfo::NameOnly: { |
| 4691 | // Not valid as an expression operand. |
| 4692 | return nullptr; |
| 4693 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4694 | } |
| 4695 | DEMANGLE_UNREACHABLE; |
| 4696 | } |
| 4697 | |
| 4698 | if (numLeft() < 2) |
| 4699 | return nullptr; |
| 4700 | |
| 4701 | if (look() == 'L') |
| 4702 | return getDerived().parseExprPrimary(); |
| 4703 | if (look() == 'T') |
| 4704 | return getDerived().parseTemplateParam(); |
| 4705 | if (look() == 'f') { |
| 4706 | // Disambiguate a fold expression from a <function-param>. |
| 4707 | if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2)))) |
| 4708 | return getDerived().parseFunctionParam(); |
| 4709 | return getDerived().parseFoldExpr(); |
| 4710 | } |
| 4711 | if (consumeIf("il")) { |
| 4712 | size_t InitsBegin = Names.size(); |
| 4713 | while (!consumeIf('E')) { |
| 4714 | Node *E = getDerived().parseBracedExpr(); |
| 4715 | if (E == nullptr) |
| 4716 | return nullptr; |
| 4717 | Names.push_back(E); |
| 4718 | } |
| 4719 | return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin)); |
| 4720 | } |
| 4721 | if (consumeIf("mc")) |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4722 | return parsePointerToMemberConversionExpr(Node::Prec::Unary); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4723 | if (consumeIf("nx")) { |
| 4724 | Node *Ex = getDerived().parseExpr(); |
| 4725 | if (Ex == nullptr) |
| 4726 | return Ex; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4727 | return make<EnclosingExpr>("noexcept ", Ex, Node::Prec::Unary); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4728 | } |
| 4729 | if (consumeIf("so")) |
| 4730 | return parseSubobjectExpr(); |
| 4731 | if (consumeIf("sp")) { |
| 4732 | Node *Child = getDerived().parseExpr(); |
| 4733 | if (Child == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4734 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4735 | return make<ParameterPackExpansion>(Child); |
| 4736 | } |
| 4737 | if (consumeIf("sZ")) { |
| 4738 | if (look() == 'T') { |
| 4739 | Node *R = getDerived().parseTemplateParam(); |
| 4740 | if (R == nullptr) |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 4741 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4742 | return make<SizeofParamPackExpr>(R); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4743 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4744 | Node *FP = getDerived().parseFunctionParam(); |
| 4745 | if (FP == nullptr) |
| 4746 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4747 | return make<EnclosingExpr>("sizeof... ", FP); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4748 | } |
| 4749 | if (consumeIf("sP")) { |
| 4750 | size_t ArgsBegin = Names.size(); |
| 4751 | while (!consumeIf('E')) { |
| 4752 | Node *Arg = getDerived().parseTemplateArg(); |
| 4753 | if (Arg == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4754 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4755 | Names.push_back(Arg); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4756 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4757 | auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin)); |
| 4758 | if (!Pack) |
| 4759 | return nullptr; |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4760 | return make<EnclosingExpr>("sizeof... ", Pack); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4761 | } |
| 4762 | if (consumeIf("tl")) { |
| 4763 | Node *Ty = getDerived().parseType(); |
| 4764 | if (Ty == nullptr) |
| 4765 | return nullptr; |
| 4766 | size_t InitsBegin = Names.size(); |
| 4767 | while (!consumeIf('E')) { |
| 4768 | Node *E = getDerived().parseBracedExpr(); |
| 4769 | if (E == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4770 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4771 | Names.push_back(E); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4772 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4773 | return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin)); |
| 4774 | } |
| 4775 | if (consumeIf("tr")) |
| 4776 | return make<NameType>("throw"); |
| 4777 | if (consumeIf("tw")) { |
| 4778 | Node *Ex = getDerived().parseExpr(); |
| 4779 | if (Ex == nullptr) |
| 4780 | return nullptr; |
| 4781 | return make<ThrowExpr>(Ex); |
| 4782 | } |
| 4783 | if (consumeIf('u')) { |
James Y Knight | 4a60efc | 2020-12-07 10:26:49 -0500 | [diff] [blame] | 4784 | Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr); |
| 4785 | if (!Name) |
| 4786 | return nullptr; |
| 4787 | // Special case legacy __uuidof mangling. The 't' and 'z' appear where the |
| 4788 | // standard encoding expects a <template-arg>, and would be otherwise be |
| 4789 | // interpreted as <type> node 'short' or 'ellipsis'. However, neither |
| 4790 | // __uuidof(short) nor __uuidof(...) can actually appear, so there is no |
| 4791 | // actual conflict here. |
Nathan Sidwell | a3b5900 | 2022-02-11 05:54:40 -0800 | [diff] [blame] | 4792 | bool IsUUID = false; |
| 4793 | Node *UUID = nullptr; |
James Y Knight | 4a60efc | 2020-12-07 10:26:49 -0500 | [diff] [blame] | 4794 | if (Name->getBaseName() == "__uuidof") { |
Nathan Sidwell | a3b5900 | 2022-02-11 05:54:40 -0800 | [diff] [blame] | 4795 | if (consumeIf('t')) { |
| 4796 | UUID = getDerived().parseType(); |
| 4797 | IsUUID = true; |
| 4798 | } else if (consumeIf('z')) { |
| 4799 | UUID = getDerived().parseExpr(); |
| 4800 | IsUUID = true; |
James Y Knight | 4a60efc | 2020-12-07 10:26:49 -0500 | [diff] [blame] | 4801 | } |
| 4802 | } |
| 4803 | size_t ExprsBegin = Names.size(); |
Nathan Sidwell | a3b5900 | 2022-02-11 05:54:40 -0800 | [diff] [blame] | 4804 | if (IsUUID) { |
| 4805 | if (UUID == nullptr) |
| 4806 | return nullptr; |
| 4807 | Names.push_back(UUID); |
| 4808 | } else { |
| 4809 | while (!consumeIf('E')) { |
| 4810 | Node *E = getDerived().parseTemplateArg(); |
| 4811 | if (E == nullptr) |
| 4812 | return E; |
| 4813 | Names.push_back(E); |
| 4814 | } |
James Y Knight | 4a60efc | 2020-12-07 10:26:49 -0500 | [diff] [blame] | 4815 | } |
Nathan Sidwell | 458a94f | 2022-02-04 11:45:23 -0800 | [diff] [blame] | 4816 | return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin), |
| 4817 | Node::Prec::Postfix); |
James Y Knight | 4a60efc | 2020-12-07 10:26:49 -0500 | [diff] [blame] | 4818 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame] | 4819 | |
| 4820 | // Only unresolved names remain. |
| 4821 | return getDerived().parseUnresolvedName(Global); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | // <call-offset> ::= h <nv-offset> _ |
| 4825 | // ::= v <v-offset> _ |
| 4826 | // |
| 4827 | // <nv-offset> ::= <offset number> |
| 4828 | // # non-virtual base override |
| 4829 | // |
| 4830 | // <v-offset> ::= <offset number> _ <virtual offset number> |
| 4831 | // # virtual base override, with vcall offset |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4832 | template <typename Alloc, typename Derived> |
| 4833 | bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4834 | // Just scan through the call offset, we never add this information into the |
| 4835 | // output. |
| 4836 | if (consumeIf('h')) |
| 4837 | return parseNumber(true).empty() || !consumeIf('_'); |
| 4838 | if (consumeIf('v')) |
| 4839 | return parseNumber(true).empty() || !consumeIf('_') || |
| 4840 | parseNumber(true).empty() || !consumeIf('_'); |
| 4841 | return true; |
| 4842 | } |
| 4843 | |
| 4844 | // <special-name> ::= TV <type> # virtual table |
| 4845 | // ::= TT <type> # VTT structure (construction vtable index) |
| 4846 | // ::= TI <type> # typeinfo structure |
| 4847 | // ::= TS <type> # typeinfo name (null-terminated byte string) |
| 4848 | // ::= Tc <call-offset> <call-offset> <base encoding> |
| 4849 | // # base is the nominal target function of thunk |
| 4850 | // # first call-offset is 'this' adjustment |
| 4851 | // # second call-offset is result adjustment |
| 4852 | // ::= T <call-offset> <base encoding> |
| 4853 | // # base is the nominal target function of thunk |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 4854 | // # Guard variable for one-time initialization |
| 4855 | // ::= GV <object name> |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4856 | // # No <type> |
| 4857 | // ::= TW <object name> # Thread-local wrapper |
| 4858 | // ::= TH <object name> # Thread-local initialization |
| 4859 | // ::= GR <object name> _ # First temporary |
| 4860 | // ::= GR <object name> <seq-id> _ # Subsequent temporaries |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 4861 | // # construction vtable for second-in-first |
| 4862 | // extension ::= TC <first type> <number> _ <second type> |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4863 | // extension ::= GR <object name> # reference temporary for object |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 4864 | // extension ::= GI <module name> # module global initializer |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4865 | template <typename Derived, typename Alloc> |
| 4866 | Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4867 | switch (look()) { |
| 4868 | case 'T': |
| 4869 | switch (look(1)) { |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 4870 | // TA <template-arg> # template parameter object |
| 4871 | // |
| 4872 | // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63 |
| 4873 | case 'A': { |
| 4874 | First += 2; |
| 4875 | Node *Arg = getDerived().parseTemplateArg(); |
| 4876 | if (Arg == nullptr) |
| 4877 | return nullptr; |
| 4878 | return make<SpecialName>("template parameter object for ", Arg); |
| 4879 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4880 | // TV <type> # virtual table |
| 4881 | case 'V': { |
| 4882 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4883 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4884 | if (Ty == nullptr) |
| 4885 | return nullptr; |
| 4886 | return make<SpecialName>("vtable for ", Ty); |
| 4887 | } |
| 4888 | // TT <type> # VTT structure (construction vtable index) |
| 4889 | case 'T': { |
| 4890 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4891 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4892 | if (Ty == nullptr) |
| 4893 | return nullptr; |
| 4894 | return make<SpecialName>("VTT for ", Ty); |
| 4895 | } |
| 4896 | // TI <type> # typeinfo structure |
| 4897 | case 'I': { |
| 4898 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4899 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4900 | if (Ty == nullptr) |
| 4901 | return nullptr; |
| 4902 | return make<SpecialName>("typeinfo for ", Ty); |
| 4903 | } |
| 4904 | // TS <type> # typeinfo name (null-terminated byte string) |
| 4905 | case 'S': { |
| 4906 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4907 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4908 | if (Ty == nullptr) |
| 4909 | return nullptr; |
| 4910 | return make<SpecialName>("typeinfo name for ", Ty); |
| 4911 | } |
| 4912 | // Tc <call-offset> <call-offset> <base encoding> |
| 4913 | case 'c': { |
| 4914 | First += 2; |
| 4915 | if (parseCallOffset() || parseCallOffset()) |
| 4916 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4917 | Node *Encoding = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4918 | if (Encoding == nullptr) |
| 4919 | return nullptr; |
| 4920 | return make<SpecialName>("covariant return thunk to ", Encoding); |
| 4921 | } |
| 4922 | // extension ::= TC <first type> <number> _ <second type> |
| 4923 | // # construction vtable for second-in-first |
| 4924 | case 'C': { |
| 4925 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4926 | Node *FirstType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4927 | if (FirstType == nullptr) |
| 4928 | return nullptr; |
| 4929 | if (parseNumber(true).empty() || !consumeIf('_')) |
| 4930 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4931 | Node *SecondType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4932 | if (SecondType == nullptr) |
| 4933 | return nullptr; |
| 4934 | return make<CtorVtableSpecialName>(SecondType, FirstType); |
| 4935 | } |
| 4936 | // TW <object name> # Thread-local wrapper |
| 4937 | case 'W': { |
| 4938 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4939 | Node *Name = getDerived().parseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4940 | if (Name == nullptr) |
| 4941 | return nullptr; |
| 4942 | return make<SpecialName>("thread-local wrapper routine for ", Name); |
| 4943 | } |
| 4944 | // TH <object name> # Thread-local initialization |
| 4945 | case 'H': { |
| 4946 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4947 | Node *Name = getDerived().parseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4948 | if (Name == nullptr) |
| 4949 | return nullptr; |
| 4950 | return make<SpecialName>("thread-local initialization routine for ", Name); |
| 4951 | } |
| 4952 | // T <call-offset> <base encoding> |
| 4953 | default: { |
| 4954 | ++First; |
| 4955 | bool IsVirt = look() == 'v'; |
| 4956 | if (parseCallOffset()) |
| 4957 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4958 | Node *BaseEncoding = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4959 | if (BaseEncoding == nullptr) |
| 4960 | return nullptr; |
| 4961 | if (IsVirt) |
| 4962 | return make<SpecialName>("virtual thunk to ", BaseEncoding); |
| 4963 | else |
| 4964 | return make<SpecialName>("non-virtual thunk to ", BaseEncoding); |
| 4965 | } |
| 4966 | } |
| 4967 | case 'G': |
| 4968 | switch (look(1)) { |
| 4969 | // GV <object name> # Guard variable for one-time initialization |
| 4970 | case 'V': { |
| 4971 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4972 | Node *Name = getDerived().parseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4973 | if (Name == nullptr) |
| 4974 | return nullptr; |
| 4975 | return make<SpecialName>("guard variable for ", Name); |
| 4976 | } |
| 4977 | // GR <object name> # reference temporary for object |
| 4978 | // GR <object name> _ # First temporary |
| 4979 | // GR <object name> <seq-id> _ # Subsequent temporaries |
| 4980 | case 'R': { |
| 4981 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4982 | Node *Name = getDerived().parseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4983 | if (Name == nullptr) |
| 4984 | return nullptr; |
| 4985 | size_t Count; |
| 4986 | bool ParsedSeqId = !parseSeqId(&Count); |
| 4987 | if (!consumeIf('_') && ParsedSeqId) |
| 4988 | return nullptr; |
| 4989 | return make<SpecialName>("reference temporary for ", Name); |
| 4990 | } |
Nathan Sidwell | edde7bb | 2022-01-26 07:22:04 -0800 | [diff] [blame] | 4991 | // GI <module-name> v |
| 4992 | case 'I': { |
| 4993 | First += 2; |
| 4994 | ModuleName *Module = nullptr; |
| 4995 | if (getDerived().parseModuleNameOpt(Module)) |
| 4996 | return nullptr; |
| 4997 | if (Module == nullptr) |
| 4998 | return nullptr; |
| 4999 | return make<SpecialName>("initializer for module ", Module); |
| 5000 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5001 | } |
| 5002 | } |
| 5003 | return nullptr; |
| 5004 | } |
| 5005 | |
| 5006 | // <encoding> ::= <function name> <bare-function-type> |
| 5007 | // ::= <data name> |
| 5008 | // ::= <special-name> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5009 | template <typename Derived, typename Alloc> |
| 5010 | Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() { |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 5011 | // The template parameters of an encoding are unrelated to those of the |
| 5012 | // enclosing context. |
| 5013 | class SaveTemplateParams { |
| 5014 | AbstractManglingParser *Parser; |
| 5015 | decltype(TemplateParams) OldParams; |
Justin Lebar | 2c53623 | 2021-06-09 16:57:22 -0700 | [diff] [blame] | 5016 | decltype(OuterTemplateParams) OldOuterParams; |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 5017 | |
| 5018 | public: |
Louis Dionne | c1fe867 | 2020-10-30 17:33:02 -0400 | [diff] [blame] | 5019 | SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) { |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 5020 | OldParams = std::move(Parser->TemplateParams); |
Justin Lebar | 2c53623 | 2021-06-09 16:57:22 -0700 | [diff] [blame] | 5021 | OldOuterParams = std::move(Parser->OuterTemplateParams); |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 5022 | Parser->TemplateParams.clear(); |
Justin Lebar | 2c53623 | 2021-06-09 16:57:22 -0700 | [diff] [blame] | 5023 | Parser->OuterTemplateParams.clear(); |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 5024 | } |
| 5025 | ~SaveTemplateParams() { |
| 5026 | Parser->TemplateParams = std::move(OldParams); |
Justin Lebar | 2c53623 | 2021-06-09 16:57:22 -0700 | [diff] [blame] | 5027 | Parser->OuterTemplateParams = std::move(OldOuterParams); |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 5028 | } |
| 5029 | } SaveTemplateParams(this); |
Richard Smith | fd43432 | 2020-07-09 20:36:04 -0700 | [diff] [blame] | 5030 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5031 | if (look() == 'G' || look() == 'T') |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5032 | return getDerived().parseSpecialName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5033 | |
| 5034 | auto IsEndOfEncoding = [&] { |
| 5035 | // The set of chars that can potentially follow an <encoding> (none of which |
| 5036 | // can start a <type>). Enumerating these allows us to avoid speculative |
| 5037 | // parsing. |
| 5038 | return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_'; |
| 5039 | }; |
| 5040 | |
| 5041 | NameState NameInfo(this); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5042 | Node *Name = getDerived().parseName(&NameInfo); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5043 | if (Name == nullptr) |
| 5044 | return nullptr; |
| 5045 | |
| 5046 | if (resolveForwardTemplateRefs(NameInfo)) |
| 5047 | return nullptr; |
| 5048 | |
| 5049 | if (IsEndOfEncoding()) |
| 5050 | return Name; |
| 5051 | |
| 5052 | Node *Attrs = nullptr; |
| 5053 | if (consumeIf("Ua9enable_ifI")) { |
| 5054 | size_t BeforeArgs = Names.size(); |
| 5055 | while (!consumeIf('E')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5056 | Node *Arg = getDerived().parseTemplateArg(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5057 | if (Arg == nullptr) |
| 5058 | return nullptr; |
| 5059 | Names.push_back(Arg); |
| 5060 | } |
| 5061 | Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs)); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 5062 | if (!Attrs) |
| 5063 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
| 5066 | Node *ReturnType = nullptr; |
| 5067 | if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5068 | ReturnType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5069 | if (ReturnType == nullptr) |
| 5070 | return nullptr; |
| 5071 | } |
| 5072 | |
| 5073 | if (consumeIf('v')) |
| 5074 | return make<FunctionEncoding>(ReturnType, Name, NodeArray(), |
| 5075 | Attrs, NameInfo.CVQualifiers, |
| 5076 | NameInfo.ReferenceQualifier); |
| 5077 | |
| 5078 | size_t ParamsBegin = Names.size(); |
| 5079 | do { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5080 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5081 | if (Ty == nullptr) |
| 5082 | return nullptr; |
| 5083 | Names.push_back(Ty); |
| 5084 | } while (!IsEndOfEncoding()); |
| 5085 | |
| 5086 | return make<FunctionEncoding>(ReturnType, Name, |
| 5087 | popTrailingNodeArray(ParamsBegin), |
| 5088 | Attrs, NameInfo.CVQualifiers, |
| 5089 | NameInfo.ReferenceQualifier); |
| 5090 | } |
| 5091 | |
| 5092 | template <class Float> |
| 5093 | struct FloatData; |
| 5094 | |
| 5095 | template <> |
| 5096 | struct FloatData<float> |
| 5097 | { |
| 5098 | static const size_t mangled_size = 8; |
| 5099 | static const size_t max_demangled_size = 24; |
| 5100 | static constexpr const char* spec = "%af"; |
| 5101 | }; |
| 5102 | |
| 5103 | template <> |
| 5104 | struct FloatData<double> |
| 5105 | { |
| 5106 | static const size_t mangled_size = 16; |
| 5107 | static const size_t max_demangled_size = 32; |
| 5108 | static constexpr const char* spec = "%a"; |
| 5109 | }; |
| 5110 | |
| 5111 | template <> |
| 5112 | struct FloatData<long double> |
| 5113 | { |
| 5114 | #if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \ |
Piggy NL | 8b1770b | 2022-05-26 22:59:49 +0800 | [diff] [blame] | 5115 | defined(__wasm__) || defined(__riscv) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5116 | static const size_t mangled_size = 32; |
| 5117 | #elif defined(__arm__) || defined(__mips__) || defined(__hexagon__) |
| 5118 | static const size_t mangled_size = 16; |
| 5119 | #else |
| 5120 | static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms |
| 5121 | #endif |
Elliott Hughes | 5a360ea | 2020-04-10 17:42:00 -0700 | [diff] [blame] | 5122 | // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes. |
| 5123 | // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits. |
| 5124 | // Negatives are one character longer than positives. |
| 5125 | // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the |
| 5126 | // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128. |
| 5127 | static const size_t max_demangled_size = 42; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5128 | static constexpr const char *spec = "%LaL"; |
| 5129 | }; |
| 5130 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5131 | template <typename Alloc, typename Derived> |
| 5132 | template <class Float> |
| 5133 | Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5134 | const size_t N = FloatData<Float>::mangled_size; |
| 5135 | if (numLeft() <= N) |
| 5136 | return nullptr; |
| 5137 | StringView Data(First, First + N); |
| 5138 | for (char C : Data) |
| 5139 | if (!std::isxdigit(C)) |
| 5140 | return nullptr; |
| 5141 | First += N; |
| 5142 | if (!consumeIf('E')) |
| 5143 | return nullptr; |
| 5144 | return make<FloatLiteralImpl<Float>>(Data); |
| 5145 | } |
| 5146 | |
| 5147 | // <seq-id> ::= <0-9A-Z>+ |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5148 | template <typename Alloc, typename Derived> |
| 5149 | bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5150 | if (!(look() >= '0' && look() <= '9') && |
| 5151 | !(look() >= 'A' && look() <= 'Z')) |
| 5152 | return true; |
| 5153 | |
| 5154 | size_t Id = 0; |
| 5155 | while (true) { |
| 5156 | if (look() >= '0' && look() <= '9') { |
| 5157 | Id *= 36; |
| 5158 | Id += static_cast<size_t>(look() - '0'); |
| 5159 | } else if (look() >= 'A' && look() <= 'Z') { |
| 5160 | Id *= 36; |
| 5161 | Id += static_cast<size_t>(look() - 'A') + 10; |
| 5162 | } else { |
| 5163 | *Out = Id; |
| 5164 | return false; |
| 5165 | } |
| 5166 | ++First; |
| 5167 | } |
| 5168 | } |
| 5169 | |
| 5170 | // <substitution> ::= S <seq-id> _ |
| 5171 | // ::= S_ |
| 5172 | // <substitution> ::= Sa # ::std::allocator |
| 5173 | // <substitution> ::= Sb # ::std::basic_string |
| 5174 | // <substitution> ::= Ss # ::std::basic_string < char, |
| 5175 | // ::std::char_traits<char>, |
| 5176 | // ::std::allocator<char> > |
| 5177 | // <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> > |
| 5178 | // <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> > |
| 5179 | // <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> > |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 5180 | // The St case is handled specially in parseNestedName. |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5181 | template <typename Derived, typename Alloc> |
| 5182 | Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5183 | if (!consumeIf('S')) |
| 5184 | return nullptr; |
| 5185 | |
Nathan Sidwell | fd0ef6d | 2022-01-20 07:40:12 -0800 | [diff] [blame] | 5186 | if (look() >= 'a' && look() <= 'z') { |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5187 | SpecialSubKind Kind; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5188 | switch (look()) { |
| 5189 | case 'a': |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5190 | Kind = SpecialSubKind::allocator; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5191 | break; |
| 5192 | case 'b': |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5193 | Kind = SpecialSubKind::basic_string; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5194 | break; |
| 5195 | case 'd': |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5196 | Kind = SpecialSubKind::iostream; |
| 5197 | break; |
| 5198 | case 'i': |
| 5199 | Kind = SpecialSubKind::istream; |
| 5200 | break; |
| 5201 | case 'o': |
| 5202 | Kind = SpecialSubKind::ostream; |
| 5203 | break; |
| 5204 | case 's': |
| 5205 | Kind = SpecialSubKind::string; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5206 | break; |
| 5207 | default: |
| 5208 | return nullptr; |
| 5209 | } |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5210 | ++First; |
| 5211 | auto *SpecialSub = make<SpecialSubstitution>(Kind); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 5212 | if (!SpecialSub) |
| 5213 | return nullptr; |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5214 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5215 | // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution> |
| 5216 | // has ABI tags, the tags are appended to the substitution; the result is a |
| 5217 | // substitutable component. |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5218 | Node *WithTags = getDerived().parseAbiTags(SpecialSub); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5219 | if (WithTags != SpecialSub) { |
| 5220 | Subs.push_back(WithTags); |
| 5221 | SpecialSub = WithTags; |
| 5222 | } |
| 5223 | return SpecialSub; |
| 5224 | } |
| 5225 | |
| 5226 | // ::= S_ |
| 5227 | if (consumeIf('_')) { |
| 5228 | if (Subs.empty()) |
| 5229 | return nullptr; |
| 5230 | return Subs[0]; |
| 5231 | } |
| 5232 | |
| 5233 | // ::= S <seq-id> _ |
| 5234 | size_t Index = 0; |
| 5235 | if (parseSeqId(&Index)) |
| 5236 | return nullptr; |
| 5237 | ++Index; |
| 5238 | if (!consumeIf('_') || Index >= Subs.size()) |
| 5239 | return nullptr; |
| 5240 | return Subs[Index]; |
| 5241 | } |
| 5242 | |
| 5243 | // <template-param> ::= T_ # first template parameter |
| 5244 | // ::= T <parameter-2 non-negative number> _ |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5245 | // ::= TL <level-1> __ |
| 5246 | // ::= TL <level-1> _ <parameter-2 non-negative number> _ |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5247 | template <typename Derived, typename Alloc> |
| 5248 | Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5249 | if (!consumeIf('T')) |
| 5250 | return nullptr; |
| 5251 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5252 | size_t Level = 0; |
| 5253 | if (consumeIf('L')) { |
| 5254 | if (parsePositiveInteger(&Level)) |
| 5255 | return nullptr; |
| 5256 | ++Level; |
| 5257 | if (!consumeIf('_')) |
| 5258 | return nullptr; |
| 5259 | } |
| 5260 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5261 | size_t Index = 0; |
| 5262 | if (!consumeIf('_')) { |
| 5263 | if (parsePositiveInteger(&Index)) |
| 5264 | return nullptr; |
| 5265 | ++Index; |
| 5266 | if (!consumeIf('_')) |
| 5267 | return nullptr; |
| 5268 | } |
| 5269 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5270 | // If we're in a context where this <template-param> refers to a |
| 5271 | // <template-arg> further ahead in the mangled name (currently just conversion |
| 5272 | // operator types), then we should only look it up in the right context. |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5273 | // This can only happen at the outermost level. |
| 5274 | if (PermitForwardTemplateReferences && Level == 0) { |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 5275 | Node *ForwardRef = make<ForwardTemplateReference>(Index); |
| 5276 | if (!ForwardRef) |
| 5277 | return nullptr; |
| 5278 | assert(ForwardRef->getKind() == Node::KForwardTemplateReference); |
| 5279 | ForwardTemplateRefs.push_back( |
| 5280 | static_cast<ForwardTemplateReference *>(ForwardRef)); |
| 5281 | return ForwardRef; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5282 | } |
| 5283 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5284 | if (Level >= TemplateParams.size() || !TemplateParams[Level] || |
| 5285 | Index >= TemplateParams[Level]->size()) { |
| 5286 | // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter |
| 5287 | // list are mangled as the corresponding artificial template type parameter. |
| 5288 | if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) { |
| 5289 | // This will be popped by the ScopedTemplateParamList in |
| 5290 | // parseUnnamedTypeName. |
| 5291 | if (Level == TemplateParams.size()) |
| 5292 | TemplateParams.push_back(nullptr); |
| 5293 | return make<NameType>("auto"); |
| 5294 | } |
| 5295 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5296 | return nullptr; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | return (*TemplateParams[Level])[Index]; |
| 5300 | } |
| 5301 | |
| 5302 | // <template-param-decl> ::= Ty # type parameter |
| 5303 | // ::= Tn <type> # non-type parameter |
| 5304 | // ::= Tt <template-param-decl>* E # template parameter |
| 5305 | // ::= Tp <template-param-decl> # parameter pack |
| 5306 | template <typename Derived, typename Alloc> |
| 5307 | Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() { |
| 5308 | auto InventTemplateParamName = [&](TemplateParamKind Kind) { |
| 5309 | unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++; |
| 5310 | Node *N = make<SyntheticTemplateParamName>(Kind, Index); |
| 5311 | if (N) TemplateParams.back()->push_back(N); |
| 5312 | return N; |
| 5313 | }; |
| 5314 | |
| 5315 | if (consumeIf("Ty")) { |
| 5316 | Node *Name = InventTemplateParamName(TemplateParamKind::Type); |
| 5317 | if (!Name) |
| 5318 | return nullptr; |
| 5319 | return make<TypeTemplateParamDecl>(Name); |
| 5320 | } |
| 5321 | |
| 5322 | if (consumeIf("Tn")) { |
| 5323 | Node *Name = InventTemplateParamName(TemplateParamKind::NonType); |
| 5324 | if (!Name) |
| 5325 | return nullptr; |
| 5326 | Node *Type = parseType(); |
| 5327 | if (!Type) |
| 5328 | return nullptr; |
| 5329 | return make<NonTypeTemplateParamDecl>(Name, Type); |
| 5330 | } |
| 5331 | |
| 5332 | if (consumeIf("Tt")) { |
| 5333 | Node *Name = InventTemplateParamName(TemplateParamKind::Template); |
| 5334 | if (!Name) |
| 5335 | return nullptr; |
| 5336 | size_t ParamsBegin = Names.size(); |
| 5337 | ScopedTemplateParamList TemplateTemplateParamParams(this); |
| 5338 | while (!consumeIf("E")) { |
| 5339 | Node *P = parseTemplateParamDecl(); |
| 5340 | if (!P) |
| 5341 | return nullptr; |
| 5342 | Names.push_back(P); |
| 5343 | } |
| 5344 | NodeArray Params = popTrailingNodeArray(ParamsBegin); |
| 5345 | return make<TemplateTemplateParamDecl>(Name, Params); |
| 5346 | } |
| 5347 | |
| 5348 | if (consumeIf("Tp")) { |
| 5349 | Node *P = parseTemplateParamDecl(); |
| 5350 | if (!P) |
| 5351 | return nullptr; |
| 5352 | return make<TemplateParamPackDecl>(P); |
| 5353 | } |
| 5354 | |
| 5355 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | // <template-arg> ::= <type> # type or template |
| 5359 | // ::= X <expression> E # expression |
| 5360 | // ::= <expr-primary> # simple expressions |
| 5361 | // ::= J <template-arg>* E # argument pack |
| 5362 | // ::= LZ <encoding> E # extension |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5363 | template <typename Derived, typename Alloc> |
| 5364 | Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5365 | switch (look()) { |
| 5366 | case 'X': { |
| 5367 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5368 | Node *Arg = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5369 | if (Arg == nullptr || !consumeIf('E')) |
| 5370 | return nullptr; |
| 5371 | return Arg; |
| 5372 | } |
| 5373 | case 'J': { |
| 5374 | ++First; |
| 5375 | size_t ArgsBegin = Names.size(); |
| 5376 | while (!consumeIf('E')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5377 | Node *Arg = getDerived().parseTemplateArg(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5378 | if (Arg == nullptr) |
| 5379 | return nullptr; |
| 5380 | Names.push_back(Arg); |
| 5381 | } |
| 5382 | NodeArray Args = popTrailingNodeArray(ArgsBegin); |
| 5383 | return make<TemplateArgumentPack>(Args); |
| 5384 | } |
| 5385 | case 'L': { |
| 5386 | // ::= LZ <encoding> E # extension |
| 5387 | if (look(1) == 'Z') { |
| 5388 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5389 | Node *Arg = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5390 | if (Arg == nullptr || !consumeIf('E')) |
| 5391 | return nullptr; |
| 5392 | return Arg; |
| 5393 | } |
| 5394 | // ::= <expr-primary> # simple expressions |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5395 | return getDerived().parseExprPrimary(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5396 | } |
| 5397 | default: |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5398 | return getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5399 | } |
| 5400 | } |
| 5401 | |
| 5402 | // <template-args> ::= I <template-arg>* E |
| 5403 | // extension, the abi says <template-arg>+ |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5404 | template <typename Derived, typename Alloc> |
| 5405 | Node * |
| 5406 | AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5407 | if (!consumeIf('I')) |
| 5408 | return nullptr; |
| 5409 | |
| 5410 | // <template-params> refer to the innermost <template-args>. Clear out any |
| 5411 | // outer args that we may have inserted into TemplateParams. |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5412 | if (TagTemplates) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5413 | TemplateParams.clear(); |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5414 | TemplateParams.push_back(&OuterTemplateParams); |
| 5415 | OuterTemplateParams.clear(); |
| 5416 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5417 | |
| 5418 | size_t ArgsBegin = Names.size(); |
| 5419 | while (!consumeIf('E')) { |
| 5420 | if (TagTemplates) { |
| 5421 | auto OldParams = std::move(TemplateParams); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5422 | Node *Arg = getDerived().parseTemplateArg(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5423 | TemplateParams = std::move(OldParams); |
| 5424 | if (Arg == nullptr) |
| 5425 | return nullptr; |
| 5426 | Names.push_back(Arg); |
| 5427 | Node *TableEntry = Arg; |
| 5428 | if (Arg->getKind() == Node::KTemplateArgumentPack) { |
| 5429 | TableEntry = make<ParameterPack>( |
| 5430 | static_cast<TemplateArgumentPack*>(TableEntry)->getElements()); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 5431 | if (!TableEntry) |
| 5432 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5433 | } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5434 | TemplateParams.back()->push_back(TableEntry); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5435 | } else { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5436 | Node *Arg = getDerived().parseTemplateArg(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5437 | if (Arg == nullptr) |
| 5438 | return nullptr; |
| 5439 | Names.push_back(Arg); |
| 5440 | } |
| 5441 | } |
| 5442 | return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin)); |
| 5443 | } |
| 5444 | |
| 5445 | // <mangled-name> ::= _Z <encoding> |
| 5446 | // ::= <type> |
| 5447 | // extension ::= ___Z <encoding> _block_invoke |
| 5448 | // extension ::= ___Z <encoding> _block_invoke<decimal-digit>+ |
| 5449 | // extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+ |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5450 | template <typename Derived, typename Alloc> |
| 5451 | Node *AbstractManglingParser<Derived, Alloc>::parse() { |
Erik Pilkington | c0df158 | 2019-01-17 21:37:36 +0000 | [diff] [blame] | 5452 | if (consumeIf("_Z") || consumeIf("__Z")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5453 | Node *Encoding = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5454 | if (Encoding == nullptr) |
| 5455 | return nullptr; |
| 5456 | if (look() == '.') { |
| 5457 | Encoding = make<DotSuffix>(Encoding, StringView(First, Last)); |
| 5458 | First = Last; |
| 5459 | } |
| 5460 | if (numLeft() != 0) |
| 5461 | return nullptr; |
| 5462 | return Encoding; |
| 5463 | } |
| 5464 | |
Erik Pilkington | c0df158 | 2019-01-17 21:37:36 +0000 | [diff] [blame] | 5465 | if (consumeIf("___Z") || consumeIf("____Z")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5466 | Node *Encoding = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5467 | if (Encoding == nullptr || !consumeIf("_block_invoke")) |
| 5468 | return nullptr; |
| 5469 | bool RequireNumber = consumeIf('_'); |
| 5470 | if (parseNumber().empty() && RequireNumber) |
| 5471 | return nullptr; |
| 5472 | if (look() == '.') |
| 5473 | First = Last; |
| 5474 | if (numLeft() != 0) |
| 5475 | return nullptr; |
| 5476 | return make<SpecialName>("invocation function for block in ", Encoding); |
| 5477 | } |
| 5478 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5479 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5480 | if (numLeft() != 0) |
| 5481 | return nullptr; |
| 5482 | return Ty; |
| 5483 | } |
| 5484 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5485 | template <typename Alloc> |
| 5486 | struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> { |
| 5487 | using AbstractManglingParser<ManglingParser<Alloc>, |
| 5488 | Alloc>::AbstractManglingParser; |
| 5489 | }; |
| 5490 | |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 5491 | DEMANGLE_NAMESPACE_END |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5492 | |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 5493 | #endif // DEMANGLE_ITANIUMDEMANGLE_H |