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 | |
| 19 | // FIXME: (possibly) incomplete list of features that clang mangles that this |
| 20 | // file does not yet support: |
| 21 | // - C++ modules TS |
| 22 | |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 23 | #include "DemangleConfig.h" |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 24 | #include "StringView.h" |
| 25 | #include "Utility.h" |
Nathan Sidwell | bbc632f | 2022-01-24 04:11:59 -0800 | [diff] [blame] | 26 | #include <algorithm> |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 27 | #include <cassert> |
| 28 | #include <cctype> |
| 29 | #include <cstdio> |
| 30 | #include <cstdlib> |
| 31 | #include <cstring> |
Nathan Sidwell | bbc632f | 2022-01-24 04:11:59 -0800 | [diff] [blame] | 32 | #include <limits> |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 33 | #include <utility> |
| 34 | |
Nathan Sidwell | f19ccea | 2022-02-18 04:05:06 -0800 | [diff] [blame] | 35 | #define FOR_EACH_NODE_KIND(X) \ |
| 36 | X(NodeArrayNode) \ |
| 37 | X(DotSuffix) \ |
| 38 | X(VendorExtQualType) \ |
| 39 | X(QualType) \ |
| 40 | X(ConversionOperatorType) \ |
| 41 | X(PostfixQualifiedType) \ |
| 42 | X(ElaboratedTypeSpefType) \ |
| 43 | X(NameType) \ |
| 44 | X(AbiTagAttr) \ |
| 45 | X(EnableIfAttr) \ |
| 46 | X(ObjCProtoName) \ |
| 47 | X(PointerType) \ |
| 48 | X(ReferenceType) \ |
| 49 | X(PointerToMemberType) \ |
| 50 | X(ArrayType) \ |
| 51 | X(FunctionType) \ |
| 52 | X(NoexceptSpec) \ |
| 53 | X(DynamicExceptionSpec) \ |
| 54 | X(FunctionEncoding) \ |
| 55 | X(LiteralOperator) \ |
| 56 | X(SpecialName) \ |
| 57 | X(CtorVtableSpecialName) \ |
| 58 | X(QualifiedName) \ |
| 59 | X(NestedName) \ |
| 60 | X(LocalName) \ |
| 61 | X(VectorType) \ |
| 62 | X(PixelVectorType) \ |
| 63 | X(BinaryFPType) \ |
| 64 | X(SyntheticTemplateParamName) \ |
| 65 | X(TypeTemplateParamDecl) \ |
| 66 | X(NonTypeTemplateParamDecl) \ |
| 67 | X(TemplateTemplateParamDecl) \ |
| 68 | X(TemplateParamPackDecl) \ |
| 69 | X(ParameterPack) \ |
| 70 | X(TemplateArgumentPack) \ |
| 71 | X(ParameterPackExpansion) \ |
| 72 | X(TemplateArgs) \ |
| 73 | X(ForwardTemplateReference) \ |
| 74 | X(NameWithTemplateArgs) \ |
| 75 | X(GlobalQualifiedName) \ |
| 76 | X(ExpandedSpecialSubstitution) \ |
| 77 | X(SpecialSubstitution) \ |
| 78 | X(CtorDtorName) \ |
| 79 | X(DtorName) \ |
| 80 | X(UnnamedTypeName) \ |
| 81 | X(ClosureTypeName) \ |
| 82 | X(StructuredBindingName) \ |
| 83 | X(BinaryExpr) \ |
| 84 | X(ArraySubscriptExpr) \ |
| 85 | X(PostfixExpr) \ |
| 86 | X(ConditionalExpr) \ |
| 87 | X(MemberExpr) \ |
| 88 | X(SubobjectExpr) \ |
| 89 | X(EnclosingExpr) \ |
| 90 | X(CastExpr) \ |
| 91 | X(SizeofParamPackExpr) \ |
| 92 | X(CallExpr) \ |
| 93 | X(NewExpr) \ |
| 94 | X(DeleteExpr) \ |
| 95 | X(PrefixExpr) \ |
| 96 | X(FunctionParam) \ |
| 97 | X(ConversionExpr) \ |
| 98 | X(PointerToMemberConversionExpr) \ |
| 99 | X(InitListExpr) \ |
| 100 | X(FoldExpr) \ |
| 101 | X(ThrowExpr) \ |
| 102 | X(BoolExpr) \ |
| 103 | X(StringLiteral) \ |
| 104 | X(LambdaExpr) \ |
| 105 | X(EnumLiteral) \ |
| 106 | X(IntegerLiteral) \ |
| 107 | X(FloatLiteral) \ |
| 108 | X(DoubleLiteral) \ |
| 109 | X(LongDoubleLiteral) \ |
| 110 | X(BracedExpr) \ |
| 111 | X(BracedRangeExpr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 112 | |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 113 | DEMANGLE_NAMESPACE_BEGIN |
| 114 | |
Mikhail Borisov | 8452f06 | 2021-08-17 18:06:53 -0400 | [diff] [blame] | 115 | template <class T, size_t N> class PODSmallVector { |
| 116 | static_assert(std::is_pod<T>::value, |
| 117 | "T is required to be a plain old data type"); |
| 118 | |
| 119 | T *First = nullptr; |
| 120 | T *Last = nullptr; |
| 121 | T *Cap = nullptr; |
| 122 | T Inline[N] = {0}; |
| 123 | |
| 124 | bool isInline() const { return First == Inline; } |
| 125 | |
| 126 | void clearInline() { |
| 127 | First = Inline; |
| 128 | Last = Inline; |
| 129 | Cap = Inline + N; |
| 130 | } |
| 131 | |
| 132 | void reserve(size_t NewCap) { |
| 133 | size_t S = size(); |
| 134 | if (isInline()) { |
| 135 | auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T))); |
| 136 | if (Tmp == nullptr) |
| 137 | std::terminate(); |
| 138 | std::copy(First, Last, Tmp); |
| 139 | First = Tmp; |
| 140 | } else { |
| 141 | First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T))); |
| 142 | if (First == nullptr) |
| 143 | std::terminate(); |
| 144 | } |
| 145 | Last = First + S; |
| 146 | Cap = First + NewCap; |
| 147 | } |
| 148 | |
| 149 | public: |
| 150 | PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {} |
| 151 | |
| 152 | PODSmallVector(const PODSmallVector &) = delete; |
| 153 | PODSmallVector &operator=(const PODSmallVector &) = delete; |
| 154 | |
| 155 | PODSmallVector(PODSmallVector &&Other) : PODSmallVector() { |
| 156 | if (Other.isInline()) { |
| 157 | std::copy(Other.begin(), Other.end(), First); |
| 158 | Last = First + Other.size(); |
| 159 | Other.clear(); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | First = Other.First; |
| 164 | Last = Other.Last; |
| 165 | Cap = Other.Cap; |
| 166 | Other.clearInline(); |
| 167 | } |
| 168 | |
| 169 | PODSmallVector &operator=(PODSmallVector &&Other) { |
| 170 | if (Other.isInline()) { |
| 171 | if (!isInline()) { |
| 172 | std::free(First); |
| 173 | clearInline(); |
| 174 | } |
| 175 | std::copy(Other.begin(), Other.end(), First); |
| 176 | Last = First + Other.size(); |
| 177 | Other.clear(); |
| 178 | return *this; |
| 179 | } |
| 180 | |
| 181 | if (isInline()) { |
| 182 | First = Other.First; |
| 183 | Last = Other.Last; |
| 184 | Cap = Other.Cap; |
| 185 | Other.clearInline(); |
| 186 | return *this; |
| 187 | } |
| 188 | |
| 189 | std::swap(First, Other.First); |
| 190 | std::swap(Last, Other.Last); |
| 191 | std::swap(Cap, Other.Cap); |
| 192 | Other.clear(); |
| 193 | return *this; |
| 194 | } |
| 195 | |
| 196 | // NOLINTNEXTLINE(readability-identifier-naming) |
| 197 | void push_back(const T &Elem) { |
| 198 | if (Last == Cap) |
| 199 | reserve(size() * 2); |
| 200 | *Last++ = Elem; |
| 201 | } |
| 202 | |
| 203 | // NOLINTNEXTLINE(readability-identifier-naming) |
| 204 | void pop_back() { |
| 205 | assert(Last != First && "Popping empty vector!"); |
| 206 | --Last; |
| 207 | } |
| 208 | |
| 209 | void dropBack(size_t Index) { |
| 210 | assert(Index <= size() && "dropBack() can't expand!"); |
| 211 | Last = First + Index; |
| 212 | } |
| 213 | |
| 214 | T *begin() { return First; } |
| 215 | T *end() { return Last; } |
| 216 | |
| 217 | bool empty() const { return First == Last; } |
| 218 | size_t size() const { return static_cast<size_t>(Last - First); } |
| 219 | T &back() { |
| 220 | assert(Last != First && "Calling back() on empty vector!"); |
| 221 | return *(Last - 1); |
| 222 | } |
| 223 | T &operator[](size_t Index) { |
| 224 | assert(Index < size() && "Invalid access!"); |
| 225 | return *(begin() + Index); |
| 226 | } |
| 227 | void clear() { Last = First; } |
| 228 | |
| 229 | ~PODSmallVector() { |
| 230 | if (!isInline()) |
| 231 | std::free(First); |
| 232 | } |
| 233 | }; |
| 234 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 235 | // Base class of all AST nodes. The AST is built by the parser, then is |
| 236 | // traversed by the printLeft/Right functions to produce a demangled string. |
| 237 | class Node { |
| 238 | public: |
| 239 | enum Kind : unsigned char { |
| 240 | #define ENUMERATOR(NodeKind) K ## NodeKind, |
| 241 | FOR_EACH_NODE_KIND(ENUMERATOR) |
| 242 | #undef ENUMERATOR |
| 243 | }; |
| 244 | |
| 245 | /// Three-way bool to track a cached value. Unknown is possible if this node |
| 246 | /// has an unexpanded parameter pack below it that may affect this cache. |
| 247 | enum class Cache : unsigned char { Yes, No, Unknown, }; |
| 248 | |
| 249 | private: |
| 250 | Kind K; |
| 251 | |
| 252 | // FIXME: Make these protected. |
| 253 | public: |
| 254 | /// Tracks if this node has a component on its right side, in which case we |
| 255 | /// need to call printRight. |
| 256 | Cache RHSComponentCache; |
| 257 | |
| 258 | /// Track if this node is a (possibly qualified) array type. This can affect |
| 259 | /// how we format the output string. |
| 260 | Cache ArrayCache; |
| 261 | |
| 262 | /// Track if this node is a (possibly qualified) function type. This can |
| 263 | /// affect how we format the output string. |
| 264 | Cache FunctionCache; |
| 265 | |
| 266 | public: |
| 267 | Node(Kind K_, Cache RHSComponentCache_ = Cache::No, |
| 268 | Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No) |
| 269 | : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_), |
| 270 | FunctionCache(FunctionCache_) {} |
| 271 | |
| 272 | /// Visit the most-derived object corresponding to this object. |
| 273 | template<typename Fn> void visit(Fn F) const; |
| 274 | |
| 275 | // The following function is provided by all derived classes: |
| 276 | // |
| 277 | // Call F with arguments that, when passed to the constructor of this node, |
| 278 | // would construct an equivalent node. |
| 279 | //template<typename Fn> void match(Fn F) const; |
| 280 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 281 | bool hasRHSComponent(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 282 | if (RHSComponentCache != Cache::Unknown) |
| 283 | return RHSComponentCache == Cache::Yes; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 284 | return hasRHSComponentSlow(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 285 | } |
| 286 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 287 | bool hasArray(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 288 | if (ArrayCache != Cache::Unknown) |
| 289 | return ArrayCache == Cache::Yes; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 290 | return hasArraySlow(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 291 | } |
| 292 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 293 | bool hasFunction(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 294 | if (FunctionCache != Cache::Unknown) |
| 295 | return FunctionCache == Cache::Yes; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 296 | return hasFunctionSlow(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | Kind getKind() const { return K; } |
| 300 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 301 | virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; } |
| 302 | virtual bool hasArraySlow(OutputBuffer &) const { return false; } |
| 303 | virtual bool hasFunctionSlow(OutputBuffer &) const { return false; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 304 | |
| 305 | // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to |
| 306 | // get at a node that actually represents some concrete syntax. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 307 | virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 308 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 309 | void print(OutputBuffer &OB) const { |
| 310 | printLeft(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 311 | if (RHSComponentCache != Cache::No) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 312 | printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Nathan Sidwell | fd0ef6d | 2022-01-20 07:40:12 -0800 | [diff] [blame] | 315 | // Print the "left" side of this Node into OutputBuffer. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 316 | virtual void printLeft(OutputBuffer &) const = 0; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 317 | |
| 318 | // Print the "right". This distinction is necessary to represent C++ types |
| 319 | // that appear on the RHS of their subtype, such as arrays or functions. |
| 320 | // Since most types don't have such a component, provide a default |
| 321 | // implementation. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 322 | virtual void printRight(OutputBuffer &) const {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 323 | |
| 324 | virtual StringView getBaseName() const { return StringView(); } |
| 325 | |
| 326 | // Silence compiler warnings, this dtor will never be called. |
| 327 | virtual ~Node() = default; |
| 328 | |
| 329 | #ifndef NDEBUG |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 330 | DEMANGLE_DUMP_METHOD void dump() const; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 331 | #endif |
| 332 | }; |
| 333 | |
| 334 | class NodeArray { |
| 335 | Node **Elements; |
| 336 | size_t NumElements; |
| 337 | |
| 338 | public: |
| 339 | NodeArray() : Elements(nullptr), NumElements(0) {} |
| 340 | NodeArray(Node **Elements_, size_t NumElements_) |
| 341 | : Elements(Elements_), NumElements(NumElements_) {} |
| 342 | |
| 343 | bool empty() const { return NumElements == 0; } |
| 344 | size_t size() const { return NumElements; } |
| 345 | |
| 346 | Node **begin() const { return Elements; } |
| 347 | Node **end() const { return Elements + NumElements; } |
| 348 | |
| 349 | Node *operator[](size_t Idx) const { return Elements[Idx]; } |
| 350 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 351 | void printWithComma(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 352 | bool FirstElement = true; |
| 353 | for (size_t Idx = 0; Idx != NumElements; ++Idx) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 354 | size_t BeforeComma = OB.getCurrentPosition(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 355 | if (!FirstElement) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 356 | OB += ", "; |
| 357 | size_t AfterComma = OB.getCurrentPosition(); |
| 358 | Elements[Idx]->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 359 | |
| 360 | // Elements[Idx] is an empty parameter pack expansion, we should erase the |
| 361 | // comma we just printed. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 362 | if (AfterComma == OB.getCurrentPosition()) { |
| 363 | OB.setCurrentPosition(BeforeComma); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 364 | continue; |
| 365 | } |
| 366 | |
| 367 | FirstElement = false; |
| 368 | } |
| 369 | } |
| 370 | }; |
| 371 | |
| 372 | struct NodeArrayNode : Node { |
| 373 | NodeArray Array; |
| 374 | NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {} |
| 375 | |
| 376 | template<typename Fn> void match(Fn F) const { F(Array); } |
| 377 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 378 | void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | class DotSuffix final : public Node { |
| 382 | const Node *Prefix; |
| 383 | const StringView Suffix; |
| 384 | |
| 385 | public: |
| 386 | DotSuffix(const Node *Prefix_, StringView Suffix_) |
| 387 | : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {} |
| 388 | |
| 389 | template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); } |
| 390 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 391 | void printLeft(OutputBuffer &OB) const override { |
| 392 | Prefix->print(OB); |
| 393 | OB += " ("; |
| 394 | OB += Suffix; |
| 395 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 396 | } |
| 397 | }; |
| 398 | |
| 399 | class VendorExtQualType final : public Node { |
| 400 | const Node *Ty; |
| 401 | StringView Ext; |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 402 | const Node *TA; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 403 | |
| 404 | public: |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 405 | VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_) |
| 406 | : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 407 | |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 408 | template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 409 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 410 | void printLeft(OutputBuffer &OB) const override { |
| 411 | Ty->print(OB); |
| 412 | OB += " "; |
| 413 | OB += Ext; |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 414 | if (TA != nullptr) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 415 | TA->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 416 | } |
| 417 | }; |
| 418 | |
| 419 | enum FunctionRefQual : unsigned char { |
| 420 | FrefQualNone, |
| 421 | FrefQualLValue, |
| 422 | FrefQualRValue, |
| 423 | }; |
| 424 | |
| 425 | enum Qualifiers { |
| 426 | QualNone = 0, |
| 427 | QualConst = 0x1, |
| 428 | QualVolatile = 0x2, |
| 429 | QualRestrict = 0x4, |
| 430 | }; |
| 431 | |
| 432 | inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) { |
| 433 | return Q1 = static_cast<Qualifiers>(Q1 | Q2); |
| 434 | } |
| 435 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 436 | class QualType final : public Node { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 437 | protected: |
| 438 | const Qualifiers Quals; |
| 439 | const Node *Child; |
| 440 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 441 | void printQuals(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 442 | if (Quals & QualConst) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 443 | OB += " const"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 444 | if (Quals & QualVolatile) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 445 | OB += " volatile"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 446 | if (Quals & QualRestrict) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 447 | OB += " restrict"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | public: |
| 451 | QualType(const Node *Child_, Qualifiers Quals_) |
| 452 | : Node(KQualType, Child_->RHSComponentCache, |
| 453 | Child_->ArrayCache, Child_->FunctionCache), |
| 454 | Quals(Quals_), Child(Child_) {} |
| 455 | |
| 456 | template<typename Fn> void match(Fn F) const { F(Child, Quals); } |
| 457 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 458 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
| 459 | return Child->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 460 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 461 | bool hasArraySlow(OutputBuffer &OB) const override { |
| 462 | return Child->hasArray(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 463 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 464 | bool hasFunctionSlow(OutputBuffer &OB) const override { |
| 465 | return Child->hasFunction(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 466 | } |
| 467 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 468 | void printLeft(OutputBuffer &OB) const override { |
| 469 | Child->printLeft(OB); |
| 470 | printQuals(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 471 | } |
| 472 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 473 | void printRight(OutputBuffer &OB) const override { Child->printRight(OB); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 474 | }; |
| 475 | |
| 476 | class ConversionOperatorType final : public Node { |
| 477 | const Node *Ty; |
| 478 | |
| 479 | public: |
| 480 | ConversionOperatorType(const Node *Ty_) |
| 481 | : Node(KConversionOperatorType), Ty(Ty_) {} |
| 482 | |
| 483 | template<typename Fn> void match(Fn F) const { F(Ty); } |
| 484 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 485 | void printLeft(OutputBuffer &OB) const override { |
| 486 | OB += "operator "; |
| 487 | Ty->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 488 | } |
| 489 | }; |
| 490 | |
| 491 | class PostfixQualifiedType final : public Node { |
| 492 | const Node *Ty; |
| 493 | const StringView Postfix; |
| 494 | |
| 495 | public: |
| 496 | PostfixQualifiedType(Node *Ty_, StringView Postfix_) |
| 497 | : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {} |
| 498 | |
| 499 | template<typename Fn> void match(Fn F) const { F(Ty, Postfix); } |
| 500 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 501 | void printLeft(OutputBuffer &OB) const override { |
| 502 | Ty->printLeft(OB); |
| 503 | OB += Postfix; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 504 | } |
| 505 | }; |
| 506 | |
| 507 | class NameType final : public Node { |
| 508 | const StringView Name; |
| 509 | |
| 510 | public: |
| 511 | NameType(StringView Name_) : Node(KNameType), Name(Name_) {} |
| 512 | |
| 513 | template<typename Fn> void match(Fn F) const { F(Name); } |
| 514 | |
| 515 | StringView getName() const { return Name; } |
| 516 | StringView getBaseName() const override { return Name; } |
| 517 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 518 | void printLeft(OutputBuffer &OB) const override { OB += Name; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 519 | }; |
| 520 | |
| 521 | class ElaboratedTypeSpefType : public Node { |
| 522 | StringView Kind; |
| 523 | Node *Child; |
| 524 | public: |
| 525 | ElaboratedTypeSpefType(StringView Kind_, Node *Child_) |
| 526 | : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {} |
| 527 | |
| 528 | template<typename Fn> void match(Fn F) const { F(Kind, Child); } |
| 529 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 530 | void printLeft(OutputBuffer &OB) const override { |
| 531 | OB += Kind; |
| 532 | OB += ' '; |
| 533 | Child->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 534 | } |
| 535 | }; |
| 536 | |
| 537 | struct AbiTagAttr : Node { |
| 538 | Node *Base; |
| 539 | StringView Tag; |
| 540 | |
| 541 | AbiTagAttr(Node* Base_, StringView Tag_) |
| 542 | : Node(KAbiTagAttr, Base_->RHSComponentCache, |
| 543 | Base_->ArrayCache, Base_->FunctionCache), |
| 544 | Base(Base_), Tag(Tag_) {} |
| 545 | |
| 546 | template<typename Fn> void match(Fn F) const { F(Base, Tag); } |
| 547 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 548 | void printLeft(OutputBuffer &OB) const override { |
| 549 | Base->printLeft(OB); |
| 550 | OB += "[abi:"; |
| 551 | OB += Tag; |
| 552 | OB += "]"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 553 | } |
| 554 | }; |
| 555 | |
| 556 | class EnableIfAttr : public Node { |
| 557 | NodeArray Conditions; |
| 558 | public: |
| 559 | EnableIfAttr(NodeArray Conditions_) |
| 560 | : Node(KEnableIfAttr), Conditions(Conditions_) {} |
| 561 | |
| 562 | template<typename Fn> void match(Fn F) const { F(Conditions); } |
| 563 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 564 | void printLeft(OutputBuffer &OB) const override { |
| 565 | OB += " [enable_if:"; |
| 566 | Conditions.printWithComma(OB); |
| 567 | OB += ']'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 568 | } |
| 569 | }; |
| 570 | |
| 571 | class ObjCProtoName : public Node { |
| 572 | const Node *Ty; |
| 573 | StringView Protocol; |
| 574 | |
| 575 | friend class PointerType; |
| 576 | |
| 577 | public: |
| 578 | ObjCProtoName(const Node *Ty_, StringView Protocol_) |
| 579 | : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {} |
| 580 | |
| 581 | template<typename Fn> void match(Fn F) const { F(Ty, Protocol); } |
| 582 | |
| 583 | bool isObjCObject() const { |
| 584 | return Ty->getKind() == KNameType && |
| 585 | static_cast<const NameType *>(Ty)->getName() == "objc_object"; |
| 586 | } |
| 587 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 588 | void printLeft(OutputBuffer &OB) const override { |
| 589 | Ty->print(OB); |
| 590 | OB += "<"; |
| 591 | OB += Protocol; |
| 592 | OB += ">"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 593 | } |
| 594 | }; |
| 595 | |
| 596 | class PointerType final : public Node { |
| 597 | const Node *Pointee; |
| 598 | |
| 599 | public: |
| 600 | PointerType(const Node *Pointee_) |
| 601 | : Node(KPointerType, Pointee_->RHSComponentCache), |
| 602 | Pointee(Pointee_) {} |
| 603 | |
| 604 | template<typename Fn> void match(Fn F) const { F(Pointee); } |
| 605 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 606 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
| 607 | return Pointee->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 608 | } |
| 609 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 610 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 611 | // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>. |
| 612 | if (Pointee->getKind() != KObjCProtoName || |
| 613 | !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 614 | Pointee->printLeft(OB); |
| 615 | if (Pointee->hasArray(OB)) |
| 616 | OB += " "; |
| 617 | if (Pointee->hasArray(OB) || Pointee->hasFunction(OB)) |
| 618 | OB += "("; |
| 619 | OB += "*"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 620 | } else { |
| 621 | const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 622 | OB += "id<"; |
| 623 | OB += objcProto->Protocol; |
| 624 | OB += ">"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 628 | void printRight(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 629 | if (Pointee->getKind() != KObjCProtoName || |
| 630 | !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 631 | if (Pointee->hasArray(OB) || Pointee->hasFunction(OB)) |
| 632 | OB += ")"; |
| 633 | Pointee->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | }; |
| 637 | |
| 638 | enum class ReferenceKind { |
| 639 | LValue, |
| 640 | RValue, |
| 641 | }; |
| 642 | |
| 643 | // Represents either a LValue or an RValue reference type. |
| 644 | class ReferenceType : public Node { |
| 645 | const Node *Pointee; |
| 646 | ReferenceKind RK; |
| 647 | |
| 648 | mutable bool Printing = false; |
| 649 | |
| 650 | // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The |
| 651 | // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any |
| 652 | // other combination collapses to a lvalue ref. |
Mikhail Borisov | 05f7722 | 2021-08-17 18:10:57 -0400 | [diff] [blame] | 653 | // |
| 654 | // A combination of a TemplateForwardReference and a back-ref Substitution |
| 655 | // from an ill-formed string may have created a cycle; use cycle detection to |
| 656 | // avoid looping forever. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 657 | std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 658 | auto SoFar = std::make_pair(RK, Pointee); |
Mikhail Borisov | 05f7722 | 2021-08-17 18:10:57 -0400 | [diff] [blame] | 659 | // Track the chain of nodes for the Floyd's 'tortoise and hare' |
| 660 | // cycle-detection algorithm, since getSyntaxNode(S) is impure |
| 661 | PODSmallVector<const Node *, 8> Prev; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 662 | for (;;) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 663 | const Node *SN = SoFar.second->getSyntaxNode(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 664 | if (SN->getKind() != KReferenceType) |
| 665 | break; |
| 666 | auto *RT = static_cast<const ReferenceType *>(SN); |
| 667 | SoFar.second = RT->Pointee; |
| 668 | SoFar.first = std::min(SoFar.first, RT->RK); |
Mikhail Borisov | 05f7722 | 2021-08-17 18:10:57 -0400 | [diff] [blame] | 669 | |
| 670 | // The middle of Prev is the 'slow' pointer moving at half speed |
| 671 | Prev.push_back(SoFar.second); |
| 672 | if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) { |
| 673 | // Cycle detected |
| 674 | SoFar.second = nullptr; |
| 675 | break; |
| 676 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 677 | } |
| 678 | return SoFar; |
| 679 | } |
| 680 | |
| 681 | public: |
| 682 | ReferenceType(const Node *Pointee_, ReferenceKind RK_) |
| 683 | : Node(KReferenceType, Pointee_->RHSComponentCache), |
| 684 | Pointee(Pointee_), RK(RK_) {} |
| 685 | |
| 686 | template<typename Fn> void match(Fn F) const { F(Pointee, RK); } |
| 687 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 688 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
| 689 | return Pointee->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 690 | } |
| 691 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 692 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 693 | if (Printing) |
| 694 | return; |
| 695 | SwapAndRestore<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 696 | std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB); |
Mikhail Borisov | 05f7722 | 2021-08-17 18:10:57 -0400 | [diff] [blame] | 697 | if (!Collapsed.second) |
| 698 | return; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 699 | Collapsed.second->printLeft(OB); |
| 700 | if (Collapsed.second->hasArray(OB)) |
| 701 | OB += " "; |
| 702 | if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB)) |
| 703 | OB += "("; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 704 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 705 | OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 706 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 707 | void printRight(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 708 | if (Printing) |
| 709 | return; |
| 710 | SwapAndRestore<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 711 | std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB); |
Mikhail Borisov | 05f7722 | 2021-08-17 18:10:57 -0400 | [diff] [blame] | 712 | if (!Collapsed.second) |
| 713 | return; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 714 | if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB)) |
| 715 | OB += ")"; |
| 716 | Collapsed.second->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 717 | } |
| 718 | }; |
| 719 | |
| 720 | class PointerToMemberType final : public Node { |
| 721 | const Node *ClassType; |
| 722 | const Node *MemberType; |
| 723 | |
| 724 | public: |
| 725 | PointerToMemberType(const Node *ClassType_, const Node *MemberType_) |
| 726 | : Node(KPointerToMemberType, MemberType_->RHSComponentCache), |
| 727 | ClassType(ClassType_), MemberType(MemberType_) {} |
| 728 | |
| 729 | template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); } |
| 730 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 731 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
| 732 | return MemberType->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 733 | } |
| 734 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 735 | void printLeft(OutputBuffer &OB) const override { |
| 736 | MemberType->printLeft(OB); |
| 737 | if (MemberType->hasArray(OB) || MemberType->hasFunction(OB)) |
| 738 | OB += "("; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 739 | else |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 740 | OB += " "; |
| 741 | ClassType->print(OB); |
| 742 | OB += "::*"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 743 | } |
| 744 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 745 | void printRight(OutputBuffer &OB) const override { |
| 746 | if (MemberType->hasArray(OB) || MemberType->hasFunction(OB)) |
| 747 | OB += ")"; |
| 748 | MemberType->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 749 | } |
| 750 | }; |
| 751 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 752 | class ArrayType final : public Node { |
| 753 | const Node *Base; |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 754 | Node *Dimension; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 755 | |
| 756 | public: |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 757 | ArrayType(const Node *Base_, Node *Dimension_) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 758 | : Node(KArrayType, |
| 759 | /*RHSComponentCache=*/Cache::Yes, |
| 760 | /*ArrayCache=*/Cache::Yes), |
| 761 | Base(Base_), Dimension(Dimension_) {} |
| 762 | |
| 763 | template<typename Fn> void match(Fn F) const { F(Base, Dimension); } |
| 764 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 765 | bool hasRHSComponentSlow(OutputBuffer &) const override { return true; } |
| 766 | bool hasArraySlow(OutputBuffer &) const override { return true; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 767 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 768 | void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 769 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 770 | void printRight(OutputBuffer &OB) const override { |
| 771 | if (OB.back() != ']') |
| 772 | OB += " "; |
| 773 | OB += "["; |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 774 | if (Dimension) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 775 | Dimension->print(OB); |
| 776 | OB += "]"; |
| 777 | Base->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 778 | } |
| 779 | }; |
| 780 | |
| 781 | class FunctionType final : public Node { |
| 782 | const Node *Ret; |
| 783 | NodeArray Params; |
| 784 | Qualifiers CVQuals; |
| 785 | FunctionRefQual RefQual; |
| 786 | const Node *ExceptionSpec; |
| 787 | |
| 788 | public: |
| 789 | FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_, |
| 790 | FunctionRefQual RefQual_, const Node *ExceptionSpec_) |
| 791 | : Node(KFunctionType, |
| 792 | /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No, |
| 793 | /*FunctionCache=*/Cache::Yes), |
| 794 | Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_), |
| 795 | ExceptionSpec(ExceptionSpec_) {} |
| 796 | |
| 797 | template<typename Fn> void match(Fn F) const { |
| 798 | F(Ret, Params, CVQuals, RefQual, ExceptionSpec); |
| 799 | } |
| 800 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 801 | bool hasRHSComponentSlow(OutputBuffer &) const override { return true; } |
| 802 | bool hasFunctionSlow(OutputBuffer &) const override { return true; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 803 | |
| 804 | // Handle C++'s ... quirky decl grammar by using the left & right |
| 805 | // distinction. Consider: |
| 806 | // int (*f(float))(char) {} |
| 807 | // f is a function that takes a float and returns a pointer to a function |
| 808 | // that takes a char and returns an int. If we're trying to print f, start |
| 809 | // by printing out the return types's left, then print our parameters, then |
| 810 | // finally print right of the return type. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 811 | void printLeft(OutputBuffer &OB) const override { |
| 812 | Ret->printLeft(OB); |
| 813 | OB += " "; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 814 | } |
| 815 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 816 | void printRight(OutputBuffer &OB) const override { |
| 817 | OB += "("; |
| 818 | Params.printWithComma(OB); |
| 819 | OB += ")"; |
| 820 | Ret->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 821 | |
| 822 | if (CVQuals & QualConst) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 823 | OB += " const"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 824 | if (CVQuals & QualVolatile) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 825 | OB += " volatile"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 826 | if (CVQuals & QualRestrict) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 827 | OB += " restrict"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 828 | |
| 829 | if (RefQual == FrefQualLValue) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 830 | OB += " &"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 831 | else if (RefQual == FrefQualRValue) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 832 | OB += " &&"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 833 | |
| 834 | if (ExceptionSpec != nullptr) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 835 | OB += ' '; |
| 836 | ExceptionSpec->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | }; |
| 840 | |
| 841 | class NoexceptSpec : public Node { |
| 842 | const Node *E; |
| 843 | public: |
| 844 | NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {} |
| 845 | |
| 846 | template<typename Fn> void match(Fn F) const { F(E); } |
| 847 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 848 | void printLeft(OutputBuffer &OB) const override { |
| 849 | OB += "noexcept("; |
| 850 | E->print(OB); |
| 851 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 852 | } |
| 853 | }; |
| 854 | |
| 855 | class DynamicExceptionSpec : public Node { |
| 856 | NodeArray Types; |
| 857 | public: |
| 858 | DynamicExceptionSpec(NodeArray Types_) |
| 859 | : Node(KDynamicExceptionSpec), Types(Types_) {} |
| 860 | |
| 861 | template<typename Fn> void match(Fn F) const { F(Types); } |
| 862 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 863 | void printLeft(OutputBuffer &OB) const override { |
| 864 | OB += "throw("; |
| 865 | Types.printWithComma(OB); |
| 866 | OB += ')'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 867 | } |
| 868 | }; |
| 869 | |
| 870 | class FunctionEncoding final : public Node { |
| 871 | const Node *Ret; |
| 872 | const Node *Name; |
| 873 | NodeArray Params; |
| 874 | const Node *Attrs; |
| 875 | Qualifiers CVQuals; |
| 876 | FunctionRefQual RefQual; |
| 877 | |
| 878 | public: |
| 879 | FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_, |
| 880 | const Node *Attrs_, Qualifiers CVQuals_, |
| 881 | FunctionRefQual RefQual_) |
| 882 | : Node(KFunctionEncoding, |
| 883 | /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No, |
| 884 | /*FunctionCache=*/Cache::Yes), |
| 885 | Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_), |
| 886 | CVQuals(CVQuals_), RefQual(RefQual_) {} |
| 887 | |
| 888 | template<typename Fn> void match(Fn F) const { |
| 889 | F(Ret, Name, Params, Attrs, CVQuals, RefQual); |
| 890 | } |
| 891 | |
| 892 | Qualifiers getCVQuals() const { return CVQuals; } |
| 893 | FunctionRefQual getRefQual() const { return RefQual; } |
| 894 | NodeArray getParams() const { return Params; } |
| 895 | const Node *getReturnType() const { return Ret; } |
| 896 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 897 | bool hasRHSComponentSlow(OutputBuffer &) const override { return true; } |
| 898 | bool hasFunctionSlow(OutputBuffer &) const override { return true; } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 899 | |
| 900 | const Node *getName() const { return Name; } |
| 901 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 902 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 903 | if (Ret) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 904 | Ret->printLeft(OB); |
| 905 | if (!Ret->hasRHSComponent(OB)) |
| 906 | OB += " "; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 907 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 908 | Name->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 909 | } |
| 910 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 911 | void printRight(OutputBuffer &OB) const override { |
| 912 | OB += "("; |
| 913 | Params.printWithComma(OB); |
| 914 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 915 | if (Ret) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 916 | Ret->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 917 | |
| 918 | if (CVQuals & QualConst) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 919 | OB += " const"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 920 | if (CVQuals & QualVolatile) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 921 | OB += " volatile"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 922 | if (CVQuals & QualRestrict) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 923 | OB += " restrict"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 924 | |
| 925 | if (RefQual == FrefQualLValue) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 926 | OB += " &"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 927 | else if (RefQual == FrefQualRValue) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 928 | OB += " &&"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 929 | |
| 930 | if (Attrs != nullptr) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 931 | Attrs->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 932 | } |
| 933 | }; |
| 934 | |
| 935 | class LiteralOperator : public Node { |
| 936 | const Node *OpName; |
| 937 | |
| 938 | public: |
| 939 | LiteralOperator(const Node *OpName_) |
| 940 | : Node(KLiteralOperator), OpName(OpName_) {} |
| 941 | |
| 942 | template<typename Fn> void match(Fn F) const { F(OpName); } |
| 943 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 944 | void printLeft(OutputBuffer &OB) const override { |
| 945 | OB += "operator\"\" "; |
| 946 | OpName->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 947 | } |
| 948 | }; |
| 949 | |
| 950 | class SpecialName final : public Node { |
| 951 | const StringView Special; |
| 952 | const Node *Child; |
| 953 | |
| 954 | public: |
| 955 | SpecialName(StringView Special_, const Node *Child_) |
| 956 | : Node(KSpecialName), Special(Special_), Child(Child_) {} |
| 957 | |
| 958 | template<typename Fn> void match(Fn F) const { F(Special, Child); } |
| 959 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 960 | void printLeft(OutputBuffer &OB) const override { |
| 961 | OB += Special; |
| 962 | Child->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 963 | } |
| 964 | }; |
| 965 | |
| 966 | class CtorVtableSpecialName final : public Node { |
| 967 | const Node *FirstType; |
| 968 | const Node *SecondType; |
| 969 | |
| 970 | public: |
| 971 | CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_) |
| 972 | : Node(KCtorVtableSpecialName), |
| 973 | FirstType(FirstType_), SecondType(SecondType_) {} |
| 974 | |
| 975 | template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); } |
| 976 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 977 | void printLeft(OutputBuffer &OB) const override { |
| 978 | OB += "construction vtable for "; |
| 979 | FirstType->print(OB); |
| 980 | OB += "-in-"; |
| 981 | SecondType->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 982 | } |
| 983 | }; |
| 984 | |
| 985 | struct NestedName : Node { |
| 986 | Node *Qual; |
| 987 | Node *Name; |
| 988 | |
| 989 | NestedName(Node *Qual_, Node *Name_) |
| 990 | : Node(KNestedName), Qual(Qual_), Name(Name_) {} |
| 991 | |
| 992 | template<typename Fn> void match(Fn F) const { F(Qual, Name); } |
| 993 | |
| 994 | StringView getBaseName() const override { return Name->getBaseName(); } |
| 995 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 996 | void printLeft(OutputBuffer &OB) const override { |
| 997 | Qual->print(OB); |
| 998 | OB += "::"; |
| 999 | Name->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1000 | } |
| 1001 | }; |
| 1002 | |
| 1003 | struct LocalName : Node { |
| 1004 | Node *Encoding; |
| 1005 | Node *Entity; |
| 1006 | |
| 1007 | LocalName(Node *Encoding_, Node *Entity_) |
| 1008 | : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {} |
| 1009 | |
| 1010 | template<typename Fn> void match(Fn F) const { F(Encoding, Entity); } |
| 1011 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1012 | void printLeft(OutputBuffer &OB) const override { |
| 1013 | Encoding->print(OB); |
| 1014 | OB += "::"; |
| 1015 | Entity->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1016 | } |
| 1017 | }; |
| 1018 | |
| 1019 | class QualifiedName final : public Node { |
| 1020 | // qualifier::name |
| 1021 | const Node *Qualifier; |
| 1022 | const Node *Name; |
| 1023 | |
| 1024 | public: |
| 1025 | QualifiedName(const Node *Qualifier_, const Node *Name_) |
| 1026 | : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {} |
| 1027 | |
| 1028 | template<typename Fn> void match(Fn F) const { F(Qualifier, Name); } |
| 1029 | |
| 1030 | StringView getBaseName() const override { return Name->getBaseName(); } |
| 1031 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1032 | void printLeft(OutputBuffer &OB) const override { |
| 1033 | Qualifier->print(OB); |
| 1034 | OB += "::"; |
| 1035 | Name->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1036 | } |
| 1037 | }; |
| 1038 | |
| 1039 | class VectorType final : public Node { |
| 1040 | const Node *BaseType; |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 1041 | const Node *Dimension; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1042 | |
| 1043 | public: |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 1044 | VectorType(const Node *BaseType_, Node *Dimension_) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1045 | : Node(KVectorType), BaseType(BaseType_), |
| 1046 | Dimension(Dimension_) {} |
| 1047 | |
| 1048 | template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); } |
| 1049 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1050 | void printLeft(OutputBuffer &OB) const override { |
| 1051 | BaseType->print(OB); |
| 1052 | OB += " vector["; |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 1053 | if (Dimension) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1054 | Dimension->print(OB); |
| 1055 | OB += "]"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1056 | } |
| 1057 | }; |
| 1058 | |
| 1059 | class PixelVectorType final : public Node { |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 1060 | const Node *Dimension; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1061 | |
| 1062 | public: |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 1063 | PixelVectorType(const Node *Dimension_) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1064 | : Node(KPixelVectorType), Dimension(Dimension_) {} |
| 1065 | |
| 1066 | template<typename Fn> void match(Fn F) const { F(Dimension); } |
| 1067 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1068 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1069 | // FIXME: This should demangle as "vector pixel". |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1070 | OB += "pixel vector["; |
| 1071 | Dimension->print(OB); |
| 1072 | OB += "]"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1073 | } |
| 1074 | }; |
| 1075 | |
Pengfei Wang | 50e90b8 | 2021-09-23 11:02:25 +0800 | [diff] [blame] | 1076 | class BinaryFPType final : public Node { |
| 1077 | const Node *Dimension; |
| 1078 | |
| 1079 | public: |
| 1080 | BinaryFPType(const Node *Dimension_) |
| 1081 | : Node(KBinaryFPType), Dimension(Dimension_) {} |
| 1082 | |
| 1083 | template<typename Fn> void match(Fn F) const { F(Dimension); } |
| 1084 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1085 | void printLeft(OutputBuffer &OB) const override { |
| 1086 | OB += "_Float"; |
| 1087 | Dimension->print(OB); |
Pengfei Wang | 50e90b8 | 2021-09-23 11:02:25 +0800 | [diff] [blame] | 1088 | } |
| 1089 | }; |
| 1090 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1091 | enum class TemplateParamKind { Type, NonType, Template }; |
| 1092 | |
| 1093 | /// An invented name for a template parameter for which we don't have a |
| 1094 | /// corresponding template argument. |
| 1095 | /// |
| 1096 | /// This node is created when parsing the <lambda-sig> for a lambda with |
| 1097 | /// explicit template arguments, which might be referenced in the parameter |
| 1098 | /// types appearing later in the <lambda-sig>. |
| 1099 | class SyntheticTemplateParamName final : public Node { |
| 1100 | TemplateParamKind Kind; |
| 1101 | unsigned Index; |
| 1102 | |
| 1103 | public: |
| 1104 | SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_) |
| 1105 | : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {} |
| 1106 | |
| 1107 | template<typename Fn> void match(Fn F) const { F(Kind, Index); } |
| 1108 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1109 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1110 | switch (Kind) { |
| 1111 | case TemplateParamKind::Type: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1112 | OB += "$T"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1113 | break; |
| 1114 | case TemplateParamKind::NonType: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1115 | OB += "$N"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1116 | break; |
| 1117 | case TemplateParamKind::Template: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1118 | OB += "$TT"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1119 | break; |
| 1120 | } |
| 1121 | if (Index > 0) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1122 | OB << Index - 1; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1123 | } |
| 1124 | }; |
| 1125 | |
| 1126 | /// A template type parameter declaration, 'typename T'. |
| 1127 | class TypeTemplateParamDecl final : public Node { |
| 1128 | Node *Name; |
| 1129 | |
| 1130 | public: |
| 1131 | TypeTemplateParamDecl(Node *Name_) |
| 1132 | : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {} |
| 1133 | |
| 1134 | template<typename Fn> void match(Fn F) const { F(Name); } |
| 1135 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1136 | void printLeft(OutputBuffer &OB) const override { OB += "typename "; } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1137 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1138 | void printRight(OutputBuffer &OB) const override { Name->print(OB); } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1139 | }; |
| 1140 | |
| 1141 | /// A non-type template parameter declaration, 'int N'. |
| 1142 | class NonTypeTemplateParamDecl final : public Node { |
| 1143 | Node *Name; |
| 1144 | Node *Type; |
| 1145 | |
| 1146 | public: |
| 1147 | NonTypeTemplateParamDecl(Node *Name_, Node *Type_) |
| 1148 | : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {} |
| 1149 | |
| 1150 | template<typename Fn> void match(Fn F) const { F(Name, Type); } |
| 1151 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1152 | void printLeft(OutputBuffer &OB) const override { |
| 1153 | Type->printLeft(OB); |
| 1154 | if (!Type->hasRHSComponent(OB)) |
| 1155 | OB += " "; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1158 | void printRight(OutputBuffer &OB) const override { |
| 1159 | Name->print(OB); |
| 1160 | Type->printRight(OB); |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1161 | } |
| 1162 | }; |
| 1163 | |
| 1164 | /// A template template parameter declaration, |
| 1165 | /// 'template<typename T> typename N'. |
| 1166 | class TemplateTemplateParamDecl final : public Node { |
| 1167 | Node *Name; |
| 1168 | NodeArray Params; |
| 1169 | |
| 1170 | public: |
| 1171 | TemplateTemplateParamDecl(Node *Name_, NodeArray Params_) |
| 1172 | : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_), |
| 1173 | Params(Params_) {} |
| 1174 | |
| 1175 | template<typename Fn> void match(Fn F) const { F(Name, Params); } |
| 1176 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1177 | void printLeft(OutputBuffer &OB) const override { |
| 1178 | OB += "template<"; |
| 1179 | Params.printWithComma(OB); |
| 1180 | OB += "> typename "; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1183 | void printRight(OutputBuffer &OB) const override { Name->print(OB); } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1184 | }; |
| 1185 | |
| 1186 | /// A template parameter pack declaration, 'typename ...T'. |
| 1187 | class TemplateParamPackDecl final : public Node { |
| 1188 | Node *Param; |
| 1189 | |
| 1190 | public: |
| 1191 | TemplateParamPackDecl(Node *Param_) |
| 1192 | : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {} |
| 1193 | |
| 1194 | template<typename Fn> void match(Fn F) const { F(Param); } |
| 1195 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1196 | void printLeft(OutputBuffer &OB) const override { |
| 1197 | Param->printLeft(OB); |
| 1198 | OB += "..."; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1201 | void printRight(OutputBuffer &OB) const override { Param->printRight(OB); } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1202 | }; |
| 1203 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1204 | /// An unexpanded parameter pack (either in the expression or type context). If |
| 1205 | /// this AST is correct, this node will have a ParameterPackExpansion node above |
| 1206 | /// it. |
| 1207 | /// |
| 1208 | /// This node is created when some <template-args> are found that apply to an |
| 1209 | /// <encoding>, and is stored in the TemplateParams table. In order for this to |
| 1210 | /// appear in the final AST, it has to referenced via a <template-param> (ie, |
| 1211 | /// T_). |
| 1212 | class ParameterPack final : public Node { |
| 1213 | NodeArray Data; |
| 1214 | |
Nathan Sidwell | fd0ef6d | 2022-01-20 07:40:12 -0800 | [diff] [blame] | 1215 | // Setup OutputBuffer for a pack expansion, unless we're already expanding |
| 1216 | // one. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1217 | void initializePackExpansion(OutputBuffer &OB) const { |
| 1218 | if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) { |
| 1219 | OB.CurrentPackMax = static_cast<unsigned>(Data.size()); |
| 1220 | OB.CurrentPackIndex = 0; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | public: |
| 1225 | ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) { |
| 1226 | ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown; |
| 1227 | if (std::all_of(Data.begin(), Data.end(), [](Node* P) { |
| 1228 | return P->ArrayCache == Cache::No; |
| 1229 | })) |
| 1230 | ArrayCache = Cache::No; |
| 1231 | if (std::all_of(Data.begin(), Data.end(), [](Node* P) { |
| 1232 | return P->FunctionCache == Cache::No; |
| 1233 | })) |
| 1234 | FunctionCache = Cache::No; |
| 1235 | if (std::all_of(Data.begin(), Data.end(), [](Node* P) { |
| 1236 | return P->RHSComponentCache == Cache::No; |
| 1237 | })) |
| 1238 | RHSComponentCache = Cache::No; |
| 1239 | } |
| 1240 | |
| 1241 | template<typename Fn> void match(Fn F) const { F(Data); } |
| 1242 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1243 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
| 1244 | initializePackExpansion(OB); |
| 1245 | size_t Idx = OB.CurrentPackIndex; |
| 1246 | return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1247 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1248 | bool hasArraySlow(OutputBuffer &OB) const override { |
| 1249 | initializePackExpansion(OB); |
| 1250 | size_t Idx = OB.CurrentPackIndex; |
| 1251 | return Idx < Data.size() && Data[Idx]->hasArray(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1252 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1253 | bool hasFunctionSlow(OutputBuffer &OB) const override { |
| 1254 | initializePackExpansion(OB); |
| 1255 | size_t Idx = OB.CurrentPackIndex; |
| 1256 | return Idx < Data.size() && Data[Idx]->hasFunction(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1257 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1258 | const Node *getSyntaxNode(OutputBuffer &OB) const override { |
| 1259 | initializePackExpansion(OB); |
| 1260 | size_t Idx = OB.CurrentPackIndex; |
| 1261 | return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1264 | void printLeft(OutputBuffer &OB) const override { |
| 1265 | initializePackExpansion(OB); |
| 1266 | size_t Idx = OB.CurrentPackIndex; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1267 | if (Idx < Data.size()) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1268 | Data[Idx]->printLeft(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1269 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1270 | void printRight(OutputBuffer &OB) const override { |
| 1271 | initializePackExpansion(OB); |
| 1272 | size_t Idx = OB.CurrentPackIndex; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1273 | if (Idx < Data.size()) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1274 | Data[Idx]->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1275 | } |
| 1276 | }; |
| 1277 | |
| 1278 | /// A variadic template argument. This node represents an occurrence of |
| 1279 | /// J<something>E in some <template-args>. It isn't itself unexpanded, unless |
| 1280 | /// one of it's Elements is. The parser inserts a ParameterPack into the |
| 1281 | /// TemplateParams table if the <template-args> this pack belongs to apply to an |
| 1282 | /// <encoding>. |
| 1283 | class TemplateArgumentPack final : public Node { |
| 1284 | NodeArray Elements; |
| 1285 | public: |
| 1286 | TemplateArgumentPack(NodeArray Elements_) |
| 1287 | : Node(KTemplateArgumentPack), Elements(Elements_) {} |
| 1288 | |
| 1289 | template<typename Fn> void match(Fn F) const { F(Elements); } |
| 1290 | |
| 1291 | NodeArray getElements() const { return Elements; } |
| 1292 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1293 | void printLeft(OutputBuffer &OB) const override { |
| 1294 | Elements.printWithComma(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1295 | } |
| 1296 | }; |
| 1297 | |
| 1298 | /// A pack expansion. Below this node, there are some unexpanded ParameterPacks |
| 1299 | /// which each have Child->ParameterPackSize elements. |
| 1300 | class ParameterPackExpansion final : public Node { |
| 1301 | const Node *Child; |
| 1302 | |
| 1303 | public: |
| 1304 | ParameterPackExpansion(const Node *Child_) |
| 1305 | : Node(KParameterPackExpansion), Child(Child_) {} |
| 1306 | |
| 1307 | template<typename Fn> void match(Fn F) const { F(Child); } |
| 1308 | |
| 1309 | const Node *getChild() const { return Child; } |
| 1310 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1311 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1312 | constexpr unsigned Max = std::numeric_limits<unsigned>::max(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1313 | SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max); |
| 1314 | SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max); |
| 1315 | size_t StreamPos = OB.getCurrentPosition(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1316 | |
| 1317 | // Print the first element in the pack. If Child contains a ParameterPack, |
| 1318 | // it will set up S.CurrentPackMax and print the first element. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1319 | Child->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1320 | |
| 1321 | // No ParameterPack was found in Child. This can occur if we've found a pack |
| 1322 | // expansion on a <function-param>. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1323 | if (OB.CurrentPackMax == Max) { |
| 1324 | OB += "..."; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1325 | return; |
| 1326 | } |
| 1327 | |
| 1328 | // We found a ParameterPack, but it has no elements. Erase whatever we may |
| 1329 | // of printed. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1330 | if (OB.CurrentPackMax == 0) { |
| 1331 | OB.setCurrentPosition(StreamPos); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1332 | return; |
| 1333 | } |
| 1334 | |
| 1335 | // Else, iterate through the rest of the elements in the pack. |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1336 | for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) { |
| 1337 | OB += ", "; |
| 1338 | OB.CurrentPackIndex = I; |
| 1339 | Child->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | }; |
| 1343 | |
| 1344 | class TemplateArgs final : public Node { |
| 1345 | NodeArray Params; |
| 1346 | |
| 1347 | public: |
| 1348 | TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {} |
| 1349 | |
| 1350 | template<typename Fn> void match(Fn F) const { F(Params); } |
| 1351 | |
| 1352 | NodeArray getParams() { return Params; } |
| 1353 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1354 | void printLeft(OutputBuffer &OB) const override { |
| 1355 | OB += "<"; |
| 1356 | Params.printWithComma(OB); |
| 1357 | if (OB.back() == '>') |
| 1358 | OB += " "; |
| 1359 | OB += ">"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1360 | } |
| 1361 | }; |
| 1362 | |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 1363 | /// A forward-reference to a template argument that was not known at the point |
| 1364 | /// where the template parameter name was parsed in a mangling. |
| 1365 | /// |
| 1366 | /// This is created when demangling the name of a specialization of a |
| 1367 | /// conversion function template: |
| 1368 | /// |
| 1369 | /// \code |
| 1370 | /// struct A { |
| 1371 | /// template<typename T> operator T*(); |
| 1372 | /// }; |
| 1373 | /// \endcode |
| 1374 | /// |
| 1375 | /// When demangling a specialization of the conversion function template, we |
| 1376 | /// encounter the name of the template (including the \c T) before we reach |
| 1377 | /// the template argument list, so we cannot substitute the parameter name |
| 1378 | /// for the corresponding argument while parsing. Instead, we create a |
| 1379 | /// \c ForwardTemplateReference node that is resolved after we parse the |
| 1380 | /// template arguments. |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1381 | struct ForwardTemplateReference : Node { |
| 1382 | size_t Index; |
| 1383 | Node *Ref = nullptr; |
| 1384 | |
| 1385 | // If we're currently printing this node. It is possible (though invalid) for |
| 1386 | // a forward template reference to refer to itself via a substitution. This |
| 1387 | // creates a cyclic AST, which will stack overflow printing. To fix this, bail |
| 1388 | // out if more than one print* function is active. |
| 1389 | mutable bool Printing = false; |
| 1390 | |
| 1391 | ForwardTemplateReference(size_t Index_) |
| 1392 | : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown, |
| 1393 | Cache::Unknown), |
| 1394 | Index(Index_) {} |
| 1395 | |
| 1396 | // We don't provide a matcher for these, because the value of the node is |
| 1397 | // not determined by its construction parameters, and it generally needs |
| 1398 | // special handling. |
| 1399 | template<typename Fn> void match(Fn F) const = delete; |
| 1400 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1401 | bool hasRHSComponentSlow(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1402 | if (Printing) |
| 1403 | return false; |
| 1404 | SwapAndRestore<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1405 | return Ref->hasRHSComponent(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1406 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1407 | bool hasArraySlow(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1408 | if (Printing) |
| 1409 | return false; |
| 1410 | SwapAndRestore<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1411 | return Ref->hasArray(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1412 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1413 | bool hasFunctionSlow(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1414 | if (Printing) |
| 1415 | return false; |
| 1416 | SwapAndRestore<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1417 | return Ref->hasFunction(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1418 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1419 | const Node *getSyntaxNode(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1420 | if (Printing) |
| 1421 | return this; |
| 1422 | SwapAndRestore<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1423 | return Ref->getSyntaxNode(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1426 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1427 | if (Printing) |
| 1428 | return; |
| 1429 | SwapAndRestore<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1430 | Ref->printLeft(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1431 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1432 | void printRight(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1433 | if (Printing) |
| 1434 | return; |
| 1435 | SwapAndRestore<bool> SavePrinting(Printing, true); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1436 | Ref->printRight(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1437 | } |
| 1438 | }; |
| 1439 | |
| 1440 | struct NameWithTemplateArgs : Node { |
| 1441 | // name<template_args> |
| 1442 | Node *Name; |
| 1443 | Node *TemplateArgs; |
| 1444 | |
| 1445 | NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_) |
| 1446 | : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {} |
| 1447 | |
| 1448 | template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); } |
| 1449 | |
| 1450 | StringView getBaseName() const override { return Name->getBaseName(); } |
| 1451 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1452 | void printLeft(OutputBuffer &OB) const override { |
| 1453 | Name->print(OB); |
| 1454 | TemplateArgs->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1455 | } |
| 1456 | }; |
| 1457 | |
| 1458 | class GlobalQualifiedName final : public Node { |
| 1459 | Node *Child; |
| 1460 | |
| 1461 | public: |
| 1462 | GlobalQualifiedName(Node* Child_) |
| 1463 | : Node(KGlobalQualifiedName), Child(Child_) {} |
| 1464 | |
| 1465 | template<typename Fn> void match(Fn F) const { F(Child); } |
| 1466 | |
| 1467 | StringView getBaseName() const override { return Child->getBaseName(); } |
| 1468 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1469 | void printLeft(OutputBuffer &OB) const override { |
| 1470 | OB += "::"; |
| 1471 | Child->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1472 | } |
| 1473 | }; |
| 1474 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1475 | enum class SpecialSubKind { |
| 1476 | allocator, |
| 1477 | basic_string, |
| 1478 | string, |
| 1479 | istream, |
| 1480 | ostream, |
| 1481 | iostream, |
| 1482 | }; |
| 1483 | |
| 1484 | class ExpandedSpecialSubstitution final : public Node { |
| 1485 | SpecialSubKind SSK; |
| 1486 | |
| 1487 | public: |
| 1488 | ExpandedSpecialSubstitution(SpecialSubKind SSK_) |
| 1489 | : Node(KExpandedSpecialSubstitution), SSK(SSK_) {} |
| 1490 | |
| 1491 | template<typename Fn> void match(Fn F) const { F(SSK); } |
| 1492 | |
| 1493 | StringView getBaseName() const override { |
| 1494 | switch (SSK) { |
| 1495 | case SpecialSubKind::allocator: |
| 1496 | return StringView("allocator"); |
| 1497 | case SpecialSubKind::basic_string: |
| 1498 | return StringView("basic_string"); |
| 1499 | case SpecialSubKind::string: |
| 1500 | return StringView("basic_string"); |
| 1501 | case SpecialSubKind::istream: |
| 1502 | return StringView("basic_istream"); |
| 1503 | case SpecialSubKind::ostream: |
| 1504 | return StringView("basic_ostream"); |
| 1505 | case SpecialSubKind::iostream: |
| 1506 | return StringView("basic_iostream"); |
| 1507 | } |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 1508 | DEMANGLE_UNREACHABLE; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1511 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1512 | switch (SSK) { |
| 1513 | case SpecialSubKind::allocator: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1514 | OB += "std::allocator"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1515 | break; |
| 1516 | case SpecialSubKind::basic_string: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1517 | OB += "std::basic_string"; |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 1518 | break; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1519 | case SpecialSubKind::string: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1520 | OB += "std::basic_string<char, std::char_traits<char>, " |
| 1521 | "std::allocator<char> >"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1522 | break; |
| 1523 | case SpecialSubKind::istream: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1524 | OB += "std::basic_istream<char, std::char_traits<char> >"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1525 | break; |
| 1526 | case SpecialSubKind::ostream: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1527 | OB += "std::basic_ostream<char, std::char_traits<char> >"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1528 | break; |
| 1529 | case SpecialSubKind::iostream: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1530 | OB += "std::basic_iostream<char, std::char_traits<char> >"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1531 | break; |
| 1532 | } |
| 1533 | } |
| 1534 | }; |
| 1535 | |
| 1536 | class SpecialSubstitution final : public Node { |
| 1537 | public: |
| 1538 | SpecialSubKind SSK; |
| 1539 | |
| 1540 | SpecialSubstitution(SpecialSubKind SSK_) |
| 1541 | : Node(KSpecialSubstitution), SSK(SSK_) {} |
| 1542 | |
| 1543 | template<typename Fn> void match(Fn F) const { F(SSK); } |
| 1544 | |
| 1545 | StringView getBaseName() const override { |
| 1546 | switch (SSK) { |
| 1547 | case SpecialSubKind::allocator: |
| 1548 | return StringView("allocator"); |
| 1549 | case SpecialSubKind::basic_string: |
| 1550 | return StringView("basic_string"); |
| 1551 | case SpecialSubKind::string: |
| 1552 | return StringView("string"); |
| 1553 | case SpecialSubKind::istream: |
| 1554 | return StringView("istream"); |
| 1555 | case SpecialSubKind::ostream: |
| 1556 | return StringView("ostream"); |
| 1557 | case SpecialSubKind::iostream: |
| 1558 | return StringView("iostream"); |
| 1559 | } |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 1560 | DEMANGLE_UNREACHABLE; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1563 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1564 | switch (SSK) { |
| 1565 | case SpecialSubKind::allocator: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1566 | OB += "std::allocator"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1567 | break; |
| 1568 | case SpecialSubKind::basic_string: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1569 | OB += "std::basic_string"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1570 | break; |
| 1571 | case SpecialSubKind::string: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1572 | OB += "std::string"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1573 | break; |
| 1574 | case SpecialSubKind::istream: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1575 | OB += "std::istream"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1576 | break; |
| 1577 | case SpecialSubKind::ostream: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1578 | OB += "std::ostream"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1579 | break; |
| 1580 | case SpecialSubKind::iostream: |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1581 | OB += "std::iostream"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1582 | break; |
| 1583 | } |
| 1584 | } |
| 1585 | }; |
| 1586 | |
| 1587 | class CtorDtorName final : public Node { |
| 1588 | const Node *Basename; |
| 1589 | const bool IsDtor; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 1590 | const int Variant; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1591 | |
| 1592 | public: |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 1593 | CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_) |
| 1594 | : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_), |
| 1595 | Variant(Variant_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1596 | |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 1597 | template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1598 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1599 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1600 | if (IsDtor) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1601 | OB += "~"; |
| 1602 | OB += Basename->getBaseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1603 | } |
| 1604 | }; |
| 1605 | |
| 1606 | class DtorName : public Node { |
| 1607 | const Node *Base; |
| 1608 | |
| 1609 | public: |
| 1610 | DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {} |
| 1611 | |
| 1612 | template<typename Fn> void match(Fn F) const { F(Base); } |
| 1613 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1614 | void printLeft(OutputBuffer &OB) const override { |
| 1615 | OB += "~"; |
| 1616 | Base->printLeft(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1617 | } |
| 1618 | }; |
| 1619 | |
| 1620 | class UnnamedTypeName : public Node { |
| 1621 | const StringView Count; |
| 1622 | |
| 1623 | public: |
| 1624 | UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {} |
| 1625 | |
| 1626 | template<typename Fn> void match(Fn F) const { F(Count); } |
| 1627 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1628 | void printLeft(OutputBuffer &OB) const override { |
| 1629 | OB += "'unnamed"; |
| 1630 | OB += Count; |
| 1631 | OB += "\'"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1632 | } |
| 1633 | }; |
| 1634 | |
| 1635 | class ClosureTypeName : public Node { |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1636 | NodeArray TemplateParams; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1637 | NodeArray Params; |
| 1638 | StringView Count; |
| 1639 | |
| 1640 | public: |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1641 | ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_, |
| 1642 | StringView Count_) |
| 1643 | : Node(KClosureTypeName), TemplateParams(TemplateParams_), |
| 1644 | Params(Params_), Count(Count_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1645 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1646 | template<typename Fn> void match(Fn F) const { |
| 1647 | F(TemplateParams, Params, Count); |
| 1648 | } |
| 1649 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1650 | void printDeclarator(OutputBuffer &OB) const { |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1651 | if (!TemplateParams.empty()) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1652 | OB += "<"; |
| 1653 | TemplateParams.printWithComma(OB); |
| 1654 | OB += ">"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1655 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1656 | OB += "("; |
| 1657 | Params.printWithComma(OB); |
| 1658 | OB += ")"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 1659 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1660 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1661 | void printLeft(OutputBuffer &OB) const override { |
| 1662 | OB += "\'lambda"; |
| 1663 | OB += Count; |
| 1664 | OB += "\'"; |
| 1665 | printDeclarator(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1666 | } |
| 1667 | }; |
| 1668 | |
| 1669 | class StructuredBindingName : public Node { |
| 1670 | NodeArray Bindings; |
| 1671 | public: |
| 1672 | StructuredBindingName(NodeArray Bindings_) |
| 1673 | : Node(KStructuredBindingName), Bindings(Bindings_) {} |
| 1674 | |
| 1675 | template<typename Fn> void match(Fn F) const { F(Bindings); } |
| 1676 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1677 | void printLeft(OutputBuffer &OB) const override { |
| 1678 | OB += '['; |
| 1679 | Bindings.printWithComma(OB); |
| 1680 | OB += ']'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1681 | } |
| 1682 | }; |
| 1683 | |
| 1684 | // -- Expression Nodes -- |
| 1685 | |
| 1686 | class BinaryExpr : public Node { |
| 1687 | const Node *LHS; |
| 1688 | const StringView InfixOperator; |
| 1689 | const Node *RHS; |
| 1690 | |
| 1691 | public: |
| 1692 | BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_) |
| 1693 | : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) { |
| 1694 | } |
| 1695 | |
| 1696 | template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); } |
| 1697 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1698 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1699 | // might be a template argument expression, then we need to disambiguate |
| 1700 | // with parens. |
| 1701 | if (InfixOperator == ">") |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1702 | OB += "("; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1703 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1704 | OB += "("; |
| 1705 | LHS->print(OB); |
| 1706 | OB += ") "; |
| 1707 | OB += InfixOperator; |
| 1708 | OB += " ("; |
| 1709 | RHS->print(OB); |
| 1710 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1711 | |
| 1712 | if (InfixOperator == ">") |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1713 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1714 | } |
| 1715 | }; |
| 1716 | |
| 1717 | class ArraySubscriptExpr : public Node { |
| 1718 | const Node *Op1; |
| 1719 | const Node *Op2; |
| 1720 | |
| 1721 | public: |
| 1722 | ArraySubscriptExpr(const Node *Op1_, const Node *Op2_) |
| 1723 | : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {} |
| 1724 | |
| 1725 | template<typename Fn> void match(Fn F) const { F(Op1, Op2); } |
| 1726 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1727 | void printLeft(OutputBuffer &OB) const override { |
| 1728 | OB += "("; |
| 1729 | Op1->print(OB); |
| 1730 | OB += ")["; |
| 1731 | Op2->print(OB); |
| 1732 | OB += "]"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1733 | } |
| 1734 | }; |
| 1735 | |
| 1736 | class PostfixExpr : public Node { |
| 1737 | const Node *Child; |
| 1738 | const StringView Operator; |
| 1739 | |
| 1740 | public: |
| 1741 | PostfixExpr(const Node *Child_, StringView Operator_) |
| 1742 | : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {} |
| 1743 | |
| 1744 | template<typename Fn> void match(Fn F) const { F(Child, Operator); } |
| 1745 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1746 | void printLeft(OutputBuffer &OB) const override { |
| 1747 | OB += "("; |
| 1748 | Child->print(OB); |
| 1749 | OB += ")"; |
| 1750 | OB += Operator; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1751 | } |
| 1752 | }; |
| 1753 | |
| 1754 | class ConditionalExpr : public Node { |
| 1755 | const Node *Cond; |
| 1756 | const Node *Then; |
| 1757 | const Node *Else; |
| 1758 | |
| 1759 | public: |
| 1760 | ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_) |
| 1761 | : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {} |
| 1762 | |
| 1763 | template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); } |
| 1764 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1765 | void printLeft(OutputBuffer &OB) const override { |
| 1766 | OB += "("; |
| 1767 | Cond->print(OB); |
| 1768 | OB += ") ? ("; |
| 1769 | Then->print(OB); |
| 1770 | OB += ") : ("; |
| 1771 | Else->print(OB); |
| 1772 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1773 | } |
| 1774 | }; |
| 1775 | |
| 1776 | class MemberExpr : public Node { |
| 1777 | const Node *LHS; |
| 1778 | const StringView Kind; |
| 1779 | const Node *RHS; |
| 1780 | |
| 1781 | public: |
| 1782 | MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_) |
| 1783 | : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {} |
| 1784 | |
| 1785 | template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); } |
| 1786 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1787 | void printLeft(OutputBuffer &OB) const override { |
| 1788 | LHS->print(OB); |
| 1789 | OB += Kind; |
Nathan Sidwell | c648304 | 2022-01-28 09:27:28 -0800 | [diff] [blame] | 1790 | // Parenthesize pointer-to-member deference argument. |
| 1791 | bool IsPtr = Kind.back() == '*'; |
| 1792 | if (IsPtr) |
| 1793 | OB += '('; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1794 | RHS->print(OB); |
Nathan Sidwell | c648304 | 2022-01-28 09:27:28 -0800 | [diff] [blame] | 1795 | if (IsPtr) |
| 1796 | OB += ')'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1797 | } |
| 1798 | }; |
| 1799 | |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1800 | class SubobjectExpr : public Node { |
| 1801 | const Node *Type; |
| 1802 | const Node *SubExpr; |
| 1803 | StringView Offset; |
| 1804 | NodeArray UnionSelectors; |
| 1805 | bool OnePastTheEnd; |
| 1806 | |
| 1807 | public: |
| 1808 | SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_, |
| 1809 | NodeArray UnionSelectors_, bool OnePastTheEnd_) |
| 1810 | : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_), |
| 1811 | UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {} |
| 1812 | |
| 1813 | template<typename Fn> void match(Fn F) const { |
| 1814 | F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd); |
| 1815 | } |
| 1816 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1817 | void printLeft(OutputBuffer &OB) const override { |
| 1818 | SubExpr->print(OB); |
| 1819 | OB += ".<"; |
| 1820 | Type->print(OB); |
| 1821 | OB += " at offset "; |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1822 | if (Offset.empty()) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1823 | OB += "0"; |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1824 | } else if (Offset[0] == 'n') { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1825 | OB += "-"; |
| 1826 | OB += Offset.dropFront(); |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1827 | } else { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1828 | OB += Offset; |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1829 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1830 | OB += ">"; |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 1831 | } |
| 1832 | }; |
| 1833 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1834 | class EnclosingExpr : public Node { |
| 1835 | const StringView Prefix; |
| 1836 | const Node *Infix; |
| 1837 | const StringView Postfix; |
| 1838 | |
| 1839 | public: |
| 1840 | EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_) |
| 1841 | : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_), |
| 1842 | Postfix(Postfix_) {} |
| 1843 | |
| 1844 | template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); } |
| 1845 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1846 | void printLeft(OutputBuffer &OB) const override { |
| 1847 | OB += Prefix; |
| 1848 | Infix->print(OB); |
| 1849 | OB += Postfix; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1850 | } |
| 1851 | }; |
| 1852 | |
| 1853 | class CastExpr : public Node { |
| 1854 | // cast_kind<to>(from) |
| 1855 | const StringView CastKind; |
| 1856 | const Node *To; |
| 1857 | const Node *From; |
| 1858 | |
| 1859 | public: |
| 1860 | CastExpr(StringView CastKind_, const Node *To_, const Node *From_) |
| 1861 | : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {} |
| 1862 | |
| 1863 | template<typename Fn> void match(Fn F) const { F(CastKind, To, From); } |
| 1864 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1865 | void printLeft(OutputBuffer &OB) const override { |
| 1866 | OB += CastKind; |
| 1867 | OB += "<"; |
| 1868 | To->printLeft(OB); |
| 1869 | OB += ">("; |
| 1870 | From->printLeft(OB); |
| 1871 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1872 | } |
| 1873 | }; |
| 1874 | |
| 1875 | class SizeofParamPackExpr : public Node { |
| 1876 | const Node *Pack; |
| 1877 | |
| 1878 | public: |
| 1879 | SizeofParamPackExpr(const Node *Pack_) |
| 1880 | : Node(KSizeofParamPackExpr), Pack(Pack_) {} |
| 1881 | |
| 1882 | template<typename Fn> void match(Fn F) const { F(Pack); } |
| 1883 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1884 | void printLeft(OutputBuffer &OB) const override { |
| 1885 | OB += "sizeof...("; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1886 | ParameterPackExpansion PPE(Pack); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1887 | PPE.printLeft(OB); |
| 1888 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1889 | } |
| 1890 | }; |
| 1891 | |
| 1892 | class CallExpr : public Node { |
| 1893 | const Node *Callee; |
| 1894 | NodeArray Args; |
| 1895 | |
| 1896 | public: |
| 1897 | CallExpr(const Node *Callee_, NodeArray Args_) |
| 1898 | : Node(KCallExpr), Callee(Callee_), Args(Args_) {} |
| 1899 | |
| 1900 | template<typename Fn> void match(Fn F) const { F(Callee, Args); } |
| 1901 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1902 | void printLeft(OutputBuffer &OB) const override { |
| 1903 | Callee->print(OB); |
| 1904 | OB += "("; |
| 1905 | Args.printWithComma(OB); |
| 1906 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1907 | } |
| 1908 | }; |
| 1909 | |
| 1910 | class NewExpr : public Node { |
| 1911 | // new (expr_list) type(init_list) |
| 1912 | NodeArray ExprList; |
| 1913 | Node *Type; |
| 1914 | NodeArray InitList; |
| 1915 | bool IsGlobal; // ::operator new ? |
| 1916 | bool IsArray; // new[] ? |
| 1917 | public: |
| 1918 | NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_, |
| 1919 | bool IsArray_) |
| 1920 | : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_), |
| 1921 | IsGlobal(IsGlobal_), IsArray(IsArray_) {} |
| 1922 | |
| 1923 | template<typename Fn> void match(Fn F) const { |
| 1924 | F(ExprList, Type, InitList, IsGlobal, IsArray); |
| 1925 | } |
| 1926 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1927 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1928 | if (IsGlobal) |
Nathan Sidwell | c69bde2 | 2022-01-28 07:09:38 -0800 | [diff] [blame] | 1929 | OB += "::"; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1930 | OB += "new"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1931 | if (IsArray) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1932 | OB += "[]"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1933 | if (!ExprList.empty()) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1934 | OB += "("; |
| 1935 | ExprList.printWithComma(OB); |
| 1936 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1937 | } |
Nathan Sidwell | c69bde2 | 2022-01-28 07:09:38 -0800 | [diff] [blame] | 1938 | OB += ' '; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1939 | Type->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1940 | if (!InitList.empty()) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1941 | OB += "("; |
| 1942 | InitList.printWithComma(OB); |
| 1943 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1944 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1945 | } |
| 1946 | }; |
| 1947 | |
| 1948 | class DeleteExpr : public Node { |
| 1949 | Node *Op; |
| 1950 | bool IsGlobal; |
| 1951 | bool IsArray; |
| 1952 | |
| 1953 | public: |
| 1954 | DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_) |
| 1955 | : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {} |
| 1956 | |
| 1957 | template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); } |
| 1958 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1959 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1960 | if (IsGlobal) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1961 | OB += "::"; |
| 1962 | OB += "delete"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1963 | if (IsArray) |
Nathan Sidwell | c69bde2 | 2022-01-28 07:09:38 -0800 | [diff] [blame] | 1964 | OB += "[]"; |
| 1965 | OB += ' '; |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1966 | Op->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1967 | } |
| 1968 | }; |
| 1969 | |
| 1970 | class PrefixExpr : public Node { |
| 1971 | StringView Prefix; |
| 1972 | Node *Child; |
| 1973 | |
| 1974 | public: |
| 1975 | PrefixExpr(StringView Prefix_, Node *Child_) |
| 1976 | : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {} |
| 1977 | |
| 1978 | template<typename Fn> void match(Fn F) const { F(Prefix, Child); } |
| 1979 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1980 | void printLeft(OutputBuffer &OB) const override { |
| 1981 | OB += Prefix; |
| 1982 | OB += "("; |
| 1983 | Child->print(OB); |
| 1984 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1985 | } |
| 1986 | }; |
| 1987 | |
| 1988 | class FunctionParam : public Node { |
| 1989 | StringView Number; |
| 1990 | |
| 1991 | public: |
| 1992 | FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {} |
| 1993 | |
| 1994 | template<typename Fn> void match(Fn F) const { F(Number); } |
| 1995 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 1996 | void printLeft(OutputBuffer &OB) const override { |
| 1997 | OB += "fp"; |
| 1998 | OB += Number; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 1999 | } |
| 2000 | }; |
| 2001 | |
| 2002 | class ConversionExpr : public Node { |
| 2003 | const Node *Type; |
| 2004 | NodeArray Expressions; |
| 2005 | |
| 2006 | public: |
| 2007 | ConversionExpr(const Node *Type_, NodeArray Expressions_) |
| 2008 | : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {} |
| 2009 | |
| 2010 | template<typename Fn> void match(Fn F) const { F(Type, Expressions); } |
| 2011 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2012 | void printLeft(OutputBuffer &OB) const override { |
| 2013 | OB += "("; |
| 2014 | Type->print(OB); |
| 2015 | OB += ")("; |
| 2016 | Expressions.printWithComma(OB); |
| 2017 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2018 | } |
| 2019 | }; |
| 2020 | |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 2021 | class PointerToMemberConversionExpr : public Node { |
| 2022 | const Node *Type; |
| 2023 | const Node *SubExpr; |
| 2024 | StringView Offset; |
| 2025 | |
| 2026 | public: |
| 2027 | PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_, |
| 2028 | StringView Offset_) |
| 2029 | : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_), |
| 2030 | Offset(Offset_) {} |
| 2031 | |
| 2032 | template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); } |
| 2033 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2034 | void printLeft(OutputBuffer &OB) const override { |
| 2035 | OB += "("; |
| 2036 | Type->print(OB); |
| 2037 | OB += ")("; |
| 2038 | SubExpr->print(OB); |
| 2039 | OB += ")"; |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 2040 | } |
| 2041 | }; |
| 2042 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2043 | class InitListExpr : public Node { |
| 2044 | const Node *Ty; |
| 2045 | NodeArray Inits; |
| 2046 | public: |
| 2047 | InitListExpr(const Node *Ty_, NodeArray Inits_) |
| 2048 | : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {} |
| 2049 | |
| 2050 | template<typename Fn> void match(Fn F) const { F(Ty, Inits); } |
| 2051 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2052 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2053 | if (Ty) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2054 | Ty->print(OB); |
| 2055 | OB += '{'; |
| 2056 | Inits.printWithComma(OB); |
| 2057 | OB += '}'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2058 | } |
| 2059 | }; |
| 2060 | |
| 2061 | class BracedExpr : public Node { |
| 2062 | const Node *Elem; |
| 2063 | const Node *Init; |
| 2064 | bool IsArray; |
| 2065 | public: |
| 2066 | BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_) |
| 2067 | : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {} |
| 2068 | |
| 2069 | template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); } |
| 2070 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2071 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2072 | if (IsArray) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2073 | OB += '['; |
| 2074 | Elem->print(OB); |
| 2075 | OB += ']'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2076 | } else { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2077 | OB += '.'; |
| 2078 | Elem->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2079 | } |
| 2080 | if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2081 | OB += " = "; |
| 2082 | Init->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2083 | } |
| 2084 | }; |
| 2085 | |
| 2086 | class BracedRangeExpr : public Node { |
| 2087 | const Node *First; |
| 2088 | const Node *Last; |
| 2089 | const Node *Init; |
| 2090 | public: |
| 2091 | BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_) |
| 2092 | : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {} |
| 2093 | |
| 2094 | template<typename Fn> void match(Fn F) const { F(First, Last, Init); } |
| 2095 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2096 | void printLeft(OutputBuffer &OB) const override { |
| 2097 | OB += '['; |
| 2098 | First->print(OB); |
| 2099 | OB += " ... "; |
| 2100 | Last->print(OB); |
| 2101 | OB += ']'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2102 | if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2103 | OB += " = "; |
| 2104 | Init->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2105 | } |
| 2106 | }; |
| 2107 | |
| 2108 | class FoldExpr : public Node { |
| 2109 | const Node *Pack, *Init; |
| 2110 | StringView OperatorName; |
| 2111 | bool IsLeftFold; |
| 2112 | |
| 2113 | public: |
| 2114 | FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_, |
| 2115 | const Node *Init_) |
| 2116 | : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_), |
| 2117 | IsLeftFold(IsLeftFold_) {} |
| 2118 | |
| 2119 | template<typename Fn> void match(Fn F) const { |
| 2120 | F(IsLeftFold, OperatorName, Pack, Init); |
| 2121 | } |
| 2122 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2123 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2124 | auto PrintPack = [&] { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2125 | OB += '('; |
| 2126 | ParameterPackExpansion(Pack).print(OB); |
| 2127 | OB += ')'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2128 | }; |
| 2129 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2130 | OB += '('; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2131 | |
| 2132 | if (IsLeftFold) { |
| 2133 | // init op ... op pack |
| 2134 | if (Init != nullptr) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2135 | Init->print(OB); |
| 2136 | OB += ' '; |
| 2137 | OB += OperatorName; |
| 2138 | OB += ' '; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2139 | } |
| 2140 | // ... op pack |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2141 | OB += "... "; |
| 2142 | OB += OperatorName; |
| 2143 | OB += ' '; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2144 | PrintPack(); |
| 2145 | } else { // !IsLeftFold |
| 2146 | // pack op ... |
| 2147 | PrintPack(); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2148 | OB += ' '; |
| 2149 | OB += OperatorName; |
| 2150 | OB += " ..."; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2151 | // pack op ... op init |
| 2152 | if (Init != nullptr) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2153 | OB += ' '; |
| 2154 | OB += OperatorName; |
| 2155 | OB += ' '; |
| 2156 | Init->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2157 | } |
| 2158 | } |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2159 | OB += ')'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2160 | } |
| 2161 | }; |
| 2162 | |
| 2163 | class ThrowExpr : public Node { |
| 2164 | const Node *Op; |
| 2165 | |
| 2166 | public: |
| 2167 | ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {} |
| 2168 | |
| 2169 | template<typename Fn> void match(Fn F) const { F(Op); } |
| 2170 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2171 | void printLeft(OutputBuffer &OB) const override { |
| 2172 | OB += "throw "; |
| 2173 | Op->print(OB); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2174 | } |
| 2175 | }; |
| 2176 | |
| 2177 | class BoolExpr : public Node { |
| 2178 | bool Value; |
| 2179 | |
| 2180 | public: |
| 2181 | BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {} |
| 2182 | |
| 2183 | template<typename Fn> void match(Fn F) const { F(Value); } |
| 2184 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2185 | void printLeft(OutputBuffer &OB) const override { |
| 2186 | OB += Value ? StringView("true") : StringView("false"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2187 | } |
| 2188 | }; |
| 2189 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2190 | class StringLiteral : public Node { |
| 2191 | const Node *Type; |
| 2192 | |
| 2193 | public: |
| 2194 | StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {} |
| 2195 | |
| 2196 | template<typename Fn> void match(Fn F) const { F(Type); } |
| 2197 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2198 | void printLeft(OutputBuffer &OB) const override { |
| 2199 | OB += "\"<"; |
| 2200 | Type->print(OB); |
| 2201 | OB += ">\""; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2202 | } |
| 2203 | }; |
| 2204 | |
| 2205 | class LambdaExpr : public Node { |
| 2206 | const Node *Type; |
| 2207 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2208 | public: |
| 2209 | LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {} |
| 2210 | |
| 2211 | template<typename Fn> void match(Fn F) const { F(Type); } |
| 2212 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2213 | void printLeft(OutputBuffer &OB) const override { |
| 2214 | OB += "[]"; |
Richard Smith | fb91746 | 2019-09-09 22:26:04 +0000 | [diff] [blame] | 2215 | if (Type->getKind() == KClosureTypeName) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2216 | static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB); |
| 2217 | OB += "{...}"; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2218 | } |
| 2219 | }; |
| 2220 | |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 2221 | class EnumLiteral : public Node { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2222 | // ty(integer) |
| 2223 | const Node *Ty; |
| 2224 | StringView Integer; |
| 2225 | |
| 2226 | public: |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 2227 | EnumLiteral(const Node *Ty_, StringView Integer_) |
| 2228 | : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {} |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2229 | |
| 2230 | template<typename Fn> void match(Fn F) const { F(Ty, Integer); } |
| 2231 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2232 | void printLeft(OutputBuffer &OB) const override { |
| 2233 | OB << "("; |
| 2234 | Ty->print(OB); |
| 2235 | OB << ")"; |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 2236 | |
| 2237 | if (Integer[0] == 'n') |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2238 | OB << "-" << Integer.dropFront(1); |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 2239 | else |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2240 | OB << Integer; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2241 | } |
| 2242 | }; |
| 2243 | |
| 2244 | class IntegerLiteral : public Node { |
| 2245 | StringView Type; |
| 2246 | StringView Value; |
| 2247 | |
| 2248 | public: |
| 2249 | IntegerLiteral(StringView Type_, StringView Value_) |
| 2250 | : Node(KIntegerLiteral), Type(Type_), Value(Value_) {} |
| 2251 | |
| 2252 | template<typename Fn> void match(Fn F) const { F(Type, Value); } |
| 2253 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2254 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2255 | if (Type.size() > 3) { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2256 | OB += "("; |
| 2257 | OB += Type; |
| 2258 | OB += ")"; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | if (Value[0] == 'n') { |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2262 | OB += "-"; |
| 2263 | OB += Value.dropFront(1); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2264 | } else |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2265 | OB += Value; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2266 | |
| 2267 | if (Type.size() <= 3) |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2268 | OB += Type; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2269 | } |
| 2270 | }; |
| 2271 | |
| 2272 | template <class Float> struct FloatData; |
| 2273 | |
| 2274 | namespace float_literal_impl { |
| 2275 | constexpr Node::Kind getFloatLiteralKind(float *) { |
| 2276 | return Node::KFloatLiteral; |
| 2277 | } |
| 2278 | constexpr Node::Kind getFloatLiteralKind(double *) { |
| 2279 | return Node::KDoubleLiteral; |
| 2280 | } |
| 2281 | constexpr Node::Kind getFloatLiteralKind(long double *) { |
| 2282 | return Node::KLongDoubleLiteral; |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | template <class Float> class FloatLiteralImpl : public Node { |
| 2287 | const StringView Contents; |
| 2288 | |
| 2289 | static constexpr Kind KindForClass = |
| 2290 | float_literal_impl::getFloatLiteralKind((Float *)nullptr); |
| 2291 | |
| 2292 | public: |
| 2293 | FloatLiteralImpl(StringView Contents_) |
| 2294 | : Node(KindForClass), Contents(Contents_) {} |
| 2295 | |
| 2296 | template<typename Fn> void match(Fn F) const { F(Contents); } |
| 2297 | |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2298 | void printLeft(OutputBuffer &OB) const override { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2299 | const char *first = Contents.begin(); |
| 2300 | const char *last = Contents.end() + 1; |
| 2301 | |
| 2302 | const size_t N = FloatData<Float>::mangled_size; |
| 2303 | if (static_cast<std::size_t>(last - first) > N) { |
| 2304 | last = first + N; |
| 2305 | union { |
| 2306 | Float value; |
| 2307 | char buf[sizeof(Float)]; |
| 2308 | }; |
| 2309 | const char *t = first; |
| 2310 | char *e = buf; |
| 2311 | for (; t != last; ++t, ++e) { |
| 2312 | unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0') |
| 2313 | : static_cast<unsigned>(*t - 'a' + 10); |
| 2314 | ++t; |
| 2315 | unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0') |
| 2316 | : static_cast<unsigned>(*t - 'a' + 10); |
| 2317 | *e = static_cast<char>((d1 << 4) + d0); |
| 2318 | } |
| 2319 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 2320 | std::reverse(buf, e); |
| 2321 | #endif |
| 2322 | char num[FloatData<Float>::max_demangled_size] = {0}; |
| 2323 | int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value); |
LuÃs Ferreira | a4380e2 | 2021-10-21 17:31:53 -0700 | [diff] [blame] | 2324 | OB += StringView(num, num + n); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2325 | } |
| 2326 | } |
| 2327 | }; |
| 2328 | |
| 2329 | using FloatLiteral = FloatLiteralImpl<float>; |
| 2330 | using DoubleLiteral = FloatLiteralImpl<double>; |
| 2331 | using LongDoubleLiteral = FloatLiteralImpl<long double>; |
| 2332 | |
| 2333 | /// Visit the node. Calls \c F(P), where \c P is the node cast to the |
| 2334 | /// appropriate derived class. |
| 2335 | template<typename Fn> |
| 2336 | void Node::visit(Fn F) const { |
| 2337 | switch (K) { |
| 2338 | #define CASE(X) case K ## X: return F(static_cast<const X*>(this)); |
| 2339 | FOR_EACH_NODE_KIND(CASE) |
| 2340 | #undef CASE |
| 2341 | } |
| 2342 | assert(0 && "unknown mangling node kind"); |
| 2343 | } |
| 2344 | |
| 2345 | /// Determine the kind of a node from its type. |
| 2346 | template<typename NodeT> struct NodeKind; |
| 2347 | #define SPECIALIZATION(X) \ |
| 2348 | template<> struct NodeKind<X> { \ |
| 2349 | static constexpr Node::Kind Kind = Node::K##X; \ |
| 2350 | static constexpr const char *name() { return #X; } \ |
| 2351 | }; |
| 2352 | FOR_EACH_NODE_KIND(SPECIALIZATION) |
| 2353 | #undef SPECIALIZATION |
| 2354 | |
| 2355 | #undef FOR_EACH_NODE_KIND |
| 2356 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2357 | template <typename Derived, typename Alloc> struct AbstractManglingParser { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2358 | const char *First; |
| 2359 | const char *Last; |
| 2360 | |
| 2361 | // Name stack, this is used by the parser to hold temporary names that were |
| 2362 | // parsed. The parser collapses multiple names into new nodes to construct |
| 2363 | // the AST. Once the parser is finished, names.size() == 1. |
| 2364 | PODSmallVector<Node *, 32> Names; |
| 2365 | |
| 2366 | // Substitution table. Itanium supports name substitutions as a means of |
| 2367 | // compression. The string "S42_" refers to the 44nd entry (base-36) in this |
| 2368 | // table. |
| 2369 | PODSmallVector<Node *, 32> Subs; |
| 2370 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2371 | using TemplateParamList = PODSmallVector<Node *, 8>; |
| 2372 | |
| 2373 | class ScopedTemplateParamList { |
| 2374 | AbstractManglingParser *Parser; |
| 2375 | size_t OldNumTemplateParamLists; |
| 2376 | TemplateParamList Params; |
| 2377 | |
| 2378 | public: |
Louis Dionne | c1fe867 | 2020-10-30 17:33:02 -0400 | [diff] [blame] | 2379 | ScopedTemplateParamList(AbstractManglingParser *TheParser) |
| 2380 | : Parser(TheParser), |
| 2381 | OldNumTemplateParamLists(TheParser->TemplateParams.size()) { |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2382 | Parser->TemplateParams.push_back(&Params); |
| 2383 | } |
| 2384 | ~ScopedTemplateParamList() { |
| 2385 | assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists); |
| 2386 | Parser->TemplateParams.dropBack(OldNumTemplateParamLists); |
| 2387 | } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2388 | }; |
| 2389 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2390 | // Template parameter table. Like the above, but referenced like "T42_". |
| 2391 | // This has a smaller size compared to Subs and Names because it can be |
| 2392 | // stored on the stack. |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2393 | TemplateParamList OuterTemplateParams; |
| 2394 | |
| 2395 | // Lists of template parameters indexed by template parameter depth, |
| 2396 | // referenced like "TL2_4_". If nonempty, element 0 is always |
| 2397 | // OuterTemplateParams; inner elements are always template parameter lists of |
| 2398 | // lambda expressions. For a generic lambda with no explicit template |
| 2399 | // parameter list, the corresponding parameter list pointer will be null. |
| 2400 | PODSmallVector<TemplateParamList *, 4> TemplateParams; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2401 | |
| 2402 | // Set of unresolved forward <template-param> references. These can occur in a |
| 2403 | // conversion operator's type, and are resolved in the enclosing <encoding>. |
| 2404 | PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs; |
| 2405 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2406 | bool TryToParseTemplateArgs = true; |
| 2407 | bool PermitForwardTemplateReferences = false; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2408 | size_t ParsingLambdaParamsAtLevel = (size_t)-1; |
| 2409 | |
| 2410 | unsigned NumSyntheticTemplateParameters[3] = {}; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2411 | |
| 2412 | Alloc ASTAllocator; |
| 2413 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2414 | AbstractManglingParser(const char *First_, const char *Last_) |
| 2415 | : First(First_), Last(Last_) {} |
| 2416 | |
| 2417 | Derived &getDerived() { return static_cast<Derived &>(*this); } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2418 | |
| 2419 | void reset(const char *First_, const char *Last_) { |
| 2420 | First = First_; |
| 2421 | Last = Last_; |
| 2422 | Names.clear(); |
| 2423 | Subs.clear(); |
| 2424 | TemplateParams.clear(); |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2425 | ParsingLambdaParamsAtLevel = (size_t)-1; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2426 | TryToParseTemplateArgs = true; |
| 2427 | PermitForwardTemplateReferences = false; |
Richard Smith | 9a2307a | 2019-09-07 00:11:53 +0000 | [diff] [blame] | 2428 | for (int I = 0; I != 3; ++I) |
| 2429 | NumSyntheticTemplateParameters[I] = 0; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2430 | ASTAllocator.reset(); |
| 2431 | } |
| 2432 | |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 2433 | template <class T, class... Args> Node *make(Args &&... args) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2434 | return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...); |
| 2435 | } |
| 2436 | |
| 2437 | template <class It> NodeArray makeNodeArray(It begin, It end) { |
| 2438 | size_t sz = static_cast<size_t>(end - begin); |
| 2439 | void *mem = ASTAllocator.allocateNodeArray(sz); |
| 2440 | Node **data = new (mem) Node *[sz]; |
| 2441 | std::copy(begin, end, data); |
| 2442 | return NodeArray(data, sz); |
| 2443 | } |
| 2444 | |
| 2445 | NodeArray popTrailingNodeArray(size_t FromPosition) { |
| 2446 | assert(FromPosition <= Names.size()); |
| 2447 | NodeArray res = |
| 2448 | makeNodeArray(Names.begin() + (long)FromPosition, Names.end()); |
| 2449 | Names.dropBack(FromPosition); |
| 2450 | return res; |
| 2451 | } |
| 2452 | |
| 2453 | bool consumeIf(StringView S) { |
| 2454 | if (StringView(First, Last).startsWith(S)) { |
| 2455 | First += S.size(); |
| 2456 | return true; |
| 2457 | } |
| 2458 | return false; |
| 2459 | } |
| 2460 | |
| 2461 | bool consumeIf(char C) { |
| 2462 | if (First != Last && *First == C) { |
| 2463 | ++First; |
| 2464 | return true; |
| 2465 | } |
| 2466 | return false; |
| 2467 | } |
| 2468 | |
| 2469 | char consume() { return First != Last ? *First++ : '\0'; } |
| 2470 | |
Nathan Sidwell | fd0ef6d | 2022-01-20 07:40:12 -0800 | [diff] [blame] | 2471 | char look(unsigned Lookahead = 0) const { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2472 | if (static_cast<size_t>(Last - First) <= Lookahead) |
| 2473 | return '\0'; |
| 2474 | return First[Lookahead]; |
| 2475 | } |
| 2476 | |
| 2477 | size_t numLeft() const { return static_cast<size_t>(Last - First); } |
| 2478 | |
| 2479 | StringView parseNumber(bool AllowNegative = false); |
| 2480 | Qualifiers parseCVQualifiers(); |
| 2481 | bool parsePositiveInteger(size_t *Out); |
| 2482 | StringView parseBareSourceName(); |
| 2483 | |
| 2484 | bool parseSeqId(size_t *Out); |
| 2485 | Node *parseSubstitution(); |
| 2486 | Node *parseTemplateParam(); |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2487 | Node *parseTemplateParamDecl(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2488 | Node *parseTemplateArgs(bool TagTemplates = false); |
| 2489 | Node *parseTemplateArg(); |
| 2490 | |
| 2491 | /// Parse the <expr> production. |
| 2492 | Node *parseExpr(); |
| 2493 | Node *parsePrefixExpr(StringView Kind); |
| 2494 | Node *parseBinaryExpr(StringView Kind); |
| 2495 | Node *parseIntegerLiteral(StringView Lit); |
| 2496 | Node *parseExprPrimary(); |
| 2497 | template <class Float> Node *parseFloatingLiteral(); |
| 2498 | Node *parseFunctionParam(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2499 | Node *parseConversionExpr(); |
| 2500 | Node *parseBracedExpr(); |
| 2501 | Node *parseFoldExpr(); |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 2502 | Node *parsePointerToMemberConversionExpr(); |
| 2503 | Node *parseSubobjectExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2504 | |
| 2505 | /// Parse the <type> production. |
| 2506 | Node *parseType(); |
| 2507 | Node *parseFunctionType(); |
| 2508 | Node *parseVectorType(); |
| 2509 | Node *parseDecltype(); |
| 2510 | Node *parseArrayType(); |
| 2511 | Node *parsePointerToMemberType(); |
| 2512 | Node *parseClassEnumType(); |
| 2513 | Node *parseQualifiedType(); |
| 2514 | |
| 2515 | Node *parseEncoding(); |
| 2516 | bool parseCallOffset(); |
| 2517 | Node *parseSpecialName(); |
| 2518 | |
| 2519 | /// Holds some extra information about a <name> that is being parsed. This |
| 2520 | /// information is only pertinent if the <name> refers to an <encoding>. |
| 2521 | struct NameState { |
| 2522 | bool CtorDtorConversion = false; |
| 2523 | bool EndsWithTemplateArgs = false; |
| 2524 | Qualifiers CVQualifiers = QualNone; |
| 2525 | FunctionRefQual ReferenceQualifier = FrefQualNone; |
| 2526 | size_t ForwardTemplateRefsBegin; |
| 2527 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2528 | NameState(AbstractManglingParser *Enclosing) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2529 | : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {} |
| 2530 | }; |
| 2531 | |
| 2532 | bool resolveForwardTemplateRefs(NameState &State) { |
| 2533 | size_t I = State.ForwardTemplateRefsBegin; |
| 2534 | size_t E = ForwardTemplateRefs.size(); |
| 2535 | for (; I < E; ++I) { |
| 2536 | size_t Idx = ForwardTemplateRefs[I]->Index; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2537 | if (TemplateParams.empty() || !TemplateParams[0] || |
| 2538 | Idx >= TemplateParams[0]->size()) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2539 | return true; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2540 | ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx]; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2541 | } |
| 2542 | ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin); |
| 2543 | return false; |
| 2544 | } |
| 2545 | |
| 2546 | /// Parse the <name> production> |
| 2547 | Node *parseName(NameState *State = nullptr); |
| 2548 | Node *parseLocalName(NameState *State); |
| 2549 | Node *parseOperatorName(NameState *State); |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2550 | Node *parseUnqualifiedName(NameState *State, Node *Scope); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2551 | Node *parseUnnamedTypeName(NameState *State); |
| 2552 | Node *parseSourceName(NameState *State); |
| 2553 | Node *parseUnscopedName(NameState *State); |
| 2554 | Node *parseNestedName(NameState *State); |
| 2555 | Node *parseCtorDtorName(Node *&SoFar, NameState *State); |
| 2556 | |
| 2557 | Node *parseAbiTags(Node *N); |
| 2558 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 2559 | struct OperatorInfo { |
| 2560 | enum OIKind : unsigned char { |
| 2561 | Prefix, // Prefix unary: @ expr |
| 2562 | Postfix, // Postfix unary: expr @ |
| 2563 | Binary, // Binary: lhs @ rhs |
| 2564 | Array, // Array index: lhs [ rhs ] |
| 2565 | Member, // Member access: lhs @ rhs |
| 2566 | New, // New |
| 2567 | Del, // Delete |
| 2568 | Call, // Function call: expr (expr*) |
| 2569 | CCast, // C cast: (type)expr |
| 2570 | Conditional, // Conditional: expr ? expr : expr |
| 2571 | // Below do not have operator names |
| 2572 | NamedCast, // Named cast, @<type>(expr) |
| 2573 | OfIdOp, // alignof, sizeof, typeid |
| 2574 | |
| 2575 | Unnameable = NamedCast, |
| 2576 | }; |
| 2577 | char Enc[2]; // Encoding |
| 2578 | OIKind Kind; // Kind of operator |
| 2579 | bool Flag : 1; // Entry-specific flag |
| 2580 | const char *Name; // Spelling |
| 2581 | |
| 2582 | public: |
| 2583 | constexpr OperatorInfo(const char (&E)[3], OIKind K, bool F, const char *N) |
| 2584 | : Enc{E[0], E[1]}, Kind{K}, Flag{F}, Name{N} {} |
| 2585 | |
| 2586 | public: |
| 2587 | bool operator<(const OperatorInfo &Other) const { |
| 2588 | return *this < Other.Enc; |
| 2589 | } |
| 2590 | bool operator<(const char *Peek) const { |
| 2591 | return Enc[0] < Peek[0] || (Enc[0] == Peek[0] && Enc[1] < Peek[1]); |
| 2592 | } |
| 2593 | bool operator==(const char *Peek) const { |
| 2594 | return Enc[0] == Peek[0] && Enc[1] == Peek[1]; |
| 2595 | } |
| 2596 | bool operator!=(const char *Peek) const { return !this->operator==(Peek); } |
| 2597 | |
| 2598 | public: |
| 2599 | StringView getSymbol() const { |
| 2600 | StringView Res = Name; |
| 2601 | if (Kind < Unnameable) { |
| 2602 | assert(Res.startsWith("operator") && |
| 2603 | "operator name does not start with 'operator'"); |
| 2604 | Res = Res.dropFront(sizeof("operator") - 1); |
| 2605 | Res.consumeFront(' '); |
| 2606 | } |
| 2607 | return Res; |
| 2608 | } |
| 2609 | StringView getName() const { return Name; } |
| 2610 | OIKind getKind() const { return Kind; } |
| 2611 | bool getFlag() const { return Flag; } |
| 2612 | }; |
| 2613 | const OperatorInfo *parseOperatorEncoding(); |
| 2614 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2615 | /// Parse the <unresolved-name> production. |
Nathan Sidwell | 77c52e2 | 2022-01-28 11:59:03 -0800 | [diff] [blame] | 2616 | Node *parseUnresolvedName(bool Global); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2617 | Node *parseSimpleId(); |
| 2618 | Node *parseBaseUnresolvedName(); |
| 2619 | Node *parseUnresolvedType(); |
| 2620 | Node *parseDestructorName(); |
| 2621 | |
| 2622 | /// Top-level entry point into the parser. |
| 2623 | Node *parse(); |
| 2624 | }; |
| 2625 | |
| 2626 | const char* parse_discriminator(const char* first, const char* last); |
| 2627 | |
| 2628 | // <name> ::= <nested-name> // N |
| 2629 | // ::= <local-name> # See Scope Encoding below // Z |
| 2630 | // ::= <unscoped-template-name> <template-args> |
| 2631 | // ::= <unscoped-name> |
| 2632 | // |
| 2633 | // <unscoped-template-name> ::= <unscoped-name> |
| 2634 | // ::= <substitution> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2635 | template <typename Derived, typename Alloc> |
| 2636 | Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2637 | if (look() == 'N') |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2638 | return getDerived().parseNestedName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2639 | if (look() == 'Z') |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2640 | return getDerived().parseLocalName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2641 | |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 2642 | Node *Result = nullptr; |
| 2643 | bool IsSubst = look() == 'S' && look(1) != 't'; |
| 2644 | if (IsSubst) { |
| 2645 | // A substitution must lead to: |
| 2646 | // ::= <unscoped-template-name> <template-args> |
| 2647 | Result = getDerived().parseSubstitution(); |
| 2648 | } else { |
| 2649 | // An unscoped name can be one of: |
| 2650 | // ::= <unscoped-name> |
| 2651 | // ::= <unscoped-template-name> <template-args> |
| 2652 | Result = getDerived().parseUnscopedName(State); |
| 2653 | } |
| 2654 | if (Result == nullptr) |
| 2655 | return nullptr; |
| 2656 | |
| 2657 | if (look() == 'I') { |
| 2658 | // ::= <unscoped-template-name> <template-args> |
| 2659 | if (!IsSubst) |
| 2660 | // An unscoped-template-name is substitutable. |
| 2661 | Subs.push_back(Result); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2662 | Node *TA = getDerived().parseTemplateArgs(State != nullptr); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2663 | if (TA == nullptr) |
| 2664 | return nullptr; |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 2665 | if (State) |
| 2666 | State->EndsWithTemplateArgs = true; |
| 2667 | Result = make<NameWithTemplateArgs>(Result, TA); |
| 2668 | } else if (IsSubst) { |
| 2669 | // The substitution case must be followed by <template-args>. |
| 2670 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 2673 | return Result; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | // <local-name> := Z <function encoding> E <entity name> [<discriminator>] |
| 2677 | // := Z <function encoding> E s [<discriminator>] |
| 2678 | // := Z <function encoding> Ed [ <parameter number> ] _ <entity name> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2679 | template <typename Derived, typename Alloc> |
| 2680 | Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2681 | if (!consumeIf('Z')) |
| 2682 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2683 | Node *Encoding = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2684 | if (Encoding == nullptr || !consumeIf('E')) |
| 2685 | return nullptr; |
| 2686 | |
| 2687 | if (consumeIf('s')) { |
| 2688 | First = parse_discriminator(First, Last); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 2689 | auto *StringLitName = make<NameType>("string literal"); |
| 2690 | if (!StringLitName) |
| 2691 | return nullptr; |
| 2692 | return make<LocalName>(Encoding, StringLitName); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | if (consumeIf('d')) { |
| 2696 | parseNumber(true); |
| 2697 | if (!consumeIf('_')) |
| 2698 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2699 | Node *N = getDerived().parseName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2700 | if (N == nullptr) |
| 2701 | return nullptr; |
| 2702 | return make<LocalName>(Encoding, N); |
| 2703 | } |
| 2704 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2705 | Node *Entity = getDerived().parseName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2706 | if (Entity == nullptr) |
| 2707 | return nullptr; |
| 2708 | First = parse_discriminator(First, Last); |
| 2709 | return make<LocalName>(Encoding, Entity); |
| 2710 | } |
| 2711 | |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 2712 | // <unscoped-name> ::= [L]* <unqualified-name> |
| 2713 | // ::= St [L]* <unqualified-name> # ::std:: |
| 2714 | // [*] extension |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2715 | template <typename Derived, typename Alloc> |
| 2716 | Node * |
| 2717 | AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) { |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2718 | Node *Std = nullptr; |
| 2719 | if (consumeIf("St")) { |
| 2720 | Std = make<NameType>("std"); |
| 2721 | if (Std == nullptr) |
Nathan Sidwell | 200e97c | 2022-01-21 11:37:01 -0800 | [diff] [blame] | 2722 | return nullptr; |
| 2723 | } |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2724 | consumeIf('L'); |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 2725 | |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2726 | return getDerived().parseUnqualifiedName(State, Std); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | // <unqualified-name> ::= <operator-name> [abi-tags] |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2730 | // ::= <ctor-dtor-name> [<abi-tags>] |
| 2731 | // ::= <source-name> [<abi-tags>] |
| 2732 | // ::= <unnamed-type-name> [<abi-tags>] |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2733 | // ::= DC <source-name>+ E # structured binding declaration |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2734 | template <typename Derived, typename Alloc> |
| 2735 | Node * |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2736 | AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State, |
| 2737 | Node *Scope) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2738 | Node *Result; |
| 2739 | if (look() == 'U') |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2740 | Result = getDerived().parseUnnamedTypeName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2741 | else if (look() >= '1' && look() <= '9') |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2742 | Result = getDerived().parseSourceName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2743 | else if (consumeIf("DC")) { |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2744 | // Structured binding |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2745 | size_t BindingsBegin = Names.size(); |
| 2746 | do { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2747 | Node *Binding = getDerived().parseSourceName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2748 | if (Binding == nullptr) |
| 2749 | return nullptr; |
| 2750 | Names.push_back(Binding); |
| 2751 | } while (!consumeIf('E')); |
| 2752 | Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin)); |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2753 | } else if (look() == 'C' || look() == 'D') { |
| 2754 | // A <ctor-dtor-name>. |
| 2755 | if (Scope == nullptr) |
| 2756 | return nullptr; |
| 2757 | Result = getDerived().parseCtorDtorName(Scope, State); |
| 2758 | } else { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2759 | Result = getDerived().parseOperatorName(State); |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2760 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2761 | if (Result != nullptr) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2762 | Result = getDerived().parseAbiTags(Result); |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 2763 | if (Result != nullptr && Scope != nullptr) |
| 2764 | Result = make<NestedName>(Scope, Result); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2765 | return Result; |
| 2766 | } |
| 2767 | |
| 2768 | // <unnamed-type-name> ::= Ut [<nonnegative number>] _ |
| 2769 | // ::= <closure-type-name> |
| 2770 | // |
| 2771 | // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ |
| 2772 | // |
| 2773 | // <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] | 2774 | template <typename Derived, typename Alloc> |
| 2775 | Node * |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2776 | AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) { |
| 2777 | // <template-params> refer to the innermost <template-args>. Clear out any |
| 2778 | // outer args that we may have inserted into TemplateParams. |
| 2779 | if (State != nullptr) |
| 2780 | TemplateParams.clear(); |
| 2781 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2782 | if (consumeIf("Ut")) { |
| 2783 | StringView Count = parseNumber(); |
| 2784 | if (!consumeIf('_')) |
| 2785 | return nullptr; |
| 2786 | return make<UnnamedTypeName>(Count); |
| 2787 | } |
| 2788 | if (consumeIf("Ul")) { |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2789 | SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel, |
| 2790 | TemplateParams.size()); |
| 2791 | ScopedTemplateParamList LambdaTemplateParams(this); |
| 2792 | |
| 2793 | size_t ParamsBegin = Names.size(); |
| 2794 | while (look() == 'T' && |
| 2795 | StringView("yptn").find(look(1)) != StringView::npos) { |
| 2796 | Node *T = parseTemplateParamDecl(); |
| 2797 | if (!T) |
| 2798 | return nullptr; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2799 | Names.push_back(T); |
| 2800 | } |
| 2801 | NodeArray TempParams = popTrailingNodeArray(ParamsBegin); |
| 2802 | |
| 2803 | // FIXME: If TempParams is empty and none of the function parameters |
| 2804 | // includes 'auto', we should remove LambdaTemplateParams from the |
| 2805 | // TemplateParams list. Unfortunately, we don't find out whether there are |
| 2806 | // any 'auto' parameters until too late in an example such as: |
| 2807 | // |
| 2808 | // template<typename T> void f( |
| 2809 | // decltype([](decltype([]<typename T>(T v) {}), |
| 2810 | // auto) {})) {} |
| 2811 | // template<typename T> void f( |
| 2812 | // decltype([](decltype([]<typename T>(T w) {}), |
| 2813 | // int) {})) {} |
| 2814 | // |
| 2815 | // Here, the type of v is at level 2 but the type of w is at level 1. We |
| 2816 | // don't find this out until we encounter the type of the next parameter. |
| 2817 | // |
| 2818 | // However, compilers can't actually cope with the former example in |
| 2819 | // practice, and it's likely to be made ill-formed in future, so we don't |
| 2820 | // need to support it here. |
| 2821 | // |
| 2822 | // If we encounter an 'auto' in the function parameter types, we will |
| 2823 | // recreate a template parameter scope for it, but any intervening lambdas |
| 2824 | // will be parsed in the 'wrong' template parameter depth. |
| 2825 | if (TempParams.empty()) |
| 2826 | TemplateParams.pop_back(); |
| 2827 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2828 | if (!consumeIf("vE")) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2829 | do { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2830 | Node *P = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2831 | if (P == nullptr) |
| 2832 | return nullptr; |
| 2833 | Names.push_back(P); |
| 2834 | } while (!consumeIf('E')); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2835 | } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2836 | NodeArray Params = popTrailingNodeArray(ParamsBegin); |
| 2837 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2838 | StringView Count = parseNumber(); |
| 2839 | if (!consumeIf('_')) |
| 2840 | return nullptr; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 2841 | return make<ClosureTypeName>(TempParams, Params, Count); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2842 | } |
Erik Pilkington | 974b654 | 2019-01-17 21:37:51 +0000 | [diff] [blame] | 2843 | if (consumeIf("Ub")) { |
| 2844 | (void)parseNumber(); |
| 2845 | if (!consumeIf('_')) |
| 2846 | return nullptr; |
| 2847 | return make<NameType>("'block-literal'"); |
| 2848 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2849 | return nullptr; |
| 2850 | } |
| 2851 | |
| 2852 | // <source-name> ::= <positive length number> <identifier> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2853 | template <typename Derived, typename Alloc> |
| 2854 | Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2855 | size_t Length = 0; |
| 2856 | if (parsePositiveInteger(&Length)) |
| 2857 | return nullptr; |
| 2858 | if (numLeft() < Length || Length == 0) |
| 2859 | return nullptr; |
| 2860 | StringView Name(First, First + Length); |
| 2861 | First += Length; |
| 2862 | if (Name.startsWith("_GLOBAL__N")) |
| 2863 | return make<NameType>("(anonymous namespace)"); |
| 2864 | return make<NameType>(Name); |
| 2865 | } |
| 2866 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 2867 | // If the next 2 chars are an operator encoding, consume them and return their |
| 2868 | // OperatorInfo. Otherwise return nullptr. |
| 2869 | template <typename Derived, typename Alloc> |
| 2870 | const typename AbstractManglingParser<Derived, Alloc>::OperatorInfo * |
| 2871 | AbstractManglingParser<Derived, Alloc>::parseOperatorEncoding() { |
| 2872 | static const OperatorInfo Ops[] = { |
| 2873 | // Keep ordered by encoding |
| 2874 | {"aN", OperatorInfo::Binary, false, "operator&="}, |
| 2875 | {"aS", OperatorInfo::Binary, false, "operator="}, |
| 2876 | {"aa", OperatorInfo::Binary, false, "operator&&"}, |
| 2877 | {"ad", OperatorInfo::Prefix, false, "operator&"}, |
| 2878 | {"an", OperatorInfo::Binary, false, "operator&"}, |
| 2879 | {"at", OperatorInfo::OfIdOp, /*Type*/ true, "alignof ("}, |
| 2880 | {"az", OperatorInfo::OfIdOp, /*Type*/ false, "alignof ("}, |
| 2881 | {"cc", OperatorInfo::NamedCast, false, "const_cast"}, |
| 2882 | {"cl", OperatorInfo::Call, false, "operator()"}, |
| 2883 | {"cm", OperatorInfo::Binary, false, "operator,"}, |
| 2884 | {"co", OperatorInfo::Prefix, false, "operator~"}, |
| 2885 | {"cv", OperatorInfo::CCast, false, "operator"}, // C Cast |
| 2886 | {"dV", OperatorInfo::Binary, false, "operator/="}, |
| 2887 | {"da", OperatorInfo::Del, /*Ary*/ true, "operator delete[]"}, |
| 2888 | {"dc", OperatorInfo::NamedCast, false, "dynamic_cast"}, |
| 2889 | {"de", OperatorInfo::Prefix, false, "operator*"}, |
| 2890 | {"dl", OperatorInfo::Del, /*Ary*/ false, "operator delete"}, |
| 2891 | {"ds", OperatorInfo::Member, /*Named*/ false, "operator.*"}, |
| 2892 | {"dt", OperatorInfo::Member, /*Named*/ false, "operator."}, |
| 2893 | {"dv", OperatorInfo::Binary, false, "operator/"}, |
| 2894 | {"eO", OperatorInfo::Binary, false, "operator^="}, |
| 2895 | {"eo", OperatorInfo::Binary, false, "operator^"}, |
| 2896 | {"eq", OperatorInfo::Binary, false, "operator=="}, |
| 2897 | {"ge", OperatorInfo::Binary, false, "operator>="}, |
| 2898 | {"gt", OperatorInfo::Binary, false, "operator>"}, |
| 2899 | {"ix", OperatorInfo::Array, false, "operator[]"}, |
| 2900 | {"lS", OperatorInfo::Binary, false, "operator<<="}, |
| 2901 | {"le", OperatorInfo::Binary, false, "operator<="}, |
| 2902 | {"ls", OperatorInfo::Binary, false, "operator<<"}, |
| 2903 | {"lt", OperatorInfo::Binary, false, "operator<"}, |
| 2904 | {"mI", OperatorInfo::Binary, false, "operator-="}, |
| 2905 | {"mL", OperatorInfo::Binary, false, "operator*="}, |
| 2906 | {"mi", OperatorInfo::Binary, false, "operator-"}, |
| 2907 | {"ml", OperatorInfo::Binary, false, "operator*"}, |
| 2908 | {"mm", OperatorInfo::Postfix, false, "operator--"}, |
| 2909 | {"na", OperatorInfo::New, /*Ary*/ true, "operator new[]"}, |
| 2910 | {"ne", OperatorInfo::Binary, false, "operator!="}, |
| 2911 | {"ng", OperatorInfo::Prefix, false, "operator-"}, |
| 2912 | {"nt", OperatorInfo::Prefix, false, "operator!"}, |
| 2913 | {"nw", OperatorInfo::New, /*Ary*/ false, "operator new"}, |
| 2914 | {"oR", OperatorInfo::Binary, false, "operator|="}, |
| 2915 | {"oo", OperatorInfo::Binary, false, "operator||"}, |
| 2916 | {"or", OperatorInfo::Binary, false, "operator|"}, |
| 2917 | {"pL", OperatorInfo::Binary, false, "operator+="}, |
| 2918 | {"pl", OperatorInfo::Binary, false, "operator+"}, |
| 2919 | {"pm", OperatorInfo::Member, /*Named*/ false, "operator->*"}, |
| 2920 | {"pp", OperatorInfo::Postfix, false, "operator++"}, |
| 2921 | {"ps", OperatorInfo::Prefix, false, "operator+"}, |
| 2922 | {"pt", OperatorInfo::Member, /*Named*/ true, "operator->"}, |
| 2923 | {"qu", OperatorInfo::Conditional, false, "operator?"}, |
| 2924 | {"rM", OperatorInfo::Binary, false, "operator%="}, |
| 2925 | {"rS", OperatorInfo::Binary, false, "operator>>="}, |
| 2926 | {"rc", OperatorInfo::NamedCast, false, "reinterpret_cast"}, |
| 2927 | {"rm", OperatorInfo::Binary, false, "operator%"}, |
| 2928 | {"rs", OperatorInfo::Binary, false, "operator>>"}, |
| 2929 | {"sc", OperatorInfo::NamedCast, false, "static_cast"}, |
| 2930 | {"ss", OperatorInfo::Binary, false, "operator<=>"}, |
| 2931 | {"st", OperatorInfo::OfIdOp, /*Type*/ true, "sizeof ("}, |
| 2932 | {"sz", OperatorInfo::OfIdOp, /*Type*/ false, "sizeof ("}, |
| 2933 | {"te", OperatorInfo::OfIdOp, /*Type*/ false, "typeid ("}, |
| 2934 | {"ti", OperatorInfo::OfIdOp, /*Type*/ true, "typeid ("}, |
| 2935 | }; |
| 2936 | const auto NumOps = sizeof(Ops) / sizeof(Ops[0]); |
| 2937 | |
| 2938 | #ifndef NDEBUG |
| 2939 | { |
| 2940 | // Verify table order. |
| 2941 | static bool Done; |
| 2942 | if (!Done) { |
| 2943 | Done = true; |
| 2944 | for (const auto *Op = &Ops[0]; Op != &Ops[NumOps - 1]; Op++) |
| 2945 | assert(Op[0] < Op[1] && "Operator table is not ordered"); |
| 2946 | } |
| 2947 | } |
| 2948 | #endif |
| 2949 | |
| 2950 | if (numLeft() < 2) |
| 2951 | return nullptr; |
| 2952 | |
| 2953 | auto Op = std::lower_bound( |
| 2954 | &Ops[0], &Ops[NumOps], First, |
| 2955 | [](const OperatorInfo &Op_, const char *Enc_) { return Op_ < Enc_; }); |
| 2956 | if (Op == &Ops[NumOps] || *Op != First) |
| 2957 | return nullptr; |
| 2958 | |
| 2959 | First += 2; |
| 2960 | return Op; |
| 2961 | } |
| 2962 | |
| 2963 | // <operator-name> ::= See parseOperatorEncoding() |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2964 | // ::= li <source-name> # operator "" |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 2965 | // ::= v <digit> <source-name> # vendor extended operator |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2966 | template <typename Derived, typename Alloc> |
| 2967 | Node * |
| 2968 | AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) { |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 2969 | if (const auto *Op = parseOperatorEncoding()) { |
| 2970 | if (Op->getKind() == OperatorInfo::CCast) { |
| 2971 | // ::= cv <type> # (cast) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2972 | SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false); |
| 2973 | // If we're parsing an encoding, State != nullptr and the conversion |
| 2974 | // operators' <type> could have a <template-param> that refers to some |
| 2975 | // <template-arg>s further ahead in the mangled name. |
| 2976 | SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences, |
| 2977 | PermitForwardTemplateReferences || |
| 2978 | State != nullptr); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 2979 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2980 | if (Ty == nullptr) |
| 2981 | return nullptr; |
| 2982 | if (State) State->CtorDtorConversion = true; |
| 2983 | return make<ConversionOperatorType>(Ty); |
| 2984 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 2985 | |
| 2986 | if (Op->getKind() >= OperatorInfo::Unnameable) |
| 2987 | /* Not a nameable operator. */ |
| 2988 | return nullptr; |
| 2989 | if (Op->getKind() == OperatorInfo::Member && !Op->getFlag()) |
| 2990 | /* Not a nameable MemberExpr */ |
| 2991 | return nullptr; |
| 2992 | |
| 2993 | return make<NameType>(Op->getName()); |
| 2994 | } |
| 2995 | |
| 2996 | if (consumeIf("li")) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 2997 | // ::= li <source-name> # operator "" |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 2998 | Node *SN = getDerived().parseSourceName(State); |
| 2999 | if (SN == nullptr) |
| 3000 | return nullptr; |
| 3001 | return make<LiteralOperator>(SN); |
| 3002 | } |
| 3003 | |
| 3004 | if (consumeIf('v')) { |
| 3005 | // ::= v <digit> <source-name> # vendor extended operator |
| 3006 | if (look() >= '0' && look() <= '9') { |
| 3007 | First++; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3008 | Node *SN = getDerived().parseSourceName(State); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3009 | if (SN == nullptr) |
| 3010 | return nullptr; |
| 3011 | return make<ConversionOperatorType>(SN); |
| 3012 | } |
| 3013 | return nullptr; |
| 3014 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 3015 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3016 | return nullptr; |
| 3017 | } |
| 3018 | |
| 3019 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 3020 | // ::= C2 # base object constructor |
| 3021 | // ::= C3 # complete object allocating constructor |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3022 | // extension ::= C4 # gcc old-style "[unified]" constructor |
| 3023 | // extension ::= C5 # the COMDAT used for ctors |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3024 | // ::= D0 # deleting destructor |
| 3025 | // ::= D1 # complete object destructor |
| 3026 | // ::= D2 # base object destructor |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3027 | // extension ::= D4 # gcc old-style "[unified]" destructor |
| 3028 | // extension ::= D5 # the COMDAT used for dtors |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3029 | template <typename Derived, typename Alloc> |
| 3030 | Node * |
| 3031 | AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar, |
| 3032 | NameState *State) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3033 | if (SoFar->getKind() == Node::KSpecialSubstitution) { |
| 3034 | auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK; |
| 3035 | switch (SSK) { |
| 3036 | case SpecialSubKind::string: |
| 3037 | case SpecialSubKind::istream: |
| 3038 | case SpecialSubKind::ostream: |
| 3039 | case SpecialSubKind::iostream: |
| 3040 | SoFar = make<ExpandedSpecialSubstitution>(SSK); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3041 | if (!SoFar) |
| 3042 | return nullptr; |
Reid Kleckner | e76aabe | 2018-11-01 18:24:03 +0000 | [diff] [blame] | 3043 | break; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3044 | default: |
| 3045 | break; |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | if (consumeIf('C')) { |
| 3050 | bool IsInherited = consumeIf('I'); |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3051 | if (look() != '1' && look() != '2' && look() != '3' && look() != '4' && |
| 3052 | look() != '5') |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3053 | return nullptr; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3054 | int Variant = look() - '0'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3055 | ++First; |
| 3056 | if (State) State->CtorDtorConversion = true; |
| 3057 | if (IsInherited) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3058 | if (getDerived().parseName(State) == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3059 | return nullptr; |
| 3060 | } |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3061 | return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3064 | if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' || |
| 3065 | look(1) == '4' || look(1) == '5')) { |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3066 | int Variant = look(1) - '0'; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3067 | First += 2; |
| 3068 | if (State) State->CtorDtorConversion = true; |
Nico Weber | 2929479 | 2019-04-03 23:14:33 +0000 | [diff] [blame] | 3069 | return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | return nullptr; |
| 3073 | } |
| 3074 | |
| 3075 | // <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E |
| 3076 | // ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E |
| 3077 | // |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3078 | // <prefix> ::= <prefix> [L]* <unqualified-name> |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3079 | // ::= <template-prefix> <template-args> |
| 3080 | // ::= <template-param> |
| 3081 | // ::= <decltype> |
| 3082 | // ::= # empty |
| 3083 | // ::= <substitution> |
| 3084 | // ::= <prefix> <data-member-prefix> |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3085 | // [*] extension |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3086 | // |
| 3087 | // <data-member-prefix> := <member source-name> [<template-args>] M |
| 3088 | // |
| 3089 | // <template-prefix> ::= <prefix> <template unqualified-name> |
| 3090 | // ::= <template-param> |
| 3091 | // ::= <substitution> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3092 | template <typename Derived, typename Alloc> |
| 3093 | Node * |
| 3094 | AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3095 | if (!consumeIf('N')) |
| 3096 | return nullptr; |
| 3097 | |
| 3098 | Qualifiers CVTmp = parseCVQualifiers(); |
| 3099 | if (State) State->CVQualifiers = CVTmp; |
| 3100 | |
| 3101 | if (consumeIf('O')) { |
| 3102 | if (State) State->ReferenceQualifier = FrefQualRValue; |
| 3103 | } else if (consumeIf('R')) { |
| 3104 | if (State) State->ReferenceQualifier = FrefQualLValue; |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3105 | } else { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3106 | if (State) State->ReferenceQualifier = FrefQualNone; |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3107 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3108 | |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3109 | Node *SoFar = nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3110 | while (!consumeIf('E')) { |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3111 | if (State) |
| 3112 | // Only set end-with-template on the case that does that. |
| 3113 | State->EndsWithTemplateArgs = false; |
| 3114 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3115 | if (look() == 'T') { |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3116 | // ::= <template-param> |
| 3117 | if (SoFar != nullptr) |
| 3118 | return nullptr; // Cannot have a prefix. |
| 3119 | SoFar = getDerived().parseTemplateParam(); |
| 3120 | } else if (look() == 'I') { |
| 3121 | // ::= <template-prefix> <template-args> |
| 3122 | if (SoFar == nullptr) |
| 3123 | return nullptr; // Must have a prefix. |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3124 | Node *TA = getDerived().parseTemplateArgs(State != nullptr); |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3125 | if (TA == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3126 | return nullptr; |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3127 | if (SoFar->getKind() == Node::KNameWithTemplateArgs) |
| 3128 | // Semantically <template-args> <template-args> cannot be generated by a |
| 3129 | // C++ entity. There will always be [something like] a name between |
| 3130 | // them. |
| 3131 | return nullptr; |
| 3132 | if (State) |
| 3133 | State->EndsWithTemplateArgs = true; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3134 | SoFar = make<NameWithTemplateArgs>(SoFar, TA); |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3135 | } else if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) { |
| 3136 | // ::= <decltype> |
| 3137 | if (SoFar != nullptr) |
| 3138 | return nullptr; // Cannot have a prefix. |
| 3139 | SoFar = getDerived().parseDecltype(); |
| 3140 | } else if (look() == 'S') { |
| 3141 | // ::= <substitution> |
| 3142 | if (SoFar != nullptr) |
| 3143 | return nullptr; // Cannot have a prefix. |
| 3144 | if (look(1) == 't') { |
| 3145 | // parseSubstition does not handle 'St'. |
| 3146 | First += 2; |
| 3147 | SoFar = make<NameType>("std"); |
| 3148 | } else { |
| 3149 | SoFar = getDerived().parseSubstitution(); |
| 3150 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3151 | if (SoFar == nullptr) |
| 3152 | return nullptr; |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3153 | continue; // Do not push a new substitution. |
| 3154 | } else { |
Nathan Sidwell | e654529 | 2022-01-25 12:31:01 -0800 | [diff] [blame] | 3155 | consumeIf('L'); // extension |
Nathan Sidwell | 9a29c97 | 2022-01-25 12:23:31 -0800 | [diff] [blame] | 3156 | // ::= [<prefix>] <unqualified-name> |
| 3157 | SoFar = getDerived().parseUnqualifiedName(State, SoFar); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3158 | } |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 3159 | if (SoFar == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3160 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3161 | Subs.push_back(SoFar); |
Nathan Sidwell | e654529 | 2022-01-25 12:31:01 -0800 | [diff] [blame] | 3162 | |
| 3163 | // No longer used. |
| 3164 | // <data-member-prefix> := <member source-name> [<template-args>] M |
| 3165 | consumeIf('M'); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | if (SoFar == nullptr || Subs.empty()) |
| 3169 | return nullptr; |
| 3170 | |
| 3171 | Subs.pop_back(); |
| 3172 | return SoFar; |
| 3173 | } |
| 3174 | |
| 3175 | // <simple-id> ::= <source-name> [ <template-args> ] |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3176 | template <typename Derived, typename Alloc> |
| 3177 | Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() { |
| 3178 | Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3179 | if (SN == nullptr) |
| 3180 | return nullptr; |
| 3181 | if (look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3182 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3183 | if (TA == nullptr) |
| 3184 | return nullptr; |
| 3185 | return make<NameWithTemplateArgs>(SN, TA); |
| 3186 | } |
| 3187 | return SN; |
| 3188 | } |
| 3189 | |
| 3190 | // <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f()) |
| 3191 | // ::= <simple-id> # e.g., ~A<2*N> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3192 | template <typename Derived, typename Alloc> |
| 3193 | Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3194 | Node *Result; |
| 3195 | if (std::isdigit(look())) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3196 | Result = getDerived().parseSimpleId(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3197 | else |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3198 | Result = getDerived().parseUnresolvedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3199 | if (Result == nullptr) |
| 3200 | return nullptr; |
| 3201 | return make<DtorName>(Result); |
| 3202 | } |
| 3203 | |
| 3204 | // <unresolved-type> ::= <template-param> |
| 3205 | // ::= <decltype> |
| 3206 | // ::= <substitution> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3207 | template <typename Derived, typename Alloc> |
| 3208 | Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3209 | if (look() == 'T') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3210 | Node *TP = getDerived().parseTemplateParam(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3211 | if (TP == nullptr) |
| 3212 | return nullptr; |
| 3213 | Subs.push_back(TP); |
| 3214 | return TP; |
| 3215 | } |
| 3216 | if (look() == 'D') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3217 | Node *DT = getDerived().parseDecltype(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3218 | if (DT == nullptr) |
| 3219 | return nullptr; |
| 3220 | Subs.push_back(DT); |
| 3221 | return DT; |
| 3222 | } |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3223 | return getDerived().parseSubstitution(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | // <base-unresolved-name> ::= <simple-id> # unresolved name |
| 3227 | // extension ::= <operator-name> # unresolved operator-function-id |
| 3228 | // extension ::= <operator-name> <template-args> # unresolved operator template-id |
| 3229 | // ::= on <operator-name> # unresolved operator-function-id |
| 3230 | // ::= on <operator-name> <template-args> # unresolved operator template-id |
| 3231 | // ::= dn <destructor-name> # destructor or pseudo-destructor; |
| 3232 | // # e.g. ~X or ~X<N-1> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3233 | template <typename Derived, typename Alloc> |
| 3234 | Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3235 | if (std::isdigit(look())) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3236 | return getDerived().parseSimpleId(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3237 | |
| 3238 | if (consumeIf("dn")) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3239 | return getDerived().parseDestructorName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3240 | |
| 3241 | consumeIf("on"); |
| 3242 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3243 | Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3244 | if (Oper == nullptr) |
| 3245 | return nullptr; |
| 3246 | if (look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3247 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3248 | if (TA == nullptr) |
| 3249 | return nullptr; |
| 3250 | return make<NameWithTemplateArgs>(Oper, TA); |
| 3251 | } |
| 3252 | return Oper; |
| 3253 | } |
| 3254 | |
| 3255 | // <unresolved-name> |
| 3256 | // extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name> |
| 3257 | // ::= [gs] <base-unresolved-name> # x or (with "gs") ::x |
| 3258 | // ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name> |
| 3259 | // # A::x, N::y, A<T>::z; "gs" means leading "::" |
Nathan Sidwell | 77c52e2 | 2022-01-28 11:59:03 -0800 | [diff] [blame] | 3260 | // [gs] has been parsed by caller. |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3261 | // ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x |
| 3262 | // extension ::= sr <unresolved-type> <template-args> <base-unresolved-name> |
| 3263 | // # T::N::x /decltype(p)::N::x |
| 3264 | // (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name> |
| 3265 | // |
| 3266 | // <unresolved-qualifier-level> ::= <simple-id> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3267 | template <typename Derived, typename Alloc> |
Nathan Sidwell | 77c52e2 | 2022-01-28 11:59:03 -0800 | [diff] [blame] | 3268 | Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3269 | Node *SoFar = nullptr; |
| 3270 | |
| 3271 | // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name> |
| 3272 | // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name> |
| 3273 | if (consumeIf("srN")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3274 | SoFar = getDerived().parseUnresolvedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3275 | if (SoFar == nullptr) |
| 3276 | return nullptr; |
| 3277 | |
| 3278 | if (look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3279 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3280 | if (TA == nullptr) |
| 3281 | return nullptr; |
| 3282 | SoFar = make<NameWithTemplateArgs>(SoFar, TA); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3283 | if (!SoFar) |
| 3284 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | while (!consumeIf('E')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3288 | Node *Qual = getDerived().parseSimpleId(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3289 | if (Qual == nullptr) |
| 3290 | return nullptr; |
| 3291 | SoFar = make<QualifiedName>(SoFar, Qual); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3292 | if (!SoFar) |
| 3293 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3296 | Node *Base = getDerived().parseBaseUnresolvedName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3297 | if (Base == nullptr) |
| 3298 | return nullptr; |
| 3299 | return make<QualifiedName>(SoFar, Base); |
| 3300 | } |
| 3301 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3302 | // [gs] <base-unresolved-name> # x or (with "gs") ::x |
| 3303 | if (!consumeIf("sr")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3304 | SoFar = getDerived().parseBaseUnresolvedName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3305 | if (SoFar == nullptr) |
| 3306 | return nullptr; |
| 3307 | if (Global) |
| 3308 | SoFar = make<GlobalQualifiedName>(SoFar); |
| 3309 | return SoFar; |
| 3310 | } |
| 3311 | |
| 3312 | // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name> |
| 3313 | if (std::isdigit(look())) { |
| 3314 | do { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3315 | Node *Qual = getDerived().parseSimpleId(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3316 | if (Qual == nullptr) |
| 3317 | return nullptr; |
| 3318 | if (SoFar) |
| 3319 | SoFar = make<QualifiedName>(SoFar, Qual); |
| 3320 | else if (Global) |
| 3321 | SoFar = make<GlobalQualifiedName>(Qual); |
| 3322 | else |
| 3323 | SoFar = Qual; |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3324 | if (!SoFar) |
| 3325 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3326 | } while (!consumeIf('E')); |
| 3327 | } |
| 3328 | // sr <unresolved-type> <base-unresolved-name> |
| 3329 | // sr <unresolved-type> <template-args> <base-unresolved-name> |
| 3330 | else { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3331 | SoFar = getDerived().parseUnresolvedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3332 | if (SoFar == nullptr) |
| 3333 | return nullptr; |
| 3334 | |
| 3335 | if (look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3336 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3337 | if (TA == nullptr) |
| 3338 | return nullptr; |
| 3339 | SoFar = make<NameWithTemplateArgs>(SoFar, TA); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3340 | if (!SoFar) |
| 3341 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3342 | } |
| 3343 | } |
| 3344 | |
| 3345 | assert(SoFar != nullptr); |
| 3346 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3347 | Node *Base = getDerived().parseBaseUnresolvedName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3348 | if (Base == nullptr) |
| 3349 | return nullptr; |
| 3350 | return make<QualifiedName>(SoFar, Base); |
| 3351 | } |
| 3352 | |
| 3353 | // <abi-tags> ::= <abi-tag> [<abi-tags>] |
| 3354 | // <abi-tag> ::= B <source-name> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3355 | template <typename Derived, typename Alloc> |
| 3356 | Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3357 | while (consumeIf('B')) { |
| 3358 | StringView SN = parseBareSourceName(); |
| 3359 | if (SN.empty()) |
| 3360 | return nullptr; |
| 3361 | N = make<AbiTagAttr>(N, SN); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3362 | if (!N) |
| 3363 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3364 | } |
| 3365 | return N; |
| 3366 | } |
| 3367 | |
| 3368 | // <number> ::= [n] <non-negative decimal integer> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3369 | template <typename Alloc, typename Derived> |
| 3370 | StringView |
| 3371 | AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3372 | const char *Tmp = First; |
| 3373 | if (AllowNegative) |
| 3374 | consumeIf('n'); |
| 3375 | if (numLeft() == 0 || !std::isdigit(*First)) |
| 3376 | return StringView(); |
| 3377 | while (numLeft() != 0 && std::isdigit(*First)) |
| 3378 | ++First; |
| 3379 | return StringView(Tmp, First); |
| 3380 | } |
| 3381 | |
| 3382 | // <positive length number> ::= [0-9]* |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3383 | template <typename Alloc, typename Derived> |
| 3384 | bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3385 | *Out = 0; |
| 3386 | if (look() < '0' || look() > '9') |
| 3387 | return true; |
| 3388 | while (look() >= '0' && look() <= '9') { |
| 3389 | *Out *= 10; |
| 3390 | *Out += static_cast<size_t>(consume() - '0'); |
| 3391 | } |
| 3392 | return false; |
| 3393 | } |
| 3394 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3395 | template <typename Alloc, typename Derived> |
| 3396 | StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3397 | size_t Int = 0; |
| 3398 | if (parsePositiveInteger(&Int) || numLeft() < Int) |
| 3399 | return StringView(); |
| 3400 | StringView R(First, First + Int); |
| 3401 | First += Int; |
| 3402 | return R; |
| 3403 | } |
| 3404 | |
| 3405 | // <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E |
| 3406 | // |
| 3407 | // <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw()) |
| 3408 | // ::= DO <expression> E # computed (instantiation-dependent) noexcept |
| 3409 | // ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types |
| 3410 | // |
| 3411 | // <ref-qualifier> ::= R # & ref-qualifier |
| 3412 | // <ref-qualifier> ::= O # && ref-qualifier |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3413 | template <typename Derived, typename Alloc> |
| 3414 | Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3415 | Qualifiers CVQuals = parseCVQualifiers(); |
| 3416 | |
| 3417 | Node *ExceptionSpec = nullptr; |
| 3418 | if (consumeIf("Do")) { |
| 3419 | ExceptionSpec = make<NameType>("noexcept"); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3420 | if (!ExceptionSpec) |
| 3421 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3422 | } else if (consumeIf("DO")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3423 | Node *E = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3424 | if (E == nullptr || !consumeIf('E')) |
| 3425 | return nullptr; |
| 3426 | ExceptionSpec = make<NoexceptSpec>(E); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3427 | if (!ExceptionSpec) |
| 3428 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3429 | } else if (consumeIf("Dw")) { |
| 3430 | size_t SpecsBegin = Names.size(); |
| 3431 | while (!consumeIf('E')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3432 | Node *T = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3433 | if (T == nullptr) |
| 3434 | return nullptr; |
| 3435 | Names.push_back(T); |
| 3436 | } |
| 3437 | ExceptionSpec = |
| 3438 | make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin)); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 3439 | if (!ExceptionSpec) |
| 3440 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | consumeIf("Dx"); // transaction safe |
| 3444 | |
| 3445 | if (!consumeIf('F')) |
| 3446 | return nullptr; |
| 3447 | consumeIf('Y'); // extern "C" |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3448 | Node *ReturnType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3449 | if (ReturnType == nullptr) |
| 3450 | return nullptr; |
| 3451 | |
| 3452 | FunctionRefQual ReferenceQualifier = FrefQualNone; |
| 3453 | size_t ParamsBegin = Names.size(); |
| 3454 | while (true) { |
| 3455 | if (consumeIf('E')) |
| 3456 | break; |
| 3457 | if (consumeIf('v')) |
| 3458 | continue; |
| 3459 | if (consumeIf("RE")) { |
| 3460 | ReferenceQualifier = FrefQualLValue; |
| 3461 | break; |
| 3462 | } |
| 3463 | if (consumeIf("OE")) { |
| 3464 | ReferenceQualifier = FrefQualRValue; |
| 3465 | break; |
| 3466 | } |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3467 | Node *T = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3468 | if (T == nullptr) |
| 3469 | return nullptr; |
| 3470 | Names.push_back(T); |
| 3471 | } |
| 3472 | |
| 3473 | NodeArray Params = popTrailingNodeArray(ParamsBegin); |
| 3474 | return make<FunctionType>(ReturnType, Params, CVQuals, |
| 3475 | ReferenceQualifier, ExceptionSpec); |
| 3476 | } |
| 3477 | |
| 3478 | // extension: |
| 3479 | // <vector-type> ::= Dv <positive dimension number> _ <extended element type> |
| 3480 | // ::= Dv [<dimension expression>] _ <element type> |
| 3481 | // <extended element type> ::= <element type> |
| 3482 | // ::= p # AltiVec vector pixel |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3483 | template <typename Derived, typename Alloc> |
| 3484 | Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3485 | if (!consumeIf("Dv")) |
| 3486 | return nullptr; |
| 3487 | if (look() >= '1' && look() <= '9') { |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 3488 | Node *DimensionNumber = make<NameType>(parseNumber()); |
| 3489 | if (!DimensionNumber) |
| 3490 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3491 | if (!consumeIf('_')) |
| 3492 | return nullptr; |
| 3493 | if (consumeIf('p')) |
| 3494 | return make<PixelVectorType>(DimensionNumber); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3495 | Node *ElemType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3496 | if (ElemType == nullptr) |
| 3497 | return nullptr; |
| 3498 | return make<VectorType>(ElemType, DimensionNumber); |
| 3499 | } |
| 3500 | |
| 3501 | if (!consumeIf('_')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3502 | Node *DimExpr = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3503 | if (!DimExpr) |
| 3504 | return nullptr; |
| 3505 | if (!consumeIf('_')) |
| 3506 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3507 | Node *ElemType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3508 | if (!ElemType) |
| 3509 | return nullptr; |
| 3510 | return make<VectorType>(ElemType, DimExpr); |
| 3511 | } |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3512 | Node *ElemType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3513 | if (!ElemType) |
| 3514 | return nullptr; |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 3515 | return make<VectorType>(ElemType, /*Dimension=*/nullptr); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | // <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x) |
| 3519 | // ::= DT <expression> E # decltype of an expression (C++0x) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3520 | template <typename Derived, typename Alloc> |
| 3521 | Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3522 | if (!consumeIf('D')) |
| 3523 | return nullptr; |
| 3524 | if (!consumeIf('t') && !consumeIf('T')) |
| 3525 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3526 | Node *E = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3527 | if (E == nullptr) |
| 3528 | return nullptr; |
| 3529 | if (!consumeIf('E')) |
| 3530 | return nullptr; |
| 3531 | return make<EnclosingExpr>("decltype(", E, ")"); |
| 3532 | } |
| 3533 | |
| 3534 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 3535 | // ::= A [<dimension expression>] _ <element type> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3536 | template <typename Derived, typename Alloc> |
| 3537 | Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3538 | if (!consumeIf('A')) |
| 3539 | return nullptr; |
| 3540 | |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 3541 | Node *Dimension = nullptr; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3542 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3543 | if (std::isdigit(look())) { |
Erik Pilkington | d7555e3 | 2019-11-04 10:47:44 -0800 | [diff] [blame] | 3544 | Dimension = make<NameType>(parseNumber()); |
| 3545 | if (!Dimension) |
| 3546 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3547 | if (!consumeIf('_')) |
| 3548 | return nullptr; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3549 | } else if (!consumeIf('_')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3550 | Node *DimExpr = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3551 | if (DimExpr == nullptr) |
| 3552 | return nullptr; |
| 3553 | if (!consumeIf('_')) |
| 3554 | return nullptr; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3555 | Dimension = DimExpr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3558 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3559 | if (Ty == nullptr) |
| 3560 | return nullptr; |
Pavel Labath | f4e67eb | 2018-10-10 08:39:16 +0000 | [diff] [blame] | 3561 | return make<ArrayType>(Ty, Dimension); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | // <pointer-to-member-type> ::= M <class type> <member type> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3565 | template <typename Derived, typename Alloc> |
| 3566 | Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3567 | if (!consumeIf('M')) |
| 3568 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3569 | Node *ClassType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3570 | if (ClassType == nullptr) |
| 3571 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3572 | Node *MemberType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3573 | if (MemberType == nullptr) |
| 3574 | return nullptr; |
| 3575 | return make<PointerToMemberType>(ClassType, MemberType); |
| 3576 | } |
| 3577 | |
| 3578 | // <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier |
| 3579 | // ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class' |
| 3580 | // ::= Tu <name> # dependent elaborated type specifier using 'union' |
| 3581 | // ::= Te <name> # dependent elaborated type specifier using 'enum' |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3582 | template <typename Derived, typename Alloc> |
| 3583 | Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3584 | StringView ElabSpef; |
| 3585 | if (consumeIf("Ts")) |
| 3586 | ElabSpef = "struct"; |
| 3587 | else if (consumeIf("Tu")) |
| 3588 | ElabSpef = "union"; |
| 3589 | else if (consumeIf("Te")) |
| 3590 | ElabSpef = "enum"; |
| 3591 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3592 | Node *Name = getDerived().parseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3593 | if (Name == nullptr) |
| 3594 | return nullptr; |
| 3595 | |
| 3596 | if (!ElabSpef.empty()) |
| 3597 | return make<ElaboratedTypeSpefType>(ElabSpef, Name); |
| 3598 | |
| 3599 | return Name; |
| 3600 | } |
| 3601 | |
| 3602 | // <qualified-type> ::= <qualifiers> <type> |
| 3603 | // <qualifiers> ::= <extended-qualifier>* <CV-qualifiers> |
| 3604 | // <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3605 | template <typename Derived, typename Alloc> |
| 3606 | Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3607 | if (consumeIf('U')) { |
| 3608 | StringView Qual = parseBareSourceName(); |
| 3609 | if (Qual.empty()) |
| 3610 | return nullptr; |
| 3611 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3612 | // extension ::= U <objc-name> <objc-type> # objc-type<identifier> |
| 3613 | if (Qual.startsWith("objcproto")) { |
| 3614 | StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto")); |
| 3615 | StringView Proto; |
| 3616 | { |
| 3617 | SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()), |
| 3618 | SaveLast(Last, ProtoSourceName.end()); |
| 3619 | Proto = parseBareSourceName(); |
| 3620 | } |
| 3621 | if (Proto.empty()) |
| 3622 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3623 | Node *Child = getDerived().parseQualifiedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3624 | if (Child == nullptr) |
| 3625 | return nullptr; |
| 3626 | return make<ObjCProtoName>(Child, Proto); |
| 3627 | } |
| 3628 | |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 3629 | Node *TA = nullptr; |
| 3630 | if (look() == 'I') { |
| 3631 | TA = getDerived().parseTemplateArgs(); |
| 3632 | if (TA == nullptr) |
| 3633 | return nullptr; |
| 3634 | } |
| 3635 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3636 | Node *Child = getDerived().parseQualifiedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3637 | if (Child == nullptr) |
| 3638 | return nullptr; |
Alex Orlov | f50df92 | 2021-03-24 10:21:32 +0400 | [diff] [blame] | 3639 | return make<VendorExtQualType>(Child, Qual, TA); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | Qualifiers Quals = parseCVQualifiers(); |
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; |
| 3646 | if (Quals != QualNone) |
| 3647 | Ty = make<QualType>(Ty, Quals); |
| 3648 | return Ty; |
| 3649 | } |
| 3650 | |
| 3651 | // <type> ::= <builtin-type> |
| 3652 | // ::= <qualified-type> |
| 3653 | // ::= <function-type> |
| 3654 | // ::= <class-enum-type> |
| 3655 | // ::= <array-type> |
| 3656 | // ::= <pointer-to-member-type> |
| 3657 | // ::= <template-param> |
| 3658 | // ::= <template-template-param> <template-args> |
| 3659 | // ::= <decltype> |
| 3660 | // ::= P <type> # pointer |
| 3661 | // ::= R <type> # l-value reference |
| 3662 | // ::= O <type> # r-value reference (C++11) |
| 3663 | // ::= C <type> # complex pair (C99) |
| 3664 | // ::= G <type> # imaginary (C99) |
| 3665 | // ::= <substitution> # See Compression below |
| 3666 | // extension ::= U <objc-name> <objc-type> # objc-type<identifier> |
| 3667 | // extension ::= <vector-type> # <vector-type> starts with Dv |
| 3668 | // |
| 3669 | // <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1 |
| 3670 | // <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] | 3671 | template <typename Derived, typename Alloc> |
| 3672 | Node *AbstractManglingParser<Derived, Alloc>::parseType() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3673 | Node *Result = nullptr; |
| 3674 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3675 | switch (look()) { |
| 3676 | // ::= <qualified-type> |
| 3677 | case 'r': |
| 3678 | case 'V': |
| 3679 | case 'K': { |
| 3680 | unsigned AfterQuals = 0; |
| 3681 | if (look(AfterQuals) == 'r') ++AfterQuals; |
| 3682 | if (look(AfterQuals) == 'V') ++AfterQuals; |
| 3683 | if (look(AfterQuals) == 'K') ++AfterQuals; |
| 3684 | |
| 3685 | if (look(AfterQuals) == 'F' || |
| 3686 | (look(AfterQuals) == 'D' && |
| 3687 | (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' || |
| 3688 | look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3689 | Result = getDerived().parseFunctionType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3690 | break; |
| 3691 | } |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 3692 | DEMANGLE_FALLTHROUGH; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3693 | } |
| 3694 | case 'U': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3695 | Result = getDerived().parseQualifiedType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3696 | break; |
| 3697 | } |
| 3698 | // <builtin-type> ::= v # void |
| 3699 | case 'v': |
| 3700 | ++First; |
| 3701 | return make<NameType>("void"); |
| 3702 | // ::= w # wchar_t |
| 3703 | case 'w': |
| 3704 | ++First; |
| 3705 | return make<NameType>("wchar_t"); |
| 3706 | // ::= b # bool |
| 3707 | case 'b': |
| 3708 | ++First; |
| 3709 | return make<NameType>("bool"); |
| 3710 | // ::= c # char |
| 3711 | case 'c': |
| 3712 | ++First; |
| 3713 | return make<NameType>("char"); |
| 3714 | // ::= a # signed char |
| 3715 | case 'a': |
| 3716 | ++First; |
| 3717 | return make<NameType>("signed char"); |
| 3718 | // ::= h # unsigned char |
| 3719 | case 'h': |
| 3720 | ++First; |
| 3721 | return make<NameType>("unsigned char"); |
| 3722 | // ::= s # short |
| 3723 | case 's': |
| 3724 | ++First; |
| 3725 | return make<NameType>("short"); |
| 3726 | // ::= t # unsigned short |
| 3727 | case 't': |
| 3728 | ++First; |
| 3729 | return make<NameType>("unsigned short"); |
| 3730 | // ::= i # int |
| 3731 | case 'i': |
| 3732 | ++First; |
| 3733 | return make<NameType>("int"); |
| 3734 | // ::= j # unsigned int |
| 3735 | case 'j': |
| 3736 | ++First; |
| 3737 | return make<NameType>("unsigned int"); |
| 3738 | // ::= l # long |
| 3739 | case 'l': |
| 3740 | ++First; |
| 3741 | return make<NameType>("long"); |
| 3742 | // ::= m # unsigned long |
| 3743 | case 'm': |
| 3744 | ++First; |
| 3745 | return make<NameType>("unsigned long"); |
| 3746 | // ::= x # long long, __int64 |
| 3747 | case 'x': |
| 3748 | ++First; |
| 3749 | return make<NameType>("long long"); |
| 3750 | // ::= y # unsigned long long, __int64 |
| 3751 | case 'y': |
| 3752 | ++First; |
| 3753 | return make<NameType>("unsigned long long"); |
| 3754 | // ::= n # __int128 |
| 3755 | case 'n': |
| 3756 | ++First; |
| 3757 | return make<NameType>("__int128"); |
| 3758 | // ::= o # unsigned __int128 |
| 3759 | case 'o': |
| 3760 | ++First; |
| 3761 | return make<NameType>("unsigned __int128"); |
| 3762 | // ::= f # float |
| 3763 | case 'f': |
| 3764 | ++First; |
| 3765 | return make<NameType>("float"); |
| 3766 | // ::= d # double |
| 3767 | case 'd': |
| 3768 | ++First; |
| 3769 | return make<NameType>("double"); |
| 3770 | // ::= e # long double, __float80 |
| 3771 | case 'e': |
| 3772 | ++First; |
| 3773 | return make<NameType>("long double"); |
| 3774 | // ::= g # __float128 |
| 3775 | case 'g': |
| 3776 | ++First; |
| 3777 | return make<NameType>("__float128"); |
| 3778 | // ::= z # ellipsis |
| 3779 | case 'z': |
| 3780 | ++First; |
| 3781 | return make<NameType>("..."); |
| 3782 | |
| 3783 | // <builtin-type> ::= u <source-name> # vendor extended type |
| 3784 | case 'u': { |
| 3785 | ++First; |
| 3786 | StringView Res = parseBareSourceName(); |
| 3787 | if (Res.empty()) |
| 3788 | return nullptr; |
Erik Pilkington | b94a1f4 | 2019-06-10 21:02:39 +0000 | [diff] [blame] | 3789 | // Typically, <builtin-type>s are not considered substitution candidates, |
| 3790 | // but the exception to that exception is vendor extended types (Itanium C++ |
| 3791 | // ABI 5.9.1). |
| 3792 | Result = make<NameType>(Res); |
| 3793 | break; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3794 | } |
| 3795 | case 'D': |
| 3796 | switch (look(1)) { |
| 3797 | // ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 3798 | case 'd': |
| 3799 | First += 2; |
| 3800 | return make<NameType>("decimal64"); |
| 3801 | // ::= De # IEEE 754r decimal floating point (128 bits) |
| 3802 | case 'e': |
| 3803 | First += 2; |
| 3804 | return make<NameType>("decimal128"); |
| 3805 | // ::= Df # IEEE 754r decimal floating point (32 bits) |
| 3806 | case 'f': |
| 3807 | First += 2; |
| 3808 | return make<NameType>("decimal32"); |
| 3809 | // ::= Dh # IEEE 754r half-precision floating point (16 bits) |
| 3810 | case 'h': |
| 3811 | First += 2; |
Stuart Brady | e8bf577 | 2021-06-07 16:30:22 +0100 | [diff] [blame] | 3812 | return make<NameType>("half"); |
Pengfei Wang | 50e90b8 | 2021-09-23 11:02:25 +0800 | [diff] [blame] | 3813 | // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits) |
| 3814 | case 'F': { |
| 3815 | First += 2; |
| 3816 | Node *DimensionNumber = make<NameType>(parseNumber()); |
| 3817 | if (!DimensionNumber) |
| 3818 | return nullptr; |
| 3819 | if (!consumeIf('_')) |
| 3820 | return nullptr; |
| 3821 | return make<BinaryFPType>(DimensionNumber); |
| 3822 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3823 | // ::= Di # char32_t |
| 3824 | case 'i': |
| 3825 | First += 2; |
| 3826 | return make<NameType>("char32_t"); |
| 3827 | // ::= Ds # char16_t |
| 3828 | case 's': |
| 3829 | First += 2; |
| 3830 | return make<NameType>("char16_t"); |
Erik Pilkington | c3780e8 | 2019-06-28 19:54:19 +0000 | [diff] [blame] | 3831 | // ::= Du # char8_t (C++2a, not yet in the Itanium spec) |
| 3832 | case 'u': |
| 3833 | First += 2; |
| 3834 | return make<NameType>("char8_t"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3835 | // ::= Da # auto (in dependent new-expressions) |
| 3836 | case 'a': |
| 3837 | First += 2; |
| 3838 | return make<NameType>("auto"); |
| 3839 | // ::= Dc # decltype(auto) |
| 3840 | case 'c': |
| 3841 | First += 2; |
| 3842 | return make<NameType>("decltype(auto)"); |
| 3843 | // ::= Dn # std::nullptr_t (i.e., decltype(nullptr)) |
| 3844 | case 'n': |
| 3845 | First += 2; |
| 3846 | return make<NameType>("std::nullptr_t"); |
| 3847 | |
| 3848 | // ::= <decltype> |
| 3849 | case 't': |
| 3850 | case 'T': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3851 | Result = getDerived().parseDecltype(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3852 | break; |
| 3853 | } |
| 3854 | // extension ::= <vector-type> # <vector-type> starts with Dv |
| 3855 | case 'v': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3856 | Result = getDerived().parseVectorType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3857 | break; |
| 3858 | } |
| 3859 | // ::= Dp <type> # pack expansion (C++0x) |
| 3860 | case 'p': { |
| 3861 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3862 | Node *Child = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3863 | if (!Child) |
| 3864 | return nullptr; |
| 3865 | Result = make<ParameterPackExpansion>(Child); |
| 3866 | break; |
| 3867 | } |
| 3868 | // Exception specifier on a function type. |
| 3869 | case 'o': |
| 3870 | case 'O': |
| 3871 | case 'w': |
| 3872 | // Transaction safe function type. |
| 3873 | case 'x': |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3874 | Result = getDerived().parseFunctionType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3875 | break; |
| 3876 | } |
| 3877 | break; |
| 3878 | // ::= <function-type> |
| 3879 | case 'F': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3880 | Result = getDerived().parseFunctionType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3881 | break; |
| 3882 | } |
| 3883 | // ::= <array-type> |
| 3884 | case 'A': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3885 | Result = getDerived().parseArrayType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3886 | break; |
| 3887 | } |
| 3888 | // ::= <pointer-to-member-type> |
| 3889 | case 'M': { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3890 | Result = getDerived().parsePointerToMemberType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3891 | break; |
| 3892 | } |
| 3893 | // ::= <template-param> |
| 3894 | case 'T': { |
| 3895 | // This could be an elaborate type specifier on a <class-enum-type>. |
| 3896 | if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3897 | Result = getDerived().parseClassEnumType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3898 | break; |
| 3899 | } |
| 3900 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3901 | Result = getDerived().parseTemplateParam(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3902 | if (Result == nullptr) |
| 3903 | return nullptr; |
| 3904 | |
| 3905 | // Result could be either of: |
| 3906 | // <type> ::= <template-param> |
| 3907 | // <type> ::= <template-template-param> <template-args> |
| 3908 | // |
| 3909 | // <template-template-param> ::= <template-param> |
| 3910 | // ::= <substitution> |
| 3911 | // |
| 3912 | // If this is followed by some <template-args>, and we're permitted to |
| 3913 | // parse them, take the second production. |
| 3914 | |
| 3915 | if (TryToParseTemplateArgs && look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3916 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3917 | if (TA == nullptr) |
| 3918 | return nullptr; |
| 3919 | Result = make<NameWithTemplateArgs>(Result, TA); |
| 3920 | } |
| 3921 | break; |
| 3922 | } |
| 3923 | // ::= P <type> # pointer |
| 3924 | case 'P': { |
| 3925 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3926 | Node *Ptr = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3927 | if (Ptr == nullptr) |
| 3928 | return nullptr; |
| 3929 | Result = make<PointerType>(Ptr); |
| 3930 | break; |
| 3931 | } |
| 3932 | // ::= R <type> # l-value reference |
| 3933 | case 'R': { |
| 3934 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3935 | Node *Ref = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3936 | if (Ref == nullptr) |
| 3937 | return nullptr; |
| 3938 | Result = make<ReferenceType>(Ref, ReferenceKind::LValue); |
| 3939 | break; |
| 3940 | } |
| 3941 | // ::= O <type> # r-value reference (C++11) |
| 3942 | case 'O': { |
| 3943 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3944 | Node *Ref = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3945 | if (Ref == nullptr) |
| 3946 | return nullptr; |
| 3947 | Result = make<ReferenceType>(Ref, ReferenceKind::RValue); |
| 3948 | break; |
| 3949 | } |
| 3950 | // ::= C <type> # complex pair (C99) |
| 3951 | case 'C': { |
| 3952 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3953 | Node *P = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3954 | if (P == nullptr) |
| 3955 | return nullptr; |
| 3956 | Result = make<PostfixQualifiedType>(P, " complex"); |
| 3957 | break; |
| 3958 | } |
| 3959 | // ::= G <type> # imaginary (C99) |
| 3960 | case 'G': { |
| 3961 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3962 | Node *P = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3963 | if (P == nullptr) |
| 3964 | return P; |
| 3965 | Result = make<PostfixQualifiedType>(P, " imaginary"); |
| 3966 | break; |
| 3967 | } |
| 3968 | // ::= <substitution> # See Compression below |
| 3969 | case 'S': { |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 3970 | if (look(1) != 't') { |
| 3971 | Result = getDerived().parseSubstitution(); |
| 3972 | if (Result == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3973 | return nullptr; |
| 3974 | |
| 3975 | // Sub could be either of: |
| 3976 | // <type> ::= <substitution> |
| 3977 | // <type> ::= <template-template-param> <template-args> |
| 3978 | // |
| 3979 | // <template-template-param> ::= <template-param> |
| 3980 | // ::= <substitution> |
| 3981 | // |
| 3982 | // If this is followed by some <template-args>, and we're permitted to |
| 3983 | // parse them, take the second production. |
| 3984 | |
| 3985 | if (TryToParseTemplateArgs && look() == 'I') { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 3986 | Node *TA = getDerived().parseTemplateArgs(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3987 | if (TA == nullptr) |
| 3988 | return nullptr; |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 3989 | Result = make<NameWithTemplateArgs>(Result, TA); |
| 3990 | } else { |
| 3991 | // If all we parsed was a substitution, don't re-insert into the |
| 3992 | // substitution table. |
| 3993 | return Result; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3994 | } |
Nathan Sidwell | e4cc353 | 2022-01-24 04:28:09 -0800 | [diff] [blame] | 3995 | break; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3996 | } |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 3997 | DEMANGLE_FALLTHROUGH; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 3998 | } |
| 3999 | // ::= <class-enum-type> |
| 4000 | default: { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4001 | Result = getDerived().parseClassEnumType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4002 | break; |
| 4003 | } |
| 4004 | } |
| 4005 | |
| 4006 | // If we parsed a type, insert it into the substitution table. Note that all |
| 4007 | // <builtin-type>s and <substitution>s have already bailed out, because they |
| 4008 | // don't get substitutions. |
| 4009 | if (Result != nullptr) |
| 4010 | Subs.push_back(Result); |
| 4011 | return Result; |
| 4012 | } |
| 4013 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4014 | template <typename Derived, typename Alloc> |
| 4015 | Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) { |
| 4016 | Node *E = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4017 | if (E == nullptr) |
| 4018 | return nullptr; |
| 4019 | return make<PrefixExpr>(Kind, E); |
| 4020 | } |
| 4021 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4022 | template <typename Derived, typename Alloc> |
| 4023 | Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) { |
| 4024 | Node *LHS = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4025 | if (LHS == nullptr) |
| 4026 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4027 | Node *RHS = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4028 | if (RHS == nullptr) |
| 4029 | return nullptr; |
| 4030 | return make<BinaryExpr>(LHS, Kind, RHS); |
| 4031 | } |
| 4032 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4033 | template <typename Derived, typename Alloc> |
| 4034 | Node * |
| 4035 | AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4036 | StringView Tmp = parseNumber(true); |
| 4037 | if (!Tmp.empty() && consumeIf('E')) |
| 4038 | return make<IntegerLiteral>(Lit, Tmp); |
| 4039 | return nullptr; |
| 4040 | } |
| 4041 | |
| 4042 | // <CV-Qualifiers> ::= [r] [V] [K] |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4043 | template <typename Alloc, typename Derived> |
| 4044 | Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4045 | Qualifiers CVR = QualNone; |
| 4046 | if (consumeIf('r')) |
| 4047 | CVR |= QualRestrict; |
| 4048 | if (consumeIf('V')) |
| 4049 | CVR |= QualVolatile; |
| 4050 | if (consumeIf('K')) |
| 4051 | CVR |= QualConst; |
| 4052 | return CVR; |
| 4053 | } |
| 4054 | |
| 4055 | // <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter |
| 4056 | // ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters |
| 4057 | // ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter |
| 4058 | // ::= 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] | 4059 | // ::= fpT # 'this' expression (not part of standard?) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4060 | template <typename Derived, typename Alloc> |
| 4061 | Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() { |
Erik Pilkington | 91c24af | 2020-05-13 22:19:45 -0400 | [diff] [blame] | 4062 | if (consumeIf("fpT")) |
| 4063 | return make<NameType>("this"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4064 | if (consumeIf("fp")) { |
| 4065 | parseCVQualifiers(); |
| 4066 | StringView Num = parseNumber(); |
| 4067 | if (!consumeIf('_')) |
| 4068 | return nullptr; |
| 4069 | return make<FunctionParam>(Num); |
| 4070 | } |
| 4071 | if (consumeIf("fL")) { |
| 4072 | if (parseNumber().empty()) |
| 4073 | return nullptr; |
| 4074 | if (!consumeIf('p')) |
| 4075 | return nullptr; |
| 4076 | parseCVQualifiers(); |
| 4077 | StringView Num = parseNumber(); |
| 4078 | if (!consumeIf('_')) |
| 4079 | return nullptr; |
| 4080 | return make<FunctionParam>(Num); |
| 4081 | } |
| 4082 | return nullptr; |
| 4083 | } |
| 4084 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4085 | // cv <type> <expression> # conversion with one argument |
| 4086 | // cv <type> _ <expression>* E # conversion with a different number of arguments |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4087 | template <typename Derived, typename Alloc> |
| 4088 | Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4089 | if (!consumeIf("cv")) |
| 4090 | return nullptr; |
| 4091 | Node *Ty; |
| 4092 | { |
| 4093 | SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4094 | Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | if (Ty == nullptr) |
| 4098 | return nullptr; |
| 4099 | |
| 4100 | if (consumeIf('_')) { |
| 4101 | size_t ExprsBegin = Names.size(); |
| 4102 | while (!consumeIf('E')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4103 | Node *E = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4104 | if (E == nullptr) |
| 4105 | return E; |
| 4106 | Names.push_back(E); |
| 4107 | } |
| 4108 | NodeArray Exprs = popTrailingNodeArray(ExprsBegin); |
| 4109 | return make<ConversionExpr>(Ty, Exprs); |
| 4110 | } |
| 4111 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4112 | Node *E[1] = {getDerived().parseExpr()}; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4113 | if (E[0] == nullptr) |
| 4114 | return nullptr; |
| 4115 | return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1)); |
| 4116 | } |
| 4117 | |
| 4118 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 4119 | // ::= L <type> <value float> E # floating literal |
| 4120 | // ::= L <string type> E # string literal |
| 4121 | // ::= L <nullptr type> E # nullptr literal (i.e., "LDnE") |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 4122 | // ::= L <lambda type> E # lambda expression |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4123 | // FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000) |
| 4124 | // ::= L <mangled-name> E # external name |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4125 | template <typename Derived, typename Alloc> |
| 4126 | Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4127 | if (!consumeIf('L')) |
| 4128 | return nullptr; |
| 4129 | switch (look()) { |
| 4130 | case 'w': |
| 4131 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4132 | return getDerived().parseIntegerLiteral("wchar_t"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4133 | case 'b': |
| 4134 | if (consumeIf("b0E")) |
| 4135 | return make<BoolExpr>(0); |
| 4136 | if (consumeIf("b1E")) |
| 4137 | return make<BoolExpr>(1); |
| 4138 | return nullptr; |
| 4139 | case 'c': |
| 4140 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4141 | return getDerived().parseIntegerLiteral("char"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4142 | case 'a': |
| 4143 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4144 | return getDerived().parseIntegerLiteral("signed char"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4145 | case 'h': |
| 4146 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4147 | return getDerived().parseIntegerLiteral("unsigned char"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4148 | case 's': |
| 4149 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4150 | return getDerived().parseIntegerLiteral("short"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4151 | case 't': |
| 4152 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4153 | return getDerived().parseIntegerLiteral("unsigned short"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4154 | case 'i': |
| 4155 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4156 | return getDerived().parseIntegerLiteral(""); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4157 | case 'j': |
| 4158 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4159 | return getDerived().parseIntegerLiteral("u"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4160 | case 'l': |
| 4161 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4162 | return getDerived().parseIntegerLiteral("l"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4163 | case 'm': |
| 4164 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4165 | return getDerived().parseIntegerLiteral("ul"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4166 | case 'x': |
| 4167 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4168 | return getDerived().parseIntegerLiteral("ll"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4169 | case 'y': |
| 4170 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4171 | return getDerived().parseIntegerLiteral("ull"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4172 | case 'n': |
| 4173 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4174 | return getDerived().parseIntegerLiteral("__int128"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4175 | case 'o': |
| 4176 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4177 | return getDerived().parseIntegerLiteral("unsigned __int128"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4178 | case 'f': |
| 4179 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4180 | return getDerived().template parseFloatingLiteral<float>(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4181 | case 'd': |
| 4182 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4183 | return getDerived().template parseFloatingLiteral<double>(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4184 | case 'e': |
| 4185 | ++First; |
Xing Xue | 3dc5e08 | 2020-04-15 09:59:06 -0400 | [diff] [blame] | 4186 | #if defined(__powerpc__) || defined(__s390__) |
| 4187 | // Handle cases where long doubles encoded with e have the same size |
| 4188 | // and representation as doubles. |
| 4189 | return getDerived().template parseFloatingLiteral<double>(); |
| 4190 | #else |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4191 | return getDerived().template parseFloatingLiteral<long double>(); |
Xing Xue | 3dc5e08 | 2020-04-15 09:59:06 -0400 | [diff] [blame] | 4192 | #endif |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4193 | case '_': |
| 4194 | if (consumeIf("_Z")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4195 | Node *R = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4196 | if (R != nullptr && consumeIf('E')) |
| 4197 | return R; |
| 4198 | } |
| 4199 | return nullptr; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 4200 | case 'A': { |
| 4201 | Node *T = getDerived().parseType(); |
| 4202 | if (T == nullptr) |
| 4203 | return nullptr; |
| 4204 | // FIXME: We need to include the string contents in the mangling. |
| 4205 | if (consumeIf('E')) |
| 4206 | return make<StringLiteral>(T); |
| 4207 | return nullptr; |
| 4208 | } |
| 4209 | case 'D': |
| 4210 | if (consumeIf("DnE")) |
| 4211 | return make<NameType>("nullptr"); |
| 4212 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4213 | case 'T': |
| 4214 | // Invalid mangled name per |
| 4215 | // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html |
| 4216 | return nullptr; |
Richard Smith | fb91746 | 2019-09-09 22:26:04 +0000 | [diff] [blame] | 4217 | case 'U': { |
| 4218 | // FIXME: Should we support LUb... for block literals? |
| 4219 | if (look(1) != 'l') |
| 4220 | return nullptr; |
| 4221 | Node *T = parseUnnamedTypeName(nullptr); |
| 4222 | if (!T || !consumeIf('E')) |
| 4223 | return nullptr; |
| 4224 | return make<LambdaExpr>(T); |
| 4225 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4226 | default: { |
| 4227 | // might be named type |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4228 | Node *T = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4229 | if (T == nullptr) |
| 4230 | return nullptr; |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 4231 | StringView N = parseNumber(/*AllowNegative=*/true); |
Richard Smith | fb91746 | 2019-09-09 22:26:04 +0000 | [diff] [blame] | 4232 | if (N.empty()) |
| 4233 | return nullptr; |
| 4234 | if (!consumeIf('E')) |
| 4235 | return nullptr; |
Erik Pilkington | 0a170f1 | 2020-05-13 14:13:37 -0400 | [diff] [blame] | 4236 | return make<EnumLiteral>(T, N); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4237 | } |
| 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | // <braced-expression> ::= <expression> |
| 4242 | // ::= di <field source-name> <braced-expression> # .name = expr |
| 4243 | // ::= dx <index expression> <braced-expression> # [expr] = expr |
| 4244 | // ::= dX <range begin expression> <range end expression> <braced-expression> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4245 | template <typename Derived, typename Alloc> |
| 4246 | Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4247 | if (look() == 'd') { |
| 4248 | switch (look(1)) { |
| 4249 | case 'i': { |
| 4250 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4251 | Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4252 | if (Field == nullptr) |
| 4253 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4254 | Node *Init = getDerived().parseBracedExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4255 | if (Init == nullptr) |
| 4256 | return nullptr; |
| 4257 | return make<BracedExpr>(Field, Init, /*isArray=*/false); |
| 4258 | } |
| 4259 | case 'x': { |
| 4260 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4261 | Node *Index = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4262 | if (Index == nullptr) |
| 4263 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4264 | Node *Init = getDerived().parseBracedExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4265 | if (Init == nullptr) |
| 4266 | return nullptr; |
| 4267 | return make<BracedExpr>(Index, Init, /*isArray=*/true); |
| 4268 | } |
| 4269 | case 'X': { |
| 4270 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4271 | Node *RangeBegin = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4272 | if (RangeBegin == nullptr) |
| 4273 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4274 | Node *RangeEnd = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4275 | if (RangeEnd == nullptr) |
| 4276 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4277 | Node *Init = getDerived().parseBracedExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4278 | if (Init == nullptr) |
| 4279 | return nullptr; |
| 4280 | return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init); |
| 4281 | } |
| 4282 | } |
| 4283 | } |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4284 | return getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | // (not yet in the spec) |
| 4288 | // <fold-expr> ::= fL <binary-operator-name> <expression> <expression> |
| 4289 | // ::= fR <binary-operator-name> <expression> <expression> |
| 4290 | // ::= fl <binary-operator-name> <expression> |
| 4291 | // ::= fr <binary-operator-name> <expression> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4292 | template <typename Derived, typename Alloc> |
| 4293 | Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4294 | if (!consumeIf('f')) |
| 4295 | return nullptr; |
| 4296 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4297 | bool IsLeftFold = false, HasInitializer = false; |
| 4298 | switch (look()) { |
| 4299 | default: |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4300 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4301 | case 'L': |
| 4302 | IsLeftFold = true; |
| 4303 | HasInitializer = true; |
| 4304 | break; |
| 4305 | case 'R': |
| 4306 | HasInitializer = true; |
| 4307 | break; |
| 4308 | case 'l': |
| 4309 | IsLeftFold = true; |
| 4310 | break; |
| 4311 | case 'r': |
| 4312 | break; |
| 4313 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4314 | ++First; |
| 4315 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4316 | const auto *Op = parseOperatorEncoding(); |
| 4317 | if (!Op || Op->getKind() != OperatorInfo::Binary) |
| 4318 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4319 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4320 | Node *Pack = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4321 | if (Pack == nullptr) |
| 4322 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4323 | |
| 4324 | Node *Init = nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4325 | if (HasInitializer) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4326 | Init = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4327 | if (Init == nullptr) |
| 4328 | return nullptr; |
| 4329 | } |
| 4330 | |
| 4331 | if (IsLeftFold && Init) |
| 4332 | std::swap(Pack, Init); |
| 4333 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4334 | return make<FoldExpr>(IsLeftFold, Op->getSymbol(), Pack, Init); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 4337 | // <expression> ::= mc <parameter type> <expr> [<offset number>] E |
| 4338 | // |
| 4339 | // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47 |
| 4340 | template <typename Derived, typename Alloc> |
| 4341 | Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() { |
| 4342 | Node *Ty = getDerived().parseType(); |
| 4343 | if (!Ty) |
| 4344 | return nullptr; |
| 4345 | Node *Expr = getDerived().parseExpr(); |
| 4346 | if (!Expr) |
| 4347 | return nullptr; |
| 4348 | StringView Offset = getDerived().parseNumber(true); |
| 4349 | if (!consumeIf('E')) |
| 4350 | return nullptr; |
| 4351 | return make<PointerToMemberConversionExpr>(Ty, Expr, Offset); |
| 4352 | } |
| 4353 | |
| 4354 | // <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E |
| 4355 | // <union-selector> ::= _ [<number>] |
| 4356 | // |
| 4357 | // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47 |
| 4358 | template <typename Derived, typename Alloc> |
| 4359 | Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() { |
| 4360 | Node *Ty = getDerived().parseType(); |
| 4361 | if (!Ty) |
| 4362 | return nullptr; |
| 4363 | Node *Expr = getDerived().parseExpr(); |
| 4364 | if (!Expr) |
| 4365 | return nullptr; |
| 4366 | StringView Offset = getDerived().parseNumber(true); |
| 4367 | size_t SelectorsBegin = Names.size(); |
| 4368 | while (consumeIf('_')) { |
| 4369 | Node *Selector = make<NameType>(parseNumber()); |
| 4370 | if (!Selector) |
| 4371 | return nullptr; |
| 4372 | Names.push_back(Selector); |
| 4373 | } |
| 4374 | bool OnePastTheEnd = consumeIf('p'); |
| 4375 | if (!consumeIf('E')) |
| 4376 | return nullptr; |
| 4377 | return make<SubobjectExpr>( |
| 4378 | Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd); |
| 4379 | } |
| 4380 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4381 | // <expression> ::= <unary operator-name> <expression> |
| 4382 | // ::= <binary operator-name> <expression> <expression> |
| 4383 | // ::= <ternary operator-name> <expression> <expression> <expression> |
| 4384 | // ::= cl <expression>+ E # call |
| 4385 | // ::= cv <type> <expression> # conversion with one argument |
| 4386 | // ::= cv <type> _ <expression>* E # conversion with a different number of arguments |
| 4387 | // ::= [gs] nw <expression>* _ <type> E # new (expr-list) type |
| 4388 | // ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init) |
| 4389 | // ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type |
| 4390 | // ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init) |
| 4391 | // ::= [gs] dl <expression> # delete expression |
| 4392 | // ::= [gs] da <expression> # delete[] expression |
| 4393 | // ::= pp_ <expression> # prefix ++ |
| 4394 | // ::= mm_ <expression> # prefix -- |
| 4395 | // ::= ti <type> # typeid (type) |
| 4396 | // ::= te <expression> # typeid (expression) |
| 4397 | // ::= dc <type> <expression> # dynamic_cast<type> (expression) |
| 4398 | // ::= sc <type> <expression> # static_cast<type> (expression) |
| 4399 | // ::= cc <type> <expression> # const_cast<type> (expression) |
| 4400 | // ::= rc <type> <expression> # reinterpret_cast<type> (expression) |
| 4401 | // ::= st <type> # sizeof (a type) |
| 4402 | // ::= sz <expression> # sizeof (an expression) |
| 4403 | // ::= at <type> # alignof (a type) |
| 4404 | // ::= az <expression> # alignof (an expression) |
| 4405 | // ::= nx <expression> # noexcept (expression) |
| 4406 | // ::= <template-param> |
| 4407 | // ::= <function-param> |
| 4408 | // ::= dt <expression> <unresolved-name> # expr.name |
| 4409 | // ::= pt <expression> <unresolved-name> # expr->name |
| 4410 | // ::= ds <expression> <expression> # expr.*expr |
| 4411 | // ::= sZ <template-param> # size of a parameter pack |
| 4412 | // ::= sZ <function-param> # size of a function parameter pack |
| 4413 | // ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template |
| 4414 | // ::= sp <expression> # pack expansion |
| 4415 | // ::= tw <expression> # throw expression |
| 4416 | // ::= tr # throw with no operand (rethrow) |
| 4417 | // ::= <unresolved-name> # f(p), N::f(p), ::f(p), |
| 4418 | // # freestanding dependent name (e.g., T::x), |
| 4419 | // # objectless nonstatic member reference |
| 4420 | // ::= fL <binary-operator-name> <expression> <expression> |
| 4421 | // ::= fR <binary-operator-name> <expression> <expression> |
| 4422 | // ::= fl <binary-operator-name> <expression> |
| 4423 | // ::= fr <binary-operator-name> <expression> |
| 4424 | // ::= <expr-primary> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4425 | template <typename Derived, typename Alloc> |
| 4426 | Node *AbstractManglingParser<Derived, Alloc>::parseExpr() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4427 | bool Global = consumeIf("gs"); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4428 | |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4429 | const auto *Op = parseOperatorEncoding(); |
| 4430 | if (Op) { |
| 4431 | auto Sym = Op->getSymbol(); |
| 4432 | switch (Op->getKind()) { |
| 4433 | case OperatorInfo::Binary: |
| 4434 | // Binary operator: lhs @ rhs |
| 4435 | return getDerived().parseBinaryExpr(Sym); |
| 4436 | case OperatorInfo::Prefix: |
| 4437 | // Prefix unary operator: @ expr |
| 4438 | return getDerived().parsePrefixExpr(Sym); |
| 4439 | case OperatorInfo::Postfix: { |
| 4440 | // Postfix unary operator: expr @ |
| 4441 | if (consumeIf('_')) |
| 4442 | return getDerived().parsePrefixExpr(Sym); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4443 | Node *Ex = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4444 | if (Ex == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4445 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4446 | return make<PostfixExpr>(Ex, Sym); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4447 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4448 | case OperatorInfo::Array: { |
| 4449 | // Array Index: lhs [ rhs ] |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4450 | Node *Base = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4451 | if (Base == nullptr) |
| 4452 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4453 | Node *Index = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4454 | if (Index == nullptr) |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4455 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4456 | return make<ArraySubscriptExpr>(Base, Index); |
| 4457 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4458 | case OperatorInfo::Member: { |
| 4459 | // Member access lhs @ rhs |
| 4460 | Node *LHS = getDerived().parseExpr(); |
| 4461 | if (LHS == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4462 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4463 | Node *RHS = getDerived().parseExpr(); |
| 4464 | if (RHS == nullptr) |
| 4465 | return nullptr; |
| 4466 | return make<MemberExpr>(LHS, Sym, RHS); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4467 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4468 | case OperatorInfo::New: { |
| 4469 | // New |
| 4470 | // # new (expr-list) type [(init)] |
| 4471 | // [gs] nw <expression>* _ <type> [pi <expression>*] E |
| 4472 | // # new[] (expr-list) type [(init)] |
| 4473 | // [gs] na <expression>* _ <type> [pi <expression>*] E |
Nathan Sidwell | c69bde2 | 2022-01-28 07:09:38 -0800 | [diff] [blame] | 4474 | size_t Exprs = Names.size(); |
| 4475 | while (!consumeIf('_')) { |
| 4476 | Node *Ex = getDerived().parseExpr(); |
| 4477 | if (Ex == nullptr) |
| 4478 | return nullptr; |
| 4479 | Names.push_back(Ex); |
| 4480 | } |
| 4481 | NodeArray ExprList = popTrailingNodeArray(Exprs); |
| 4482 | Node *Ty = getDerived().parseType(); |
| 4483 | if (Ty == nullptr) |
| 4484 | return nullptr; |
| 4485 | bool HaveInits = consumeIf("pi"); |
| 4486 | size_t InitsBegin = Names.size(); |
| 4487 | while (!consumeIf('E')) { |
| 4488 | if (!HaveInits) |
| 4489 | return nullptr; |
| 4490 | Node *Init = getDerived().parseExpr(); |
| 4491 | if (Init == nullptr) |
| 4492 | return Init; |
| 4493 | Names.push_back(Init); |
| 4494 | } |
| 4495 | NodeArray Inits = popTrailingNodeArray(InitsBegin); |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4496 | return make<NewExpr>(ExprList, Ty, Inits, Global, |
| 4497 | /*IsArray=*/Op->getFlag()); |
Nathan Sidwell | c69bde2 | 2022-01-28 07:09:38 -0800 | [diff] [blame] | 4498 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4499 | case OperatorInfo::Del: { |
| 4500 | // Delete |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4501 | Node *Ex = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4502 | if (Ex == nullptr) |
Nathan Sidwell | c648304 | 2022-01-28 09:27:28 -0800 | [diff] [blame] | 4503 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4504 | return make<DeleteExpr>(Ex, Global, /*IsArray=*/Op->getFlag()); |
Nathan Sidwell | c648304 | 2022-01-28 09:27:28 -0800 | [diff] [blame] | 4505 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4506 | case OperatorInfo::Call: { |
| 4507 | // Function Call |
| 4508 | Node *Callee = getDerived().parseExpr(); |
| 4509 | if (Callee == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4510 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4511 | size_t ExprsBegin = Names.size(); |
| 4512 | while (!consumeIf('E')) { |
| 4513 | Node *E = getDerived().parseExpr(); |
| 4514 | if (E == nullptr) |
| 4515 | return nullptr; |
| 4516 | Names.push_back(E); |
| 4517 | } |
| 4518 | return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin)); |
| 4519 | } |
| 4520 | case OperatorInfo::CCast: { |
| 4521 | // C Cast: (type)expr |
| 4522 | Node *Ty; |
| 4523 | { |
| 4524 | SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false); |
| 4525 | Ty = getDerived().parseType(); |
| 4526 | } |
| 4527 | if (Ty == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4528 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4529 | |
| 4530 | size_t ExprsBegin = Names.size(); |
| 4531 | bool IsMany = consumeIf('_'); |
| 4532 | while (!consumeIf('E')) { |
| 4533 | Node *E = getDerived().parseExpr(); |
| 4534 | if (E == nullptr) |
| 4535 | return E; |
| 4536 | Names.push_back(E); |
| 4537 | if (!IsMany) |
| 4538 | break; |
| 4539 | } |
| 4540 | NodeArray Exprs = popTrailingNodeArray(ExprsBegin); |
| 4541 | if (!IsMany && Exprs.size() != 1) |
| 4542 | return nullptr; |
| 4543 | return make<ConversionExpr>(Ty, Exprs); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4544 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4545 | case OperatorInfo::Conditional: { |
| 4546 | // Conditional operator: expr ? expr : expr |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4547 | Node *Cond = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4548 | if (Cond == nullptr) |
| 4549 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4550 | Node *LHS = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4551 | if (LHS == nullptr) |
| 4552 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4553 | Node *RHS = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4554 | if (RHS == nullptr) |
| 4555 | return nullptr; |
| 4556 | return make<ConditionalExpr>(Cond, LHS, RHS); |
| 4557 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4558 | case OperatorInfo::NamedCast: { |
| 4559 | // Named cast operation, @<type>(expr) |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4560 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4561 | if (Ty == nullptr) |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4562 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4563 | Node *Ex = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4564 | if (Ex == nullptr) |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4565 | return nullptr; |
| 4566 | return make<CastExpr>(Sym, Ty, Ex); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4567 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4568 | case OperatorInfo::OfIdOp: { |
| 4569 | // [sizeof/alignof/typeid] ( <type>|<expr> ) |
| 4570 | Node *Arg = |
| 4571 | Op->getFlag() ? getDerived().parseType() : getDerived().parseExpr(); |
| 4572 | if (!Arg) |
| 4573 | return nullptr; |
| 4574 | return make<EnclosingExpr>(Sym, Arg, ")"); |
| 4575 | } |
| 4576 | } |
| 4577 | DEMANGLE_UNREACHABLE; |
| 4578 | } |
| 4579 | |
| 4580 | if (numLeft() < 2) |
| 4581 | return nullptr; |
| 4582 | |
| 4583 | if (look() == 'L') |
| 4584 | return getDerived().parseExprPrimary(); |
| 4585 | if (look() == 'T') |
| 4586 | return getDerived().parseTemplateParam(); |
| 4587 | if (look() == 'f') { |
| 4588 | // Disambiguate a fold expression from a <function-param>. |
| 4589 | if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2)))) |
| 4590 | return getDerived().parseFunctionParam(); |
| 4591 | return getDerived().parseFoldExpr(); |
| 4592 | } |
| 4593 | if (consumeIf("il")) { |
| 4594 | size_t InitsBegin = Names.size(); |
| 4595 | while (!consumeIf('E')) { |
| 4596 | Node *E = getDerived().parseBracedExpr(); |
| 4597 | if (E == nullptr) |
| 4598 | return nullptr; |
| 4599 | Names.push_back(E); |
| 4600 | } |
| 4601 | return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin)); |
| 4602 | } |
| 4603 | if (consumeIf("mc")) |
| 4604 | return parsePointerToMemberConversionExpr(); |
| 4605 | if (consumeIf("nx")) { |
| 4606 | Node *Ex = getDerived().parseExpr(); |
| 4607 | if (Ex == nullptr) |
| 4608 | return Ex; |
| 4609 | return make<EnclosingExpr>("noexcept (", Ex, ")"); |
| 4610 | } |
| 4611 | if (consumeIf("so")) |
| 4612 | return parseSubobjectExpr(); |
| 4613 | if (consumeIf("sp")) { |
| 4614 | Node *Child = getDerived().parseExpr(); |
| 4615 | if (Child == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4616 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4617 | return make<ParameterPackExpansion>(Child); |
| 4618 | } |
| 4619 | if (consumeIf("sZ")) { |
| 4620 | if (look() == 'T') { |
| 4621 | Node *R = getDerived().parseTemplateParam(); |
| 4622 | if (R == nullptr) |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 4623 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4624 | return make<SizeofParamPackExpr>(R); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4625 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4626 | Node *FP = getDerived().parseFunctionParam(); |
| 4627 | if (FP == nullptr) |
| 4628 | return nullptr; |
| 4629 | return make<EnclosingExpr>("sizeof... (", FP, ")"); |
| 4630 | } |
| 4631 | if (consumeIf("sP")) { |
| 4632 | size_t ArgsBegin = Names.size(); |
| 4633 | while (!consumeIf('E')) { |
| 4634 | Node *Arg = getDerived().parseTemplateArg(); |
| 4635 | if (Arg == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4636 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4637 | Names.push_back(Arg); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4638 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4639 | auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin)); |
| 4640 | if (!Pack) |
| 4641 | return nullptr; |
| 4642 | return make<EnclosingExpr>("sizeof... (", Pack, ")"); |
| 4643 | } |
| 4644 | if (consumeIf("tl")) { |
| 4645 | Node *Ty = getDerived().parseType(); |
| 4646 | if (Ty == nullptr) |
| 4647 | return nullptr; |
| 4648 | size_t InitsBegin = Names.size(); |
| 4649 | while (!consumeIf('E')) { |
| 4650 | Node *E = getDerived().parseBracedExpr(); |
| 4651 | if (E == nullptr) |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4652 | return nullptr; |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4653 | Names.push_back(E); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4654 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4655 | return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin)); |
| 4656 | } |
| 4657 | if (consumeIf("tr")) |
| 4658 | return make<NameType>("throw"); |
| 4659 | if (consumeIf("tw")) { |
| 4660 | Node *Ex = getDerived().parseExpr(); |
| 4661 | if (Ex == nullptr) |
| 4662 | return nullptr; |
| 4663 | return make<ThrowExpr>(Ex); |
| 4664 | } |
| 4665 | if (consumeIf('u')) { |
James Y Knight | 4a60efc | 2020-12-07 10:26:49 -0500 | [diff] [blame] | 4666 | Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr); |
| 4667 | if (!Name) |
| 4668 | return nullptr; |
| 4669 | // Special case legacy __uuidof mangling. The 't' and 'z' appear where the |
| 4670 | // standard encoding expects a <template-arg>, and would be otherwise be |
| 4671 | // interpreted as <type> node 'short' or 'ellipsis'. However, neither |
| 4672 | // __uuidof(short) nor __uuidof(...) can actually appear, so there is no |
| 4673 | // actual conflict here. |
Nathan Sidwell | a3b5900 | 2022-02-11 05:54:40 -0800 | [diff] [blame] | 4674 | bool IsUUID = false; |
| 4675 | Node *UUID = nullptr; |
James Y Knight | 4a60efc | 2020-12-07 10:26:49 -0500 | [diff] [blame] | 4676 | if (Name->getBaseName() == "__uuidof") { |
Nathan Sidwell | a3b5900 | 2022-02-11 05:54:40 -0800 | [diff] [blame] | 4677 | if (consumeIf('t')) { |
| 4678 | UUID = getDerived().parseType(); |
| 4679 | IsUUID = true; |
| 4680 | } else if (consumeIf('z')) { |
| 4681 | UUID = getDerived().parseExpr(); |
| 4682 | IsUUID = true; |
James Y Knight | 4a60efc | 2020-12-07 10:26:49 -0500 | [diff] [blame] | 4683 | } |
| 4684 | } |
| 4685 | size_t ExprsBegin = Names.size(); |
Nathan Sidwell | a3b5900 | 2022-02-11 05:54:40 -0800 | [diff] [blame] | 4686 | if (IsUUID) { |
| 4687 | if (UUID == nullptr) |
| 4688 | return nullptr; |
| 4689 | Names.push_back(UUID); |
| 4690 | } else { |
| 4691 | while (!consumeIf('E')) { |
| 4692 | Node *E = getDerived().parseTemplateArg(); |
| 4693 | if (E == nullptr) |
| 4694 | return E; |
| 4695 | Names.push_back(E); |
| 4696 | } |
James Y Knight | 4a60efc | 2020-12-07 10:26:49 -0500 | [diff] [blame] | 4697 | } |
| 4698 | return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin)); |
| 4699 | } |
Nathan Sidwell | 12b2ce7 | 2022-01-27 13:23:16 -0800 | [diff] [blame^] | 4700 | |
| 4701 | // Only unresolved names remain. |
| 4702 | return getDerived().parseUnresolvedName(Global); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | // <call-offset> ::= h <nv-offset> _ |
| 4706 | // ::= v <v-offset> _ |
| 4707 | // |
| 4708 | // <nv-offset> ::= <offset number> |
| 4709 | // # non-virtual base override |
| 4710 | // |
| 4711 | // <v-offset> ::= <offset number> _ <virtual offset number> |
| 4712 | // # virtual base override, with vcall offset |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4713 | template <typename Alloc, typename Derived> |
| 4714 | bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4715 | // Just scan through the call offset, we never add this information into the |
| 4716 | // output. |
| 4717 | if (consumeIf('h')) |
| 4718 | return parseNumber(true).empty() || !consumeIf('_'); |
| 4719 | if (consumeIf('v')) |
| 4720 | return parseNumber(true).empty() || !consumeIf('_') || |
| 4721 | parseNumber(true).empty() || !consumeIf('_'); |
| 4722 | return true; |
| 4723 | } |
| 4724 | |
| 4725 | // <special-name> ::= TV <type> # virtual table |
| 4726 | // ::= TT <type> # VTT structure (construction vtable index) |
| 4727 | // ::= TI <type> # typeinfo structure |
| 4728 | // ::= TS <type> # typeinfo name (null-terminated byte string) |
| 4729 | // ::= Tc <call-offset> <call-offset> <base encoding> |
| 4730 | // # base is the nominal target function of thunk |
| 4731 | // # first call-offset is 'this' adjustment |
| 4732 | // # second call-offset is result adjustment |
| 4733 | // ::= T <call-offset> <base encoding> |
| 4734 | // # base is the nominal target function of thunk |
| 4735 | // ::= GV <object name> # Guard variable for one-time initialization |
| 4736 | // # No <type> |
| 4737 | // ::= TW <object name> # Thread-local wrapper |
| 4738 | // ::= TH <object name> # Thread-local initialization |
| 4739 | // ::= GR <object name> _ # First temporary |
| 4740 | // ::= GR <object name> <seq-id> _ # Subsequent temporaries |
| 4741 | // extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first |
| 4742 | // extension ::= GR <object name> # reference temporary for object |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4743 | template <typename Derived, typename Alloc> |
| 4744 | Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4745 | switch (look()) { |
| 4746 | case 'T': |
| 4747 | switch (look(1)) { |
Richard Smith | 1865d2f | 2020-10-22 19:29:36 -0700 | [diff] [blame] | 4748 | // TA <template-arg> # template parameter object |
| 4749 | // |
| 4750 | // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63 |
| 4751 | case 'A': { |
| 4752 | First += 2; |
| 4753 | Node *Arg = getDerived().parseTemplateArg(); |
| 4754 | if (Arg == nullptr) |
| 4755 | return nullptr; |
| 4756 | return make<SpecialName>("template parameter object for ", Arg); |
| 4757 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4758 | // TV <type> # virtual table |
| 4759 | case 'V': { |
| 4760 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4761 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4762 | if (Ty == nullptr) |
| 4763 | return nullptr; |
| 4764 | return make<SpecialName>("vtable for ", Ty); |
| 4765 | } |
| 4766 | // TT <type> # VTT structure (construction vtable index) |
| 4767 | case 'T': { |
| 4768 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4769 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4770 | if (Ty == nullptr) |
| 4771 | return nullptr; |
| 4772 | return make<SpecialName>("VTT for ", Ty); |
| 4773 | } |
| 4774 | // TI <type> # typeinfo structure |
| 4775 | case 'I': { |
| 4776 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4777 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4778 | if (Ty == nullptr) |
| 4779 | return nullptr; |
| 4780 | return make<SpecialName>("typeinfo for ", Ty); |
| 4781 | } |
| 4782 | // TS <type> # typeinfo name (null-terminated byte string) |
| 4783 | case 'S': { |
| 4784 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4785 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4786 | if (Ty == nullptr) |
| 4787 | return nullptr; |
| 4788 | return make<SpecialName>("typeinfo name for ", Ty); |
| 4789 | } |
| 4790 | // Tc <call-offset> <call-offset> <base encoding> |
| 4791 | case 'c': { |
| 4792 | First += 2; |
| 4793 | if (parseCallOffset() || parseCallOffset()) |
| 4794 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4795 | Node *Encoding = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4796 | if (Encoding == nullptr) |
| 4797 | return nullptr; |
| 4798 | return make<SpecialName>("covariant return thunk to ", Encoding); |
| 4799 | } |
| 4800 | // extension ::= TC <first type> <number> _ <second type> |
| 4801 | // # construction vtable for second-in-first |
| 4802 | case 'C': { |
| 4803 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4804 | Node *FirstType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4805 | if (FirstType == nullptr) |
| 4806 | return nullptr; |
| 4807 | if (parseNumber(true).empty() || !consumeIf('_')) |
| 4808 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4809 | Node *SecondType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4810 | if (SecondType == nullptr) |
| 4811 | return nullptr; |
| 4812 | return make<CtorVtableSpecialName>(SecondType, FirstType); |
| 4813 | } |
| 4814 | // TW <object name> # Thread-local wrapper |
| 4815 | case 'W': { |
| 4816 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4817 | Node *Name = getDerived().parseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4818 | if (Name == nullptr) |
| 4819 | return nullptr; |
| 4820 | return make<SpecialName>("thread-local wrapper routine for ", Name); |
| 4821 | } |
| 4822 | // TH <object name> # Thread-local initialization |
| 4823 | case 'H': { |
| 4824 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4825 | Node *Name = getDerived().parseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4826 | if (Name == nullptr) |
| 4827 | return nullptr; |
| 4828 | return make<SpecialName>("thread-local initialization routine for ", Name); |
| 4829 | } |
| 4830 | // T <call-offset> <base encoding> |
| 4831 | default: { |
| 4832 | ++First; |
| 4833 | bool IsVirt = look() == 'v'; |
| 4834 | if (parseCallOffset()) |
| 4835 | return nullptr; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4836 | Node *BaseEncoding = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4837 | if (BaseEncoding == nullptr) |
| 4838 | return nullptr; |
| 4839 | if (IsVirt) |
| 4840 | return make<SpecialName>("virtual thunk to ", BaseEncoding); |
| 4841 | else |
| 4842 | return make<SpecialName>("non-virtual thunk to ", BaseEncoding); |
| 4843 | } |
| 4844 | } |
| 4845 | case 'G': |
| 4846 | switch (look(1)) { |
| 4847 | // GV <object name> # Guard variable for one-time initialization |
| 4848 | case 'V': { |
| 4849 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4850 | Node *Name = getDerived().parseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4851 | if (Name == nullptr) |
| 4852 | return nullptr; |
| 4853 | return make<SpecialName>("guard variable for ", Name); |
| 4854 | } |
| 4855 | // GR <object name> # reference temporary for object |
| 4856 | // GR <object name> _ # First temporary |
| 4857 | // GR <object name> <seq-id> _ # Subsequent temporaries |
| 4858 | case 'R': { |
| 4859 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4860 | Node *Name = getDerived().parseName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4861 | if (Name == nullptr) |
| 4862 | return nullptr; |
| 4863 | size_t Count; |
| 4864 | bool ParsedSeqId = !parseSeqId(&Count); |
| 4865 | if (!consumeIf('_') && ParsedSeqId) |
| 4866 | return nullptr; |
| 4867 | return make<SpecialName>("reference temporary for ", Name); |
| 4868 | } |
| 4869 | } |
| 4870 | } |
| 4871 | return nullptr; |
| 4872 | } |
| 4873 | |
| 4874 | // <encoding> ::= <function name> <bare-function-type> |
| 4875 | // ::= <data name> |
| 4876 | // ::= <special-name> |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4877 | template <typename Derived, typename Alloc> |
| 4878 | Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() { |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 4879 | // The template parameters of an encoding are unrelated to those of the |
| 4880 | // enclosing context. |
| 4881 | class SaveTemplateParams { |
| 4882 | AbstractManglingParser *Parser; |
| 4883 | decltype(TemplateParams) OldParams; |
Justin Lebar | 2c53623 | 2021-06-09 16:57:22 -0700 | [diff] [blame] | 4884 | decltype(OuterTemplateParams) OldOuterParams; |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 4885 | |
| 4886 | public: |
Louis Dionne | c1fe867 | 2020-10-30 17:33:02 -0400 | [diff] [blame] | 4887 | SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) { |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 4888 | OldParams = std::move(Parser->TemplateParams); |
Justin Lebar | 2c53623 | 2021-06-09 16:57:22 -0700 | [diff] [blame] | 4889 | OldOuterParams = std::move(Parser->OuterTemplateParams); |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 4890 | Parser->TemplateParams.clear(); |
Justin Lebar | 2c53623 | 2021-06-09 16:57:22 -0700 | [diff] [blame] | 4891 | Parser->OuterTemplateParams.clear(); |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 4892 | } |
| 4893 | ~SaveTemplateParams() { |
| 4894 | Parser->TemplateParams = std::move(OldParams); |
Justin Lebar | 2c53623 | 2021-06-09 16:57:22 -0700 | [diff] [blame] | 4895 | Parser->OuterTemplateParams = std::move(OldOuterParams); |
Richard Smith | fac3971 | 2020-07-09 21:08:39 -0700 | [diff] [blame] | 4896 | } |
| 4897 | } SaveTemplateParams(this); |
Richard Smith | fd43432 | 2020-07-09 20:36:04 -0700 | [diff] [blame] | 4898 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4899 | if (look() == 'G' || look() == 'T') |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4900 | return getDerived().parseSpecialName(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4901 | |
| 4902 | auto IsEndOfEncoding = [&] { |
| 4903 | // The set of chars that can potentially follow an <encoding> (none of which |
| 4904 | // can start a <type>). Enumerating these allows us to avoid speculative |
| 4905 | // parsing. |
| 4906 | return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_'; |
| 4907 | }; |
| 4908 | |
| 4909 | NameState NameInfo(this); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4910 | Node *Name = getDerived().parseName(&NameInfo); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4911 | if (Name == nullptr) |
| 4912 | return nullptr; |
| 4913 | |
| 4914 | if (resolveForwardTemplateRefs(NameInfo)) |
| 4915 | return nullptr; |
| 4916 | |
| 4917 | if (IsEndOfEncoding()) |
| 4918 | return Name; |
| 4919 | |
| 4920 | Node *Attrs = nullptr; |
| 4921 | if (consumeIf("Ua9enable_ifI")) { |
| 4922 | size_t BeforeArgs = Names.size(); |
| 4923 | while (!consumeIf('E')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4924 | Node *Arg = getDerived().parseTemplateArg(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4925 | if (Arg == nullptr) |
| 4926 | return nullptr; |
| 4927 | Names.push_back(Arg); |
| 4928 | } |
| 4929 | Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs)); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 4930 | if (!Attrs) |
| 4931 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | Node *ReturnType = nullptr; |
| 4935 | if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4936 | ReturnType = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4937 | if (ReturnType == nullptr) |
| 4938 | return nullptr; |
| 4939 | } |
| 4940 | |
| 4941 | if (consumeIf('v')) |
| 4942 | return make<FunctionEncoding>(ReturnType, Name, NodeArray(), |
| 4943 | Attrs, NameInfo.CVQualifiers, |
| 4944 | NameInfo.ReferenceQualifier); |
| 4945 | |
| 4946 | size_t ParamsBegin = Names.size(); |
| 4947 | do { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4948 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4949 | if (Ty == nullptr) |
| 4950 | return nullptr; |
| 4951 | Names.push_back(Ty); |
| 4952 | } while (!IsEndOfEncoding()); |
| 4953 | |
| 4954 | return make<FunctionEncoding>(ReturnType, Name, |
| 4955 | popTrailingNodeArray(ParamsBegin), |
| 4956 | Attrs, NameInfo.CVQualifiers, |
| 4957 | NameInfo.ReferenceQualifier); |
| 4958 | } |
| 4959 | |
| 4960 | template <class Float> |
| 4961 | struct FloatData; |
| 4962 | |
| 4963 | template <> |
| 4964 | struct FloatData<float> |
| 4965 | { |
| 4966 | static const size_t mangled_size = 8; |
| 4967 | static const size_t max_demangled_size = 24; |
| 4968 | static constexpr const char* spec = "%af"; |
| 4969 | }; |
| 4970 | |
| 4971 | template <> |
| 4972 | struct FloatData<double> |
| 4973 | { |
| 4974 | static const size_t mangled_size = 16; |
| 4975 | static const size_t max_demangled_size = 32; |
| 4976 | static constexpr const char* spec = "%a"; |
| 4977 | }; |
| 4978 | |
| 4979 | template <> |
| 4980 | struct FloatData<long double> |
| 4981 | { |
| 4982 | #if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \ |
| 4983 | defined(__wasm__) |
| 4984 | static const size_t mangled_size = 32; |
| 4985 | #elif defined(__arm__) || defined(__mips__) || defined(__hexagon__) |
| 4986 | static const size_t mangled_size = 16; |
| 4987 | #else |
| 4988 | static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms |
| 4989 | #endif |
Elliott Hughes | 5a360ea | 2020-04-10 17:42:00 -0700 | [diff] [blame] | 4990 | // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes. |
| 4991 | // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits. |
| 4992 | // Negatives are one character longer than positives. |
| 4993 | // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the |
| 4994 | // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128. |
| 4995 | static const size_t max_demangled_size = 42; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 4996 | static constexpr const char *spec = "%LaL"; |
| 4997 | }; |
| 4998 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 4999 | template <typename Alloc, typename Derived> |
| 5000 | template <class Float> |
| 5001 | Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5002 | const size_t N = FloatData<Float>::mangled_size; |
| 5003 | if (numLeft() <= N) |
| 5004 | return nullptr; |
| 5005 | StringView Data(First, First + N); |
| 5006 | for (char C : Data) |
| 5007 | if (!std::isxdigit(C)) |
| 5008 | return nullptr; |
| 5009 | First += N; |
| 5010 | if (!consumeIf('E')) |
| 5011 | return nullptr; |
| 5012 | return make<FloatLiteralImpl<Float>>(Data); |
| 5013 | } |
| 5014 | |
| 5015 | // <seq-id> ::= <0-9A-Z>+ |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5016 | template <typename Alloc, typename Derived> |
| 5017 | bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5018 | if (!(look() >= '0' && look() <= '9') && |
| 5019 | !(look() >= 'A' && look() <= 'Z')) |
| 5020 | return true; |
| 5021 | |
| 5022 | size_t Id = 0; |
| 5023 | while (true) { |
| 5024 | if (look() >= '0' && look() <= '9') { |
| 5025 | Id *= 36; |
| 5026 | Id += static_cast<size_t>(look() - '0'); |
| 5027 | } else if (look() >= 'A' && look() <= 'Z') { |
| 5028 | Id *= 36; |
| 5029 | Id += static_cast<size_t>(look() - 'A') + 10; |
| 5030 | } else { |
| 5031 | *Out = Id; |
| 5032 | return false; |
| 5033 | } |
| 5034 | ++First; |
| 5035 | } |
| 5036 | } |
| 5037 | |
| 5038 | // <substitution> ::= S <seq-id> _ |
| 5039 | // ::= S_ |
| 5040 | // <substitution> ::= Sa # ::std::allocator |
| 5041 | // <substitution> ::= Sb # ::std::basic_string |
| 5042 | // <substitution> ::= Ss # ::std::basic_string < char, |
| 5043 | // ::std::char_traits<char>, |
| 5044 | // ::std::allocator<char> > |
| 5045 | // <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> > |
| 5046 | // <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> > |
| 5047 | // <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> > |
Nathan Sidwell | e12f7bd | 2022-01-21 11:00:56 -0800 | [diff] [blame] | 5048 | // The St case is handled specially in parseNestedName. |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5049 | template <typename Derived, typename Alloc> |
| 5050 | Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5051 | if (!consumeIf('S')) |
| 5052 | return nullptr; |
| 5053 | |
Nathan Sidwell | fd0ef6d | 2022-01-20 07:40:12 -0800 | [diff] [blame] | 5054 | if (look() >= 'a' && look() <= 'z') { |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5055 | SpecialSubKind Kind; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5056 | switch (look()) { |
| 5057 | case 'a': |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5058 | Kind = SpecialSubKind::allocator; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5059 | break; |
| 5060 | case 'b': |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5061 | Kind = SpecialSubKind::basic_string; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5062 | break; |
| 5063 | case 'd': |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5064 | Kind = SpecialSubKind::iostream; |
| 5065 | break; |
| 5066 | case 'i': |
| 5067 | Kind = SpecialSubKind::istream; |
| 5068 | break; |
| 5069 | case 'o': |
| 5070 | Kind = SpecialSubKind::ostream; |
| 5071 | break; |
| 5072 | case 's': |
| 5073 | Kind = SpecialSubKind::string; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5074 | break; |
| 5075 | default: |
| 5076 | return nullptr; |
| 5077 | } |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5078 | ++First; |
| 5079 | auto *SpecialSub = make<SpecialSubstitution>(Kind); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 5080 | if (!SpecialSub) |
| 5081 | return nullptr; |
Nathan Sidwell | df43e1b | 2022-01-24 07:59:57 -0800 | [diff] [blame] | 5082 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5083 | // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution> |
| 5084 | // has ABI tags, the tags are appended to the substitution; the result is a |
| 5085 | // substitutable component. |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5086 | Node *WithTags = getDerived().parseAbiTags(SpecialSub); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5087 | if (WithTags != SpecialSub) { |
| 5088 | Subs.push_back(WithTags); |
| 5089 | SpecialSub = WithTags; |
| 5090 | } |
| 5091 | return SpecialSub; |
| 5092 | } |
| 5093 | |
| 5094 | // ::= S_ |
| 5095 | if (consumeIf('_')) { |
| 5096 | if (Subs.empty()) |
| 5097 | return nullptr; |
| 5098 | return Subs[0]; |
| 5099 | } |
| 5100 | |
| 5101 | // ::= S <seq-id> _ |
| 5102 | size_t Index = 0; |
| 5103 | if (parseSeqId(&Index)) |
| 5104 | return nullptr; |
| 5105 | ++Index; |
| 5106 | if (!consumeIf('_') || Index >= Subs.size()) |
| 5107 | return nullptr; |
| 5108 | return Subs[Index]; |
| 5109 | } |
| 5110 | |
| 5111 | // <template-param> ::= T_ # first template parameter |
| 5112 | // ::= T <parameter-2 non-negative number> _ |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5113 | // ::= TL <level-1> __ |
| 5114 | // ::= TL <level-1> _ <parameter-2 non-negative number> _ |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5115 | template <typename Derived, typename Alloc> |
| 5116 | Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5117 | if (!consumeIf('T')) |
| 5118 | return nullptr; |
| 5119 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5120 | size_t Level = 0; |
| 5121 | if (consumeIf('L')) { |
| 5122 | if (parsePositiveInteger(&Level)) |
| 5123 | return nullptr; |
| 5124 | ++Level; |
| 5125 | if (!consumeIf('_')) |
| 5126 | return nullptr; |
| 5127 | } |
| 5128 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5129 | size_t Index = 0; |
| 5130 | if (!consumeIf('_')) { |
| 5131 | if (parsePositiveInteger(&Index)) |
| 5132 | return nullptr; |
| 5133 | ++Index; |
| 5134 | if (!consumeIf('_')) |
| 5135 | return nullptr; |
| 5136 | } |
| 5137 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5138 | // If we're in a context where this <template-param> refers to a |
| 5139 | // <template-arg> further ahead in the mangled name (currently just conversion |
| 5140 | // 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] | 5141 | // This can only happen at the outermost level. |
| 5142 | if (PermitForwardTemplateReferences && Level == 0) { |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 5143 | Node *ForwardRef = make<ForwardTemplateReference>(Index); |
| 5144 | if (!ForwardRef) |
| 5145 | return nullptr; |
| 5146 | assert(ForwardRef->getKind() == Node::KForwardTemplateReference); |
| 5147 | ForwardTemplateRefs.push_back( |
| 5148 | static_cast<ForwardTemplateReference *>(ForwardRef)); |
| 5149 | return ForwardRef; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5152 | if (Level >= TemplateParams.size() || !TemplateParams[Level] || |
| 5153 | Index >= TemplateParams[Level]->size()) { |
| 5154 | // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter |
| 5155 | // list are mangled as the corresponding artificial template type parameter. |
| 5156 | if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) { |
| 5157 | // This will be popped by the ScopedTemplateParamList in |
| 5158 | // parseUnnamedTypeName. |
| 5159 | if (Level == TemplateParams.size()) |
| 5160 | TemplateParams.push_back(nullptr); |
| 5161 | return make<NameType>("auto"); |
| 5162 | } |
| 5163 | |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5164 | return nullptr; |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
| 5167 | return (*TemplateParams[Level])[Index]; |
| 5168 | } |
| 5169 | |
| 5170 | // <template-param-decl> ::= Ty # type parameter |
| 5171 | // ::= Tn <type> # non-type parameter |
| 5172 | // ::= Tt <template-param-decl>* E # template parameter |
| 5173 | // ::= Tp <template-param-decl> # parameter pack |
| 5174 | template <typename Derived, typename Alloc> |
| 5175 | Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() { |
| 5176 | auto InventTemplateParamName = [&](TemplateParamKind Kind) { |
| 5177 | unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++; |
| 5178 | Node *N = make<SyntheticTemplateParamName>(Kind, Index); |
| 5179 | if (N) TemplateParams.back()->push_back(N); |
| 5180 | return N; |
| 5181 | }; |
| 5182 | |
| 5183 | if (consumeIf("Ty")) { |
| 5184 | Node *Name = InventTemplateParamName(TemplateParamKind::Type); |
| 5185 | if (!Name) |
| 5186 | return nullptr; |
| 5187 | return make<TypeTemplateParamDecl>(Name); |
| 5188 | } |
| 5189 | |
| 5190 | if (consumeIf("Tn")) { |
| 5191 | Node *Name = InventTemplateParamName(TemplateParamKind::NonType); |
| 5192 | if (!Name) |
| 5193 | return nullptr; |
| 5194 | Node *Type = parseType(); |
| 5195 | if (!Type) |
| 5196 | return nullptr; |
| 5197 | return make<NonTypeTemplateParamDecl>(Name, Type); |
| 5198 | } |
| 5199 | |
| 5200 | if (consumeIf("Tt")) { |
| 5201 | Node *Name = InventTemplateParamName(TemplateParamKind::Template); |
| 5202 | if (!Name) |
| 5203 | return nullptr; |
| 5204 | size_t ParamsBegin = Names.size(); |
| 5205 | ScopedTemplateParamList TemplateTemplateParamParams(this); |
| 5206 | while (!consumeIf("E")) { |
| 5207 | Node *P = parseTemplateParamDecl(); |
| 5208 | if (!P) |
| 5209 | return nullptr; |
| 5210 | Names.push_back(P); |
| 5211 | } |
| 5212 | NodeArray Params = popTrailingNodeArray(ParamsBegin); |
| 5213 | return make<TemplateTemplateParamDecl>(Name, Params); |
| 5214 | } |
| 5215 | |
| 5216 | if (consumeIf("Tp")) { |
| 5217 | Node *P = parseTemplateParamDecl(); |
| 5218 | if (!P) |
| 5219 | return nullptr; |
| 5220 | return make<TemplateParamPackDecl>(P); |
| 5221 | } |
| 5222 | |
| 5223 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | // <template-arg> ::= <type> # type or template |
| 5227 | // ::= X <expression> E # expression |
| 5228 | // ::= <expr-primary> # simple expressions |
| 5229 | // ::= J <template-arg>* E # argument pack |
| 5230 | // ::= LZ <encoding> E # extension |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5231 | template <typename Derived, typename Alloc> |
| 5232 | Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5233 | switch (look()) { |
| 5234 | case 'X': { |
| 5235 | ++First; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5236 | Node *Arg = getDerived().parseExpr(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5237 | if (Arg == nullptr || !consumeIf('E')) |
| 5238 | return nullptr; |
| 5239 | return Arg; |
| 5240 | } |
| 5241 | case 'J': { |
| 5242 | ++First; |
| 5243 | size_t ArgsBegin = Names.size(); |
| 5244 | while (!consumeIf('E')) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5245 | Node *Arg = getDerived().parseTemplateArg(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5246 | if (Arg == nullptr) |
| 5247 | return nullptr; |
| 5248 | Names.push_back(Arg); |
| 5249 | } |
| 5250 | NodeArray Args = popTrailingNodeArray(ArgsBegin); |
| 5251 | return make<TemplateArgumentPack>(Args); |
| 5252 | } |
| 5253 | case 'L': { |
| 5254 | // ::= LZ <encoding> E # extension |
| 5255 | if (look(1) == 'Z') { |
| 5256 | First += 2; |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5257 | Node *Arg = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5258 | if (Arg == nullptr || !consumeIf('E')) |
| 5259 | return nullptr; |
| 5260 | return Arg; |
| 5261 | } |
| 5262 | // ::= <expr-primary> # simple expressions |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5263 | return getDerived().parseExprPrimary(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5264 | } |
| 5265 | default: |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5266 | return getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5267 | } |
| 5268 | } |
| 5269 | |
| 5270 | // <template-args> ::= I <template-arg>* E |
| 5271 | // extension, the abi says <template-arg>+ |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5272 | template <typename Derived, typename Alloc> |
| 5273 | Node * |
| 5274 | AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5275 | if (!consumeIf('I')) |
| 5276 | return nullptr; |
| 5277 | |
| 5278 | // <template-params> refer to the innermost <template-args>. Clear out any |
| 5279 | // outer args that we may have inserted into TemplateParams. |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5280 | if (TagTemplates) { |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5281 | TemplateParams.clear(); |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5282 | TemplateParams.push_back(&OuterTemplateParams); |
| 5283 | OuterTemplateParams.clear(); |
| 5284 | } |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5285 | |
| 5286 | size_t ArgsBegin = Names.size(); |
| 5287 | while (!consumeIf('E')) { |
| 5288 | if (TagTemplates) { |
| 5289 | auto OldParams = std::move(TemplateParams); |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5290 | Node *Arg = getDerived().parseTemplateArg(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5291 | TemplateParams = std::move(OldParams); |
| 5292 | if (Arg == nullptr) |
| 5293 | return nullptr; |
| 5294 | Names.push_back(Arg); |
| 5295 | Node *TableEntry = Arg; |
| 5296 | if (Arg->getKind() == Node::KTemplateArgumentPack) { |
| 5297 | TableEntry = make<ParameterPack>( |
| 5298 | static_cast<TemplateArgumentPack*>(TableEntry)->getElements()); |
Richard Smith | b485b35 | 2018-08-24 23:30:26 +0000 | [diff] [blame] | 5299 | if (!TableEntry) |
| 5300 | return nullptr; |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5301 | } |
Richard Smith | df1c14c | 2019-09-06 23:53:21 +0000 | [diff] [blame] | 5302 | TemplateParams.back()->push_back(TableEntry); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5303 | } else { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5304 | Node *Arg = getDerived().parseTemplateArg(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5305 | if (Arg == nullptr) |
| 5306 | return nullptr; |
| 5307 | Names.push_back(Arg); |
| 5308 | } |
| 5309 | } |
| 5310 | return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin)); |
| 5311 | } |
| 5312 | |
| 5313 | // <mangled-name> ::= _Z <encoding> |
| 5314 | // ::= <type> |
| 5315 | // extension ::= ___Z <encoding> _block_invoke |
| 5316 | // extension ::= ___Z <encoding> _block_invoke<decimal-digit>+ |
| 5317 | // extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+ |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5318 | template <typename Derived, typename Alloc> |
| 5319 | Node *AbstractManglingParser<Derived, Alloc>::parse() { |
Erik Pilkington | c0df158 | 2019-01-17 21:37:36 +0000 | [diff] [blame] | 5320 | if (consumeIf("_Z") || consumeIf("__Z")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5321 | Node *Encoding = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5322 | if (Encoding == nullptr) |
| 5323 | return nullptr; |
| 5324 | if (look() == '.') { |
| 5325 | Encoding = make<DotSuffix>(Encoding, StringView(First, Last)); |
| 5326 | First = Last; |
| 5327 | } |
| 5328 | if (numLeft() != 0) |
| 5329 | return nullptr; |
| 5330 | return Encoding; |
| 5331 | } |
| 5332 | |
Erik Pilkington | c0df158 | 2019-01-17 21:37:36 +0000 | [diff] [blame] | 5333 | if (consumeIf("___Z") || consumeIf("____Z")) { |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5334 | Node *Encoding = getDerived().parseEncoding(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5335 | if (Encoding == nullptr || !consumeIf("_block_invoke")) |
| 5336 | return nullptr; |
| 5337 | bool RequireNumber = consumeIf('_'); |
| 5338 | if (parseNumber().empty() && RequireNumber) |
| 5339 | return nullptr; |
| 5340 | if (look() == '.') |
| 5341 | First = Last; |
| 5342 | if (numLeft() != 0) |
| 5343 | return nullptr; |
| 5344 | return make<SpecialName>("invocation function for block in ", Encoding); |
| 5345 | } |
| 5346 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5347 | Node *Ty = getDerived().parseType(); |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5348 | if (numLeft() != 0) |
| 5349 | return nullptr; |
| 5350 | return Ty; |
| 5351 | } |
| 5352 | |
Pavel Labath | ba82519 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 5353 | template <typename Alloc> |
| 5354 | struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> { |
| 5355 | using AbstractManglingParser<ManglingParser<Alloc>, |
| 5356 | Alloc>::AbstractManglingParser; |
| 5357 | }; |
| 5358 | |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 5359 | DEMANGLE_NAMESPACE_END |
Richard Smith | c20d144 | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 5360 | |
Erik Pilkington | f70e4d8 | 2019-01-17 20:37:51 +0000 | [diff] [blame] | 5361 | #endif // DEMANGLE_ITANIUMDEMANGLE_H |