MicrosoftDemangle.cpp 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377
  1. //===- MicrosoftDemangle.cpp ----------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines a demangler for MSVC-style mangled symbols.
  10. //
  11. // This file has no dependencies on the rest of LLVM so that it can be
  12. // easily reused in other programs such as libcxxabi.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Demangle/MicrosoftDemangle.h"
  16. #include "llvm/Demangle/Demangle.h"
  17. #include "llvm/Demangle/MicrosoftDemangleNodes.h"
  18. #include "llvm/Demangle/DemangleConfig.h"
  19. #include "llvm/Demangle/StringView.h"
  20. #include "llvm/Demangle/Utility.h"
  21. #include <array>
  22. #include <cctype>
  23. #include <cstdio>
  24. #include <tuple>
  25. using namespace llvm;
  26. using namespace ms_demangle;
  27. static bool startsWithDigit(StringView S) {
  28. return !S.empty() && std::isdigit(S.front());
  29. }
  30. struct NodeList {
  31. Node *N = nullptr;
  32. NodeList *Next = nullptr;
  33. };
  34. static bool isMemberPointer(StringView MangledName, bool &Error) {
  35. Error = false;
  36. switch (MangledName.popFront()) {
  37. case '$':
  38. // This is probably an rvalue reference (e.g. $$Q), and you cannot have an
  39. // rvalue reference to a member.
  40. return false;
  41. case 'A':
  42. // 'A' indicates a reference, and you cannot have a reference to a member
  43. // function or member.
  44. return false;
  45. case 'P':
  46. case 'Q':
  47. case 'R':
  48. case 'S':
  49. // These 4 values indicate some kind of pointer, but we still don't know
  50. // what.
  51. break;
  52. default:
  53. // isMemberPointer() is called only if isPointerType() returns true,
  54. // and it rejects other prefixes.
  55. DEMANGLE_UNREACHABLE;
  56. }
  57. // If it starts with a number, then 6 indicates a non-member function
  58. // pointer, and 8 indicates a member function pointer.
  59. if (startsWithDigit(MangledName)) {
  60. if (MangledName[0] != '6' && MangledName[0] != '8') {
  61. Error = true;
  62. return false;
  63. }
  64. return (MangledName[0] == '8');
  65. }
  66. // Remove ext qualifiers since those can appear on either type and are
  67. // therefore not indicative.
  68. MangledName.consumeFront('E'); // 64-bit
  69. MangledName.consumeFront('I'); // restrict
  70. MangledName.consumeFront('F'); // unaligned
  71. if (MangledName.empty()) {
  72. Error = true;
  73. return false;
  74. }
  75. // The next value should be either ABCD (non-member) or QRST (member).
  76. switch (MangledName.front()) {
  77. case 'A':
  78. case 'B':
  79. case 'C':
  80. case 'D':
  81. return false;
  82. case 'Q':
  83. case 'R':
  84. case 'S':
  85. case 'T':
  86. return true;
  87. default:
  88. Error = true;
  89. return false;
  90. }
  91. }
  92. static SpecialIntrinsicKind
  93. consumeSpecialIntrinsicKind(StringView &MangledName) {
  94. if (MangledName.consumeFront("?_7"))
  95. return SpecialIntrinsicKind::Vftable;
  96. if (MangledName.consumeFront("?_8"))
  97. return SpecialIntrinsicKind::Vbtable;
  98. if (MangledName.consumeFront("?_9"))
  99. return SpecialIntrinsicKind::VcallThunk;
  100. if (MangledName.consumeFront("?_A"))
  101. return SpecialIntrinsicKind::Typeof;
  102. if (MangledName.consumeFront("?_B"))
  103. return SpecialIntrinsicKind::LocalStaticGuard;
  104. if (MangledName.consumeFront("?_C"))
  105. return SpecialIntrinsicKind::StringLiteralSymbol;
  106. if (MangledName.consumeFront("?_P"))
  107. return SpecialIntrinsicKind::UdtReturning;
  108. if (MangledName.consumeFront("?_R0"))
  109. return SpecialIntrinsicKind::RttiTypeDescriptor;
  110. if (MangledName.consumeFront("?_R1"))
  111. return SpecialIntrinsicKind::RttiBaseClassDescriptor;
  112. if (MangledName.consumeFront("?_R2"))
  113. return SpecialIntrinsicKind::RttiBaseClassArray;
  114. if (MangledName.consumeFront("?_R3"))
  115. return SpecialIntrinsicKind::RttiClassHierarchyDescriptor;
  116. if (MangledName.consumeFront("?_R4"))
  117. return SpecialIntrinsicKind::RttiCompleteObjLocator;
  118. if (MangledName.consumeFront("?_S"))
  119. return SpecialIntrinsicKind::LocalVftable;
  120. if (MangledName.consumeFront("?__E"))
  121. return SpecialIntrinsicKind::DynamicInitializer;
  122. if (MangledName.consumeFront("?__F"))
  123. return SpecialIntrinsicKind::DynamicAtexitDestructor;
  124. if (MangledName.consumeFront("?__J"))
  125. return SpecialIntrinsicKind::LocalStaticThreadGuard;
  126. return SpecialIntrinsicKind::None;
  127. }
  128. static bool startsWithLocalScopePattern(StringView S) {
  129. if (!S.consumeFront('?'))
  130. return false;
  131. size_t End = S.find('?');
  132. if (End == StringView::npos)
  133. return false;
  134. StringView Candidate = S.substr(0, End);
  135. if (Candidate.empty())
  136. return false;
  137. // \?[0-9]\?
  138. // ?@? is the discriminator 0.
  139. if (Candidate.size() == 1)
  140. return Candidate[0] == '@' || (Candidate[0] >= '0' && Candidate[0] <= '9');
  141. // If it's not 0-9, then it's an encoded number terminated with an @
  142. if (Candidate.back() != '@')
  143. return false;
  144. Candidate = Candidate.dropBack();
  145. // An encoded number starts with B-P and all subsequent digits are in A-P.
  146. // Note that the reason the first digit cannot be A is two fold. First, it
  147. // would create an ambiguity with ?A which delimits the beginning of an
  148. // anonymous namespace. Second, A represents 0, and you don't start a multi
  149. // digit number with a leading 0. Presumably the anonymous namespace
  150. // ambiguity is also why single digit encoded numbers use 0-9 rather than A-J.
  151. if (Candidate[0] < 'B' || Candidate[0] > 'P')
  152. return false;
  153. Candidate = Candidate.dropFront();
  154. while (!Candidate.empty()) {
  155. if (Candidate[0] < 'A' || Candidate[0] > 'P')
  156. return false;
  157. Candidate = Candidate.dropFront();
  158. }
  159. return true;
  160. }
  161. static bool isTagType(StringView S) {
  162. switch (S.front()) {
  163. case 'T': // union
  164. case 'U': // struct
  165. case 'V': // class
  166. case 'W': // enum
  167. return true;
  168. }
  169. return false;
  170. }
  171. static bool isCustomType(StringView S) { return S[0] == '?'; }
  172. static bool isPointerType(StringView S) {
  173. if (S.startsWith("$$Q")) // foo &&
  174. return true;
  175. switch (S.front()) {
  176. case 'A': // foo &
  177. case 'P': // foo *
  178. case 'Q': // foo *const
  179. case 'R': // foo *volatile
  180. case 'S': // foo *const volatile
  181. return true;
  182. }
  183. return false;
  184. }
  185. static bool isArrayType(StringView S) { return S[0] == 'Y'; }
  186. static bool isFunctionType(StringView S) {
  187. return S.startsWith("$$A8@@") || S.startsWith("$$A6");
  188. }
  189. static FunctionRefQualifier
  190. demangleFunctionRefQualifier(StringView &MangledName) {
  191. if (MangledName.consumeFront('G'))
  192. return FunctionRefQualifier::Reference;
  193. else if (MangledName.consumeFront('H'))
  194. return FunctionRefQualifier::RValueReference;
  195. return FunctionRefQualifier::None;
  196. }
  197. static std::pair<Qualifiers, PointerAffinity>
  198. demanglePointerCVQualifiers(StringView &MangledName) {
  199. if (MangledName.consumeFront("$$Q"))
  200. return std::make_pair(Q_None, PointerAffinity::RValueReference);
  201. switch (MangledName.popFront()) {
  202. case 'A':
  203. return std::make_pair(Q_None, PointerAffinity::Reference);
  204. case 'P':
  205. return std::make_pair(Q_None, PointerAffinity::Pointer);
  206. case 'Q':
  207. return std::make_pair(Q_Const, PointerAffinity::Pointer);
  208. case 'R':
  209. return std::make_pair(Q_Volatile, PointerAffinity::Pointer);
  210. case 'S':
  211. return std::make_pair(Qualifiers(Q_Const | Q_Volatile),
  212. PointerAffinity::Pointer);
  213. }
  214. // This function is only called if isPointerType() returns true,
  215. // and it only returns true for the six cases listed above.
  216. DEMANGLE_UNREACHABLE;
  217. }
  218. StringView Demangler::copyString(StringView Borrowed) {
  219. char *Stable = Arena.allocUnalignedBuffer(Borrowed.size() + 1);
  220. std::strcpy(Stable, Borrowed.begin());
  221. return {Stable, Borrowed.size()};
  222. }
  223. SpecialTableSymbolNode *
  224. Demangler::demangleSpecialTableSymbolNode(StringView &MangledName,
  225. SpecialIntrinsicKind K) {
  226. NamedIdentifierNode *NI = Arena.alloc<NamedIdentifierNode>();
  227. switch (K) {
  228. case SpecialIntrinsicKind::Vftable:
  229. NI->Name = "`vftable'";
  230. break;
  231. case SpecialIntrinsicKind::Vbtable:
  232. NI->Name = "`vbtable'";
  233. break;
  234. case SpecialIntrinsicKind::LocalVftable:
  235. NI->Name = "`local vftable'";
  236. break;
  237. case SpecialIntrinsicKind::RttiCompleteObjLocator:
  238. NI->Name = "`RTTI Complete Object Locator'";
  239. break;
  240. default:
  241. DEMANGLE_UNREACHABLE;
  242. }
  243. QualifiedNameNode *QN = demangleNameScopeChain(MangledName, NI);
  244. SpecialTableSymbolNode *STSN = Arena.alloc<SpecialTableSymbolNode>();
  245. STSN->Name = QN;
  246. bool IsMember = false;
  247. if (MangledName.empty()) {
  248. Error = true;
  249. return nullptr;
  250. }
  251. char Front = MangledName.popFront();
  252. if (Front != '6' && Front != '7') {
  253. Error = true;
  254. return nullptr;
  255. }
  256. std::tie(STSN->Quals, IsMember) = demangleQualifiers(MangledName);
  257. if (!MangledName.consumeFront('@'))
  258. STSN->TargetName = demangleFullyQualifiedTypeName(MangledName);
  259. return STSN;
  260. }
  261. LocalStaticGuardVariableNode *
  262. Demangler::demangleLocalStaticGuard(StringView &MangledName, bool IsThread) {
  263. LocalStaticGuardIdentifierNode *LSGI =
  264. Arena.alloc<LocalStaticGuardIdentifierNode>();
  265. LSGI->IsThread = IsThread;
  266. QualifiedNameNode *QN = demangleNameScopeChain(MangledName, LSGI);
  267. LocalStaticGuardVariableNode *LSGVN =
  268. Arena.alloc<LocalStaticGuardVariableNode>();
  269. LSGVN->Name = QN;
  270. if (MangledName.consumeFront("4IA"))
  271. LSGVN->IsVisible = false;
  272. else if (MangledName.consumeFront("5"))
  273. LSGVN->IsVisible = true;
  274. else {
  275. Error = true;
  276. return nullptr;
  277. }
  278. if (!MangledName.empty())
  279. LSGI->ScopeIndex = demangleUnsigned(MangledName);
  280. return LSGVN;
  281. }
  282. static NamedIdentifierNode *synthesizeNamedIdentifier(ArenaAllocator &Arena,
  283. StringView Name) {
  284. NamedIdentifierNode *Id = Arena.alloc<NamedIdentifierNode>();
  285. Id->Name = Name;
  286. return Id;
  287. }
  288. static QualifiedNameNode *synthesizeQualifiedName(ArenaAllocator &Arena,
  289. IdentifierNode *Identifier) {
  290. QualifiedNameNode *QN = Arena.alloc<QualifiedNameNode>();
  291. QN->Components = Arena.alloc<NodeArrayNode>();
  292. QN->Components->Count = 1;
  293. QN->Components->Nodes = Arena.allocArray<Node *>(1);
  294. QN->Components->Nodes[0] = Identifier;
  295. return QN;
  296. }
  297. static QualifiedNameNode *synthesizeQualifiedName(ArenaAllocator &Arena,
  298. StringView Name) {
  299. NamedIdentifierNode *Id = synthesizeNamedIdentifier(Arena, Name);
  300. return synthesizeQualifiedName(Arena, Id);
  301. }
  302. static VariableSymbolNode *synthesizeVariable(ArenaAllocator &Arena,
  303. TypeNode *Type,
  304. StringView VariableName) {
  305. VariableSymbolNode *VSN = Arena.alloc<VariableSymbolNode>();
  306. VSN->Type = Type;
  307. VSN->Name = synthesizeQualifiedName(Arena, VariableName);
  308. return VSN;
  309. }
  310. VariableSymbolNode *Demangler::demangleUntypedVariable(
  311. ArenaAllocator &Arena, StringView &MangledName, StringView VariableName) {
  312. NamedIdentifierNode *NI = synthesizeNamedIdentifier(Arena, VariableName);
  313. QualifiedNameNode *QN = demangleNameScopeChain(MangledName, NI);
  314. VariableSymbolNode *VSN = Arena.alloc<VariableSymbolNode>();
  315. VSN->Name = QN;
  316. if (MangledName.consumeFront("8"))
  317. return VSN;
  318. Error = true;
  319. return nullptr;
  320. }
  321. VariableSymbolNode *
  322. Demangler::demangleRttiBaseClassDescriptorNode(ArenaAllocator &Arena,
  323. StringView &MangledName) {
  324. RttiBaseClassDescriptorNode *RBCDN =
  325. Arena.alloc<RttiBaseClassDescriptorNode>();
  326. RBCDN->NVOffset = demangleUnsigned(MangledName);
  327. RBCDN->VBPtrOffset = demangleSigned(MangledName);
  328. RBCDN->VBTableOffset = demangleUnsigned(MangledName);
  329. RBCDN->Flags = demangleUnsigned(MangledName);
  330. if (Error)
  331. return nullptr;
  332. VariableSymbolNode *VSN = Arena.alloc<VariableSymbolNode>();
  333. VSN->Name = demangleNameScopeChain(MangledName, RBCDN);
  334. MangledName.consumeFront('8');
  335. return VSN;
  336. }
  337. FunctionSymbolNode *Demangler::demangleInitFiniStub(StringView &MangledName,
  338. bool IsDestructor) {
  339. DynamicStructorIdentifierNode *DSIN =
  340. Arena.alloc<DynamicStructorIdentifierNode>();
  341. DSIN->IsDestructor = IsDestructor;
  342. bool IsKnownStaticDataMember = false;
  343. if (MangledName.consumeFront('?'))
  344. IsKnownStaticDataMember = true;
  345. SymbolNode *Symbol = demangleDeclarator(MangledName);
  346. if (Error)
  347. return nullptr;
  348. FunctionSymbolNode *FSN = nullptr;
  349. if (Symbol->kind() == NodeKind::VariableSymbol) {
  350. DSIN->Variable = static_cast<VariableSymbolNode *>(Symbol);
  351. // Older versions of clang mangled this type of symbol incorrectly. They
  352. // would omit the leading ? and they would only emit a single @ at the end.
  353. // The correct mangling is a leading ? and 2 trailing @ signs. Handle
  354. // both cases.
  355. int AtCount = IsKnownStaticDataMember ? 2 : 1;
  356. for (int I = 0; I < AtCount; ++I) {
  357. if (MangledName.consumeFront('@'))
  358. continue;
  359. Error = true;
  360. return nullptr;
  361. }
  362. FSN = demangleFunctionEncoding(MangledName);
  363. if (FSN)
  364. FSN->Name = synthesizeQualifiedName(Arena, DSIN);
  365. } else {
  366. if (IsKnownStaticDataMember) {
  367. // This was supposed to be a static data member, but we got a function.
  368. Error = true;
  369. return nullptr;
  370. }
  371. FSN = static_cast<FunctionSymbolNode *>(Symbol);
  372. DSIN->Name = Symbol->Name;
  373. FSN->Name = synthesizeQualifiedName(Arena, DSIN);
  374. }
  375. return FSN;
  376. }
  377. SymbolNode *Demangler::demangleSpecialIntrinsic(StringView &MangledName) {
  378. SpecialIntrinsicKind SIK = consumeSpecialIntrinsicKind(MangledName);
  379. switch (SIK) {
  380. case SpecialIntrinsicKind::None:
  381. return nullptr;
  382. case SpecialIntrinsicKind::StringLiteralSymbol:
  383. return demangleStringLiteral(MangledName);
  384. case SpecialIntrinsicKind::Vftable:
  385. case SpecialIntrinsicKind::Vbtable:
  386. case SpecialIntrinsicKind::LocalVftable:
  387. case SpecialIntrinsicKind::RttiCompleteObjLocator:
  388. return demangleSpecialTableSymbolNode(MangledName, SIK);
  389. case SpecialIntrinsicKind::VcallThunk:
  390. return demangleVcallThunkNode(MangledName);
  391. case SpecialIntrinsicKind::LocalStaticGuard:
  392. return demangleLocalStaticGuard(MangledName, /*IsThread=*/false);
  393. case SpecialIntrinsicKind::LocalStaticThreadGuard:
  394. return demangleLocalStaticGuard(MangledName, /*IsThread=*/true);
  395. case SpecialIntrinsicKind::RttiTypeDescriptor: {
  396. TypeNode *T = demangleType(MangledName, QualifierMangleMode::Result);
  397. if (Error)
  398. break;
  399. if (!MangledName.consumeFront("@8"))
  400. break;
  401. if (!MangledName.empty())
  402. break;
  403. return synthesizeVariable(Arena, T, "`RTTI Type Descriptor'");
  404. }
  405. case SpecialIntrinsicKind::RttiBaseClassArray:
  406. return demangleUntypedVariable(Arena, MangledName,
  407. "`RTTI Base Class Array'");
  408. case SpecialIntrinsicKind::RttiClassHierarchyDescriptor:
  409. return demangleUntypedVariable(Arena, MangledName,
  410. "`RTTI Class Hierarchy Descriptor'");
  411. case SpecialIntrinsicKind::RttiBaseClassDescriptor:
  412. return demangleRttiBaseClassDescriptorNode(Arena, MangledName);
  413. case SpecialIntrinsicKind::DynamicInitializer:
  414. return demangleInitFiniStub(MangledName, /*IsDestructor=*/false);
  415. case SpecialIntrinsicKind::DynamicAtexitDestructor:
  416. return demangleInitFiniStub(MangledName, /*IsDestructor=*/true);
  417. case SpecialIntrinsicKind::Typeof:
  418. case SpecialIntrinsicKind::UdtReturning:
  419. // It's unclear which tools produces these manglings, so demangling
  420. // support is not (yet?) implemented.
  421. break;
  422. case SpecialIntrinsicKind::Unknown:
  423. DEMANGLE_UNREACHABLE; // Never returned by consumeSpecialIntrinsicKind.
  424. }
  425. Error = true;
  426. return nullptr;
  427. }
  428. IdentifierNode *
  429. Demangler::demangleFunctionIdentifierCode(StringView &MangledName) {
  430. assert(MangledName.startsWith('?'));
  431. MangledName = MangledName.dropFront();
  432. if (MangledName.empty()) {
  433. Error = true;
  434. return nullptr;
  435. }
  436. if (MangledName.consumeFront("__"))
  437. return demangleFunctionIdentifierCode(
  438. MangledName, FunctionIdentifierCodeGroup::DoubleUnder);
  439. if (MangledName.consumeFront("_"))
  440. return demangleFunctionIdentifierCode(MangledName,
  441. FunctionIdentifierCodeGroup::Under);
  442. return demangleFunctionIdentifierCode(MangledName,
  443. FunctionIdentifierCodeGroup::Basic);
  444. }
  445. StructorIdentifierNode *
  446. Demangler::demangleStructorIdentifier(StringView &MangledName,
  447. bool IsDestructor) {
  448. StructorIdentifierNode *N = Arena.alloc<StructorIdentifierNode>();
  449. N->IsDestructor = IsDestructor;
  450. return N;
  451. }
  452. ConversionOperatorIdentifierNode *
  453. Demangler::demangleConversionOperatorIdentifier(StringView &MangledName) {
  454. ConversionOperatorIdentifierNode *N =
  455. Arena.alloc<ConversionOperatorIdentifierNode>();
  456. return N;
  457. }
  458. LiteralOperatorIdentifierNode *
  459. Demangler::demangleLiteralOperatorIdentifier(StringView &MangledName) {
  460. LiteralOperatorIdentifierNode *N =
  461. Arena.alloc<LiteralOperatorIdentifierNode>();
  462. N->Name = demangleSimpleString(MangledName, /*Memorize=*/false);
  463. return N;
  464. }
  465. IntrinsicFunctionKind
  466. Demangler::translateIntrinsicFunctionCode(char CH,
  467. FunctionIdentifierCodeGroup Group) {
  468. using IFK = IntrinsicFunctionKind;
  469. if (!(CH >= '0' && CH <= '9') && !(CH >= 'A' && CH <= 'Z')) {
  470. Error = true;
  471. return IFK::None;
  472. }
  473. // Not all ? identifiers are intrinsics *functions*. This function only maps
  474. // operator codes for the special functions, all others are handled elsewhere,
  475. // hence the IFK::None entries in the table.
  476. static IFK Basic[36] = {
  477. IFK::None, // ?0 # Foo::Foo()
  478. IFK::None, // ?1 # Foo::~Foo()
  479. IFK::New, // ?2 # operator new
  480. IFK::Delete, // ?3 # operator delete
  481. IFK::Assign, // ?4 # operator=
  482. IFK::RightShift, // ?5 # operator>>
  483. IFK::LeftShift, // ?6 # operator<<
  484. IFK::LogicalNot, // ?7 # operator!
  485. IFK::Equals, // ?8 # operator==
  486. IFK::NotEquals, // ?9 # operator!=
  487. IFK::ArraySubscript, // ?A # operator[]
  488. IFK::None, // ?B # Foo::operator <type>()
  489. IFK::Pointer, // ?C # operator->
  490. IFK::Dereference, // ?D # operator*
  491. IFK::Increment, // ?E # operator++
  492. IFK::Decrement, // ?F # operator--
  493. IFK::Minus, // ?G # operator-
  494. IFK::Plus, // ?H # operator+
  495. IFK::BitwiseAnd, // ?I # operator&
  496. IFK::MemberPointer, // ?J # operator->*
  497. IFK::Divide, // ?K # operator/
  498. IFK::Modulus, // ?L # operator%
  499. IFK::LessThan, // ?M operator<
  500. IFK::LessThanEqual, // ?N operator<=
  501. IFK::GreaterThan, // ?O operator>
  502. IFK::GreaterThanEqual, // ?P operator>=
  503. IFK::Comma, // ?Q operator,
  504. IFK::Parens, // ?R operator()
  505. IFK::BitwiseNot, // ?S operator~
  506. IFK::BitwiseXor, // ?T operator^
  507. IFK::BitwiseOr, // ?U operator|
  508. IFK::LogicalAnd, // ?V operator&&
  509. IFK::LogicalOr, // ?W operator||
  510. IFK::TimesEqual, // ?X operator*=
  511. IFK::PlusEqual, // ?Y operator+=
  512. IFK::MinusEqual, // ?Z operator-=
  513. };
  514. static IFK Under[36] = {
  515. IFK::DivEqual, // ?_0 operator/=
  516. IFK::ModEqual, // ?_1 operator%=
  517. IFK::RshEqual, // ?_2 operator>>=
  518. IFK::LshEqual, // ?_3 operator<<=
  519. IFK::BitwiseAndEqual, // ?_4 operator&=
  520. IFK::BitwiseOrEqual, // ?_5 operator|=
  521. IFK::BitwiseXorEqual, // ?_6 operator^=
  522. IFK::None, // ?_7 # vftable
  523. IFK::None, // ?_8 # vbtable
  524. IFK::None, // ?_9 # vcall
  525. IFK::None, // ?_A # typeof
  526. IFK::None, // ?_B # local static guard
  527. IFK::None, // ?_C # string literal
  528. IFK::VbaseDtor, // ?_D # vbase destructor
  529. IFK::VecDelDtor, // ?_E # vector deleting destructor
  530. IFK::DefaultCtorClosure, // ?_F # default constructor closure
  531. IFK::ScalarDelDtor, // ?_G # scalar deleting destructor
  532. IFK::VecCtorIter, // ?_H # vector constructor iterator
  533. IFK::VecDtorIter, // ?_I # vector destructor iterator
  534. IFK::VecVbaseCtorIter, // ?_J # vector vbase constructor iterator
  535. IFK::VdispMap, // ?_K # virtual displacement map
  536. IFK::EHVecCtorIter, // ?_L # eh vector constructor iterator
  537. IFK::EHVecDtorIter, // ?_M # eh vector destructor iterator
  538. IFK::EHVecVbaseCtorIter, // ?_N # eh vector vbase constructor iterator
  539. IFK::CopyCtorClosure, // ?_O # copy constructor closure
  540. IFK::None, // ?_P<name> # udt returning <name>
  541. IFK::None, // ?_Q # <unknown>
  542. IFK::None, // ?_R0 - ?_R4 # RTTI Codes
  543. IFK::None, // ?_S # local vftable
  544. IFK::LocalVftableCtorClosure, // ?_T # local vftable constructor closure
  545. IFK::ArrayNew, // ?_U operator new[]
  546. IFK::ArrayDelete, // ?_V operator delete[]
  547. IFK::None, // ?_W <unused>
  548. IFK::None, // ?_X <unused>
  549. IFK::None, // ?_Y <unused>
  550. IFK::None, // ?_Z <unused>
  551. };
  552. static IFK DoubleUnder[36] = {
  553. IFK::None, // ?__0 <unused>
  554. IFK::None, // ?__1 <unused>
  555. IFK::None, // ?__2 <unused>
  556. IFK::None, // ?__3 <unused>
  557. IFK::None, // ?__4 <unused>
  558. IFK::None, // ?__5 <unused>
  559. IFK::None, // ?__6 <unused>
  560. IFK::None, // ?__7 <unused>
  561. IFK::None, // ?__8 <unused>
  562. IFK::None, // ?__9 <unused>
  563. IFK::ManVectorCtorIter, // ?__A managed vector ctor iterator
  564. IFK::ManVectorDtorIter, // ?__B managed vector dtor iterator
  565. IFK::EHVectorCopyCtorIter, // ?__C EH vector copy ctor iterator
  566. IFK::EHVectorVbaseCopyCtorIter, // ?__D EH vector vbase copy ctor iter
  567. IFK::None, // ?__E dynamic initializer for `T'
  568. IFK::None, // ?__F dynamic atexit destructor for `T'
  569. IFK::VectorCopyCtorIter, // ?__G vector copy constructor iter
  570. IFK::VectorVbaseCopyCtorIter, // ?__H vector vbase copy ctor iter
  571. IFK::ManVectorVbaseCopyCtorIter, // ?__I managed vector vbase copy ctor
  572. // iter
  573. IFK::None, // ?__J local static thread guard
  574. IFK::None, // ?__K operator ""_name
  575. IFK::CoAwait, // ?__L operator co_await
  576. IFK::Spaceship, // ?__M operator<=>
  577. IFK::None, // ?__N <unused>
  578. IFK::None, // ?__O <unused>
  579. IFK::None, // ?__P <unused>
  580. IFK::None, // ?__Q <unused>
  581. IFK::None, // ?__R <unused>
  582. IFK::None, // ?__S <unused>
  583. IFK::None, // ?__T <unused>
  584. IFK::None, // ?__U <unused>
  585. IFK::None, // ?__V <unused>
  586. IFK::None, // ?__W <unused>
  587. IFK::None, // ?__X <unused>
  588. IFK::None, // ?__Y <unused>
  589. IFK::None, // ?__Z <unused>
  590. };
  591. int Index = (CH >= '0' && CH <= '9') ? (CH - '0') : (CH - 'A' + 10);
  592. switch (Group) {
  593. case FunctionIdentifierCodeGroup::Basic:
  594. return Basic[Index];
  595. case FunctionIdentifierCodeGroup::Under:
  596. return Under[Index];
  597. case FunctionIdentifierCodeGroup::DoubleUnder:
  598. return DoubleUnder[Index];
  599. }
  600. DEMANGLE_UNREACHABLE;
  601. }
  602. IdentifierNode *
  603. Demangler::demangleFunctionIdentifierCode(StringView &MangledName,
  604. FunctionIdentifierCodeGroup Group) {
  605. if (MangledName.empty()) {
  606. Error = true;
  607. return nullptr;
  608. }
  609. switch (Group) {
  610. case FunctionIdentifierCodeGroup::Basic:
  611. switch (char CH = MangledName.popFront()) {
  612. case '0':
  613. case '1':
  614. return demangleStructorIdentifier(MangledName, CH == '1');
  615. case 'B':
  616. return demangleConversionOperatorIdentifier(MangledName);
  617. default:
  618. return Arena.alloc<IntrinsicFunctionIdentifierNode>(
  619. translateIntrinsicFunctionCode(CH, Group));
  620. }
  621. case FunctionIdentifierCodeGroup::Under:
  622. return Arena.alloc<IntrinsicFunctionIdentifierNode>(
  623. translateIntrinsicFunctionCode(MangledName.popFront(), Group));
  624. case FunctionIdentifierCodeGroup::DoubleUnder:
  625. switch (char CH = MangledName.popFront()) {
  626. case 'K':
  627. return demangleLiteralOperatorIdentifier(MangledName);
  628. default:
  629. return Arena.alloc<IntrinsicFunctionIdentifierNode>(
  630. translateIntrinsicFunctionCode(CH, Group));
  631. }
  632. }
  633. DEMANGLE_UNREACHABLE;
  634. }
  635. SymbolNode *Demangler::demangleEncodedSymbol(StringView &MangledName,
  636. QualifiedNameNode *Name) {
  637. if (MangledName.empty()) {
  638. Error = true;
  639. return nullptr;
  640. }
  641. // Read a variable.
  642. switch (MangledName.front()) {
  643. case '0':
  644. case '1':
  645. case '2':
  646. case '3':
  647. case '4': {
  648. StorageClass SC = demangleVariableStorageClass(MangledName);
  649. return demangleVariableEncoding(MangledName, SC);
  650. }
  651. }
  652. FunctionSymbolNode *FSN = demangleFunctionEncoding(MangledName);
  653. IdentifierNode *UQN = Name->getUnqualifiedIdentifier();
  654. if (UQN->kind() == NodeKind::ConversionOperatorIdentifier) {
  655. ConversionOperatorIdentifierNode *COIN =
  656. static_cast<ConversionOperatorIdentifierNode *>(UQN);
  657. if (FSN)
  658. COIN->TargetType = FSN->Signature->ReturnType;
  659. }
  660. return FSN;
  661. }
  662. SymbolNode *Demangler::demangleDeclarator(StringView &MangledName) {
  663. // What follows is a main symbol name. This may include namespaces or class
  664. // back references.
  665. QualifiedNameNode *QN = demangleFullyQualifiedSymbolName(MangledName);
  666. if (Error)
  667. return nullptr;
  668. SymbolNode *Symbol = demangleEncodedSymbol(MangledName, QN);
  669. if (Error)
  670. return nullptr;
  671. Symbol->Name = QN;
  672. IdentifierNode *UQN = QN->getUnqualifiedIdentifier();
  673. if (UQN->kind() == NodeKind::ConversionOperatorIdentifier) {
  674. ConversionOperatorIdentifierNode *COIN =
  675. static_cast<ConversionOperatorIdentifierNode *>(UQN);
  676. if (!COIN->TargetType) {
  677. Error = true;
  678. return nullptr;
  679. }
  680. }
  681. return Symbol;
  682. }
  683. SymbolNode *Demangler::demangleMD5Name(StringView &MangledName) {
  684. assert(MangledName.startsWith("??@"));
  685. // This is an MD5 mangled name. We can't demangle it, just return the
  686. // mangled name.
  687. // An MD5 mangled name is ??@ followed by 32 characters and a terminating @.
  688. size_t MD5Last = MangledName.find('@', strlen("??@"));
  689. if (MD5Last == StringView::npos) {
  690. Error = true;
  691. return nullptr;
  692. }
  693. const char *Start = MangledName.begin();
  694. MangledName = MangledName.dropFront(MD5Last + 1);
  695. // There are two additional special cases for MD5 names:
  696. // 1. For complete object locators where the object name is long enough
  697. // for the object to have an MD5 name, the complete object locator is
  698. // called ??@...@??_R4@ (with a trailing "??_R4@" instead of the usual
  699. // leading "??_R4". This is handled here.
  700. // 2. For catchable types, in versions of MSVC before 2015 (<1900) or after
  701. // 2017.2 (>= 1914), the catchable type mangling is _CT??@...@??@...@8
  702. // instead of_CT??@...@8 with just one MD5 name. Since we don't yet
  703. // demangle catchable types anywhere, this isn't handled for MD5 names
  704. // either.
  705. MangledName.consumeFront("??_R4@");
  706. StringView MD5(Start, MangledName.begin());
  707. SymbolNode *S = Arena.alloc<SymbolNode>(NodeKind::Md5Symbol);
  708. S->Name = synthesizeQualifiedName(Arena, MD5);
  709. return S;
  710. }
  711. SymbolNode *Demangler::demangleTypeinfoName(StringView &MangledName) {
  712. assert(MangledName.startsWith('.'));
  713. MangledName.consumeFront('.');
  714. TypeNode *T = demangleType(MangledName, QualifierMangleMode::Result);
  715. if (Error || !MangledName.empty()) {
  716. Error = true;
  717. return nullptr;
  718. }
  719. return synthesizeVariable(Arena, T, "`RTTI Type Descriptor Name'");
  720. }
  721. // Parser entry point.
  722. SymbolNode *Demangler::parse(StringView &MangledName) {
  723. // Typeinfo names are strings stored in RTTI data. They're not symbol names.
  724. // It's still useful to demangle them. They're the only demangled entity
  725. // that doesn't start with a "?" but a ".".
  726. if (MangledName.startsWith('.'))
  727. return demangleTypeinfoName(MangledName);
  728. if (MangledName.startsWith("??@"))
  729. return demangleMD5Name(MangledName);
  730. // MSVC-style mangled symbols must start with '?'.
  731. if (!MangledName.startsWith('?')) {
  732. Error = true;
  733. return nullptr;
  734. }
  735. MangledName.consumeFront('?');
  736. // ?$ is a template instantiation, but all other names that start with ? are
  737. // operators / special names.
  738. if (SymbolNode *SI = demangleSpecialIntrinsic(MangledName))
  739. return SI;
  740. return demangleDeclarator(MangledName);
  741. }
  742. TagTypeNode *Demangler::parseTagUniqueName(StringView &MangledName) {
  743. if (!MangledName.consumeFront(".?A"))
  744. return nullptr;
  745. MangledName.consumeFront(".?A");
  746. if (MangledName.empty())
  747. return nullptr;
  748. return demangleClassType(MangledName);
  749. }
  750. // <type-encoding> ::= <storage-class> <variable-type>
  751. // <storage-class> ::= 0 # private static member
  752. // ::= 1 # protected static member
  753. // ::= 2 # public static member
  754. // ::= 3 # global
  755. // ::= 4 # static local
  756. VariableSymbolNode *Demangler::demangleVariableEncoding(StringView &MangledName,
  757. StorageClass SC) {
  758. VariableSymbolNode *VSN = Arena.alloc<VariableSymbolNode>();
  759. VSN->Type = demangleType(MangledName, QualifierMangleMode::Drop);
  760. VSN->SC = SC;
  761. if (Error)
  762. return nullptr;
  763. // <variable-type> ::= <type> <cvr-qualifiers>
  764. // ::= <type> <pointee-cvr-qualifiers> # pointers, references
  765. switch (VSN->Type->kind()) {
  766. case NodeKind::PointerType: {
  767. PointerTypeNode *PTN = static_cast<PointerTypeNode *>(VSN->Type);
  768. Qualifiers ExtraChildQuals = Q_None;
  769. PTN->Quals = Qualifiers(VSN->Type->Quals |
  770. demanglePointerExtQualifiers(MangledName));
  771. bool IsMember = false;
  772. std::tie(ExtraChildQuals, IsMember) = demangleQualifiers(MangledName);
  773. if (PTN->ClassParent) {
  774. QualifiedNameNode *BackRefName =
  775. demangleFullyQualifiedTypeName(MangledName);
  776. (void)BackRefName;
  777. }
  778. PTN->Pointee->Quals = Qualifiers(PTN->Pointee->Quals | ExtraChildQuals);
  779. break;
  780. }
  781. default:
  782. VSN->Type->Quals = demangleQualifiers(MangledName).first;
  783. break;
  784. }
  785. return VSN;
  786. }
  787. // Sometimes numbers are encoded in mangled symbols. For example,
  788. // "int (*x)[20]" is a valid C type (x is a pointer to an array of
  789. // length 20), so we need some way to embed numbers as part of symbols.
  790. // This function parses it.
  791. //
  792. // <number> ::= [?] <non-negative integer>
  793. //
  794. // <non-negative integer> ::= <decimal digit> # when 1 <= Number <= 10
  795. // ::= <hex digit>+ @ # when Number == 0 or >= 10
  796. //
  797. // <hex-digit> ::= [A-P] # A = 0, B = 1, ...
  798. std::pair<uint64_t, bool> Demangler::demangleNumber(StringView &MangledName) {
  799. bool IsNegative = MangledName.consumeFront('?');
  800. if (startsWithDigit(MangledName)) {
  801. uint64_t Ret = MangledName[0] - '0' + 1;
  802. MangledName = MangledName.dropFront(1);
  803. return {Ret, IsNegative};
  804. }
  805. uint64_t Ret = 0;
  806. for (size_t i = 0; i < MangledName.size(); ++i) {
  807. char C = MangledName[i];
  808. if (C == '@') {
  809. MangledName = MangledName.dropFront(i + 1);
  810. return {Ret, IsNegative};
  811. }
  812. if ('A' <= C && C <= 'P') {
  813. Ret = (Ret << 4) + (C - 'A');
  814. continue;
  815. }
  816. break;
  817. }
  818. Error = true;
  819. return {0ULL, false};
  820. }
  821. uint64_t Demangler::demangleUnsigned(StringView &MangledName) {
  822. bool IsNegative = false;
  823. uint64_t Number = 0;
  824. std::tie(Number, IsNegative) = demangleNumber(MangledName);
  825. if (IsNegative)
  826. Error = true;
  827. return Number;
  828. }
  829. int64_t Demangler::demangleSigned(StringView &MangledName) {
  830. bool IsNegative = false;
  831. uint64_t Number = 0;
  832. std::tie(Number, IsNegative) = demangleNumber(MangledName);
  833. if (Number > INT64_MAX)
  834. Error = true;
  835. int64_t I = static_cast<int64_t>(Number);
  836. return IsNegative ? -I : I;
  837. }
  838. // First 10 strings can be referenced by special BackReferences ?0, ?1, ..., ?9.
  839. // Memorize it.
  840. void Demangler::memorizeString(StringView S) {
  841. if (Backrefs.NamesCount >= BackrefContext::Max)
  842. return;
  843. for (size_t i = 0; i < Backrefs.NamesCount; ++i)
  844. if (S == Backrefs.Names[i]->Name)
  845. return;
  846. NamedIdentifierNode *N = Arena.alloc<NamedIdentifierNode>();
  847. N->Name = S;
  848. Backrefs.Names[Backrefs.NamesCount++] = N;
  849. }
  850. NamedIdentifierNode *Demangler::demangleBackRefName(StringView &MangledName) {
  851. assert(startsWithDigit(MangledName));
  852. size_t I = MangledName[0] - '0';
  853. if (I >= Backrefs.NamesCount) {
  854. Error = true;
  855. return nullptr;
  856. }
  857. MangledName = MangledName.dropFront();
  858. return Backrefs.Names[I];
  859. }
  860. void Demangler::memorizeIdentifier(IdentifierNode *Identifier) {
  861. // Render this class template name into a string buffer so that we can
  862. // memorize it for the purpose of back-referencing.
  863. OutputStream OS;
  864. if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
  865. // FIXME: Propagate out-of-memory as an error?
  866. std::terminate();
  867. Identifier->output(OS, OF_Default);
  868. OS << '\0';
  869. char *Name = OS.getBuffer();
  870. StringView Owned = copyString(Name);
  871. memorizeString(Owned);
  872. std::free(Name);
  873. }
  874. IdentifierNode *
  875. Demangler::demangleTemplateInstantiationName(StringView &MangledName,
  876. NameBackrefBehavior NBB) {
  877. assert(MangledName.startsWith("?$"));
  878. MangledName.consumeFront("?$");
  879. BackrefContext OuterContext;
  880. std::swap(OuterContext, Backrefs);
  881. IdentifierNode *Identifier =
  882. demangleUnqualifiedSymbolName(MangledName, NBB_Simple);
  883. if (!Error)
  884. Identifier->TemplateParams = demangleTemplateParameterList(MangledName);
  885. std::swap(OuterContext, Backrefs);
  886. if (Error)
  887. return nullptr;
  888. if (NBB & NBB_Template) {
  889. // NBB_Template is only set for types and non-leaf names ("a::" in "a::b").
  890. // Structors and conversion operators only makes sense in a leaf name, so
  891. // reject them in NBB_Template contexts.
  892. if (Identifier->kind() == NodeKind::ConversionOperatorIdentifier ||
  893. Identifier->kind() == NodeKind::StructorIdentifier) {
  894. Error = true;
  895. return nullptr;
  896. }
  897. memorizeIdentifier(Identifier);
  898. }
  899. return Identifier;
  900. }
  901. NamedIdentifierNode *Demangler::demangleSimpleName(StringView &MangledName,
  902. bool Memorize) {
  903. StringView S = demangleSimpleString(MangledName, Memorize);
  904. if (Error)
  905. return nullptr;
  906. NamedIdentifierNode *Name = Arena.alloc<NamedIdentifierNode>();
  907. Name->Name = S;
  908. return Name;
  909. }
  910. static bool isRebasedHexDigit(char C) { return (C >= 'A' && C <= 'P'); }
  911. static uint8_t rebasedHexDigitToNumber(char C) {
  912. assert(isRebasedHexDigit(C));
  913. return (C <= 'J') ? (C - 'A') : (10 + C - 'K');
  914. }
  915. uint8_t Demangler::demangleCharLiteral(StringView &MangledName) {
  916. assert(!MangledName.empty());
  917. if (!MangledName.startsWith('?'))
  918. return MangledName.popFront();
  919. MangledName = MangledName.dropFront();
  920. if (MangledName.empty())
  921. goto CharLiteralError;
  922. if (MangledName.consumeFront('$')) {
  923. // Two hex digits
  924. if (MangledName.size() < 2)
  925. goto CharLiteralError;
  926. StringView Nibbles = MangledName.substr(0, 2);
  927. if (!isRebasedHexDigit(Nibbles[0]) || !isRebasedHexDigit(Nibbles[1]))
  928. goto CharLiteralError;
  929. // Don't append the null terminator.
  930. uint8_t C1 = rebasedHexDigitToNumber(Nibbles[0]);
  931. uint8_t C2 = rebasedHexDigitToNumber(Nibbles[1]);
  932. MangledName = MangledName.dropFront(2);
  933. return (C1 << 4) | C2;
  934. }
  935. if (startsWithDigit(MangledName)) {
  936. const char *Lookup = ",/\\:. \n\t'-";
  937. char C = Lookup[MangledName[0] - '0'];
  938. MangledName = MangledName.dropFront();
  939. return C;
  940. }
  941. if (MangledName[0] >= 'a' && MangledName[0] <= 'z') {
  942. char Lookup[26] = {'\xE1', '\xE2', '\xE3', '\xE4', '\xE5', '\xE6', '\xE7',
  943. '\xE8', '\xE9', '\xEA', '\xEB', '\xEC', '\xED', '\xEE',
  944. '\xEF', '\xF0', '\xF1', '\xF2', '\xF3', '\xF4', '\xF5',
  945. '\xF6', '\xF7', '\xF8', '\xF9', '\xFA'};
  946. char C = Lookup[MangledName[0] - 'a'];
  947. MangledName = MangledName.dropFront();
  948. return C;
  949. }
  950. if (MangledName[0] >= 'A' && MangledName[0] <= 'Z') {
  951. char Lookup[26] = {'\xC1', '\xC2', '\xC3', '\xC4', '\xC5', '\xC6', '\xC7',
  952. '\xC8', '\xC9', '\xCA', '\xCB', '\xCC', '\xCD', '\xCE',
  953. '\xCF', '\xD0', '\xD1', '\xD2', '\xD3', '\xD4', '\xD5',
  954. '\xD6', '\xD7', '\xD8', '\xD9', '\xDA'};
  955. char C = Lookup[MangledName[0] - 'A'];
  956. MangledName = MangledName.dropFront();
  957. return C;
  958. }
  959. CharLiteralError:
  960. Error = true;
  961. return '\0';
  962. }
  963. wchar_t Demangler::demangleWcharLiteral(StringView &MangledName) {
  964. uint8_t C1, C2;
  965. C1 = demangleCharLiteral(MangledName);
  966. if (Error || MangledName.empty())
  967. goto WCharLiteralError;
  968. C2 = demangleCharLiteral(MangledName);
  969. if (Error)
  970. goto WCharLiteralError;
  971. return ((wchar_t)C1 << 8) | (wchar_t)C2;
  972. WCharLiteralError:
  973. Error = true;
  974. return L'\0';
  975. }
  976. static void writeHexDigit(char *Buffer, uint8_t Digit) {
  977. assert(Digit <= 15);
  978. *Buffer = (Digit < 10) ? ('0' + Digit) : ('A' + Digit - 10);
  979. }
  980. static void outputHex(OutputStream &OS, unsigned C) {
  981. assert (C != 0);
  982. // It's easier to do the math if we can work from right to left, but we need
  983. // to print the numbers from left to right. So render this into a temporary
  984. // buffer first, then output the temporary buffer. Each byte is of the form
  985. // \xAB, which means that each byte needs 4 characters. Since there are at
  986. // most 4 bytes, we need a 4*4+1 = 17 character temporary buffer.
  987. char TempBuffer[17];
  988. ::memset(TempBuffer, 0, sizeof(TempBuffer));
  989. constexpr int MaxPos = sizeof(TempBuffer) - 1;
  990. int Pos = MaxPos - 1; // TempBuffer[MaxPos] is the terminating \0.
  991. while (C != 0) {
  992. for (int I = 0; I < 2; ++I) {
  993. writeHexDigit(&TempBuffer[Pos--], C % 16);
  994. C /= 16;
  995. }
  996. }
  997. TempBuffer[Pos--] = 'x';
  998. assert(Pos >= 0);
  999. TempBuffer[Pos--] = '\\';
  1000. OS << StringView(&TempBuffer[Pos + 1]);
  1001. }
  1002. static void outputEscapedChar(OutputStream &OS, unsigned C) {
  1003. switch (C) {
  1004. case '\0': // nul
  1005. OS << "\\0";
  1006. return;
  1007. case '\'': // single quote
  1008. OS << "\\\'";
  1009. return;
  1010. case '\"': // double quote
  1011. OS << "\\\"";
  1012. return;
  1013. case '\\': // backslash
  1014. OS << "\\\\";
  1015. return;
  1016. case '\a': // bell
  1017. OS << "\\a";
  1018. return;
  1019. case '\b': // backspace
  1020. OS << "\\b";
  1021. return;
  1022. case '\f': // form feed
  1023. OS << "\\f";
  1024. return;
  1025. case '\n': // new line
  1026. OS << "\\n";
  1027. return;
  1028. case '\r': // carriage return
  1029. OS << "\\r";
  1030. return;
  1031. case '\t': // tab
  1032. OS << "\\t";
  1033. return;
  1034. case '\v': // vertical tab
  1035. OS << "\\v";
  1036. return;
  1037. default:
  1038. break;
  1039. }
  1040. if (C > 0x1F && C < 0x7F) {
  1041. // Standard ascii char.
  1042. OS << (char)C;
  1043. return;
  1044. }
  1045. outputHex(OS, C);
  1046. }
  1047. static unsigned countTrailingNullBytes(const uint8_t *StringBytes, int Length) {
  1048. const uint8_t *End = StringBytes + Length - 1;
  1049. unsigned Count = 0;
  1050. while (Length > 0 && *End == 0) {
  1051. --Length;
  1052. --End;
  1053. ++Count;
  1054. }
  1055. return Count;
  1056. }
  1057. static unsigned countEmbeddedNulls(const uint8_t *StringBytes,
  1058. unsigned Length) {
  1059. unsigned Result = 0;
  1060. for (unsigned I = 0; I < Length; ++I) {
  1061. if (*StringBytes++ == 0)
  1062. ++Result;
  1063. }
  1064. return Result;
  1065. }
  1066. // A mangled (non-wide) string literal stores the total length of the string it
  1067. // refers to (passed in NumBytes), and it contains up to 32 bytes of actual text
  1068. // (passed in StringBytes, NumChars).
  1069. static unsigned guessCharByteSize(const uint8_t *StringBytes, unsigned NumChars,
  1070. uint64_t NumBytes) {
  1071. assert(NumBytes > 0);
  1072. // If the number of bytes is odd, this is guaranteed to be a char string.
  1073. if (NumBytes % 2 == 1)
  1074. return 1;
  1075. // All strings can encode at most 32 bytes of data. If it's less than that,
  1076. // then we encoded the entire string. In this case we check for a 1-byte,
  1077. // 2-byte, or 4-byte null terminator.
  1078. if (NumBytes < 32) {
  1079. unsigned TrailingNulls = countTrailingNullBytes(StringBytes, NumChars);
  1080. if (TrailingNulls >= 4 && NumBytes % 4 == 0)
  1081. return 4;
  1082. if (TrailingNulls >= 2)
  1083. return 2;
  1084. return 1;
  1085. }
  1086. // The whole string was not able to be encoded. Try to look at embedded null
  1087. // terminators to guess. The heuristic is that we count all embedded null
  1088. // terminators. If more than 2/3 are null, it's a char32. If more than 1/3
  1089. // are null, it's a char16. Otherwise it's a char8. This obviously isn't
  1090. // perfect and is biased towards languages that have ascii alphabets, but this
  1091. // was always going to be best effort since the encoding is lossy.
  1092. unsigned Nulls = countEmbeddedNulls(StringBytes, NumChars);
  1093. if (Nulls >= 2 * NumChars / 3 && NumBytes % 4 == 0)
  1094. return 4;
  1095. if (Nulls >= NumChars / 3)
  1096. return 2;
  1097. return 1;
  1098. }
  1099. static unsigned decodeMultiByteChar(const uint8_t *StringBytes,
  1100. unsigned CharIndex, unsigned CharBytes) {
  1101. assert(CharBytes == 1 || CharBytes == 2 || CharBytes == 4);
  1102. unsigned Offset = CharIndex * CharBytes;
  1103. unsigned Result = 0;
  1104. StringBytes = StringBytes + Offset;
  1105. for (unsigned I = 0; I < CharBytes; ++I) {
  1106. unsigned C = static_cast<unsigned>(StringBytes[I]);
  1107. Result |= C << (8 * I);
  1108. }
  1109. return Result;
  1110. }
  1111. FunctionSymbolNode *Demangler::demangleVcallThunkNode(StringView &MangledName) {
  1112. FunctionSymbolNode *FSN = Arena.alloc<FunctionSymbolNode>();
  1113. VcallThunkIdentifierNode *VTIN = Arena.alloc<VcallThunkIdentifierNode>();
  1114. FSN->Signature = Arena.alloc<ThunkSignatureNode>();
  1115. FSN->Signature->FunctionClass = FC_NoParameterList;
  1116. FSN->Name = demangleNameScopeChain(MangledName, VTIN);
  1117. if (!Error)
  1118. Error = !MangledName.consumeFront("$B");
  1119. if (!Error)
  1120. VTIN->OffsetInVTable = demangleUnsigned(MangledName);
  1121. if (!Error)
  1122. Error = !MangledName.consumeFront('A');
  1123. if (!Error)
  1124. FSN->Signature->CallConvention = demangleCallingConvention(MangledName);
  1125. return (Error) ? nullptr : FSN;
  1126. }
  1127. EncodedStringLiteralNode *
  1128. Demangler::demangleStringLiteral(StringView &MangledName) {
  1129. // This function uses goto, so declare all variables up front.
  1130. OutputStream OS;
  1131. StringView CRC;
  1132. uint64_t StringByteSize;
  1133. bool IsWcharT = false;
  1134. bool IsNegative = false;
  1135. size_t CrcEndPos = 0;
  1136. char *ResultBuffer = nullptr;
  1137. EncodedStringLiteralNode *Result = Arena.alloc<EncodedStringLiteralNode>();
  1138. // Must happen before the first `goto StringLiteralError`.
  1139. if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
  1140. // FIXME: Propagate out-of-memory as an error?
  1141. std::terminate();
  1142. // Prefix indicating the beginning of a string literal
  1143. if (!MangledName.consumeFront("@_"))
  1144. goto StringLiteralError;
  1145. if (MangledName.empty())
  1146. goto StringLiteralError;
  1147. // Char Type (regular or wchar_t)
  1148. switch (MangledName.popFront()) {
  1149. case '1':
  1150. IsWcharT = true;
  1151. DEMANGLE_FALLTHROUGH;
  1152. case '0':
  1153. break;
  1154. default:
  1155. goto StringLiteralError;
  1156. }
  1157. // Encoded Length
  1158. std::tie(StringByteSize, IsNegative) = demangleNumber(MangledName);
  1159. if (Error || IsNegative || StringByteSize < (IsWcharT ? 2 : 1))
  1160. goto StringLiteralError;
  1161. // CRC 32 (always 8 characters plus a terminator)
  1162. CrcEndPos = MangledName.find('@');
  1163. if (CrcEndPos == StringView::npos)
  1164. goto StringLiteralError;
  1165. CRC = MangledName.substr(0, CrcEndPos);
  1166. MangledName = MangledName.dropFront(CrcEndPos + 1);
  1167. if (MangledName.empty())
  1168. goto StringLiteralError;
  1169. if (IsWcharT) {
  1170. Result->Char = CharKind::Wchar;
  1171. if (StringByteSize > 64)
  1172. Result->IsTruncated = true;
  1173. while (!MangledName.consumeFront('@')) {
  1174. if (MangledName.size() < 2)
  1175. goto StringLiteralError;
  1176. wchar_t W = demangleWcharLiteral(MangledName);
  1177. if (StringByteSize != 2 || Result->IsTruncated)
  1178. outputEscapedChar(OS, W);
  1179. StringByteSize -= 2;
  1180. if (Error)
  1181. goto StringLiteralError;
  1182. }
  1183. } else {
  1184. // The max byte length is actually 32, but some compilers mangled strings
  1185. // incorrectly, so we have to assume it can go higher.
  1186. constexpr unsigned MaxStringByteLength = 32 * 4;
  1187. uint8_t StringBytes[MaxStringByteLength];
  1188. unsigned BytesDecoded = 0;
  1189. while (!MangledName.consumeFront('@')) {
  1190. if (MangledName.size() < 1 || BytesDecoded >= MaxStringByteLength)
  1191. goto StringLiteralError;
  1192. StringBytes[BytesDecoded++] = demangleCharLiteral(MangledName);
  1193. }
  1194. if (StringByteSize > BytesDecoded)
  1195. Result->IsTruncated = true;
  1196. unsigned CharBytes =
  1197. guessCharByteSize(StringBytes, BytesDecoded, StringByteSize);
  1198. assert(StringByteSize % CharBytes == 0);
  1199. switch (CharBytes) {
  1200. case 1:
  1201. Result->Char = CharKind::Char;
  1202. break;
  1203. case 2:
  1204. Result->Char = CharKind::Char16;
  1205. break;
  1206. case 4:
  1207. Result->Char = CharKind::Char32;
  1208. break;
  1209. default:
  1210. DEMANGLE_UNREACHABLE;
  1211. }
  1212. const unsigned NumChars = BytesDecoded / CharBytes;
  1213. for (unsigned CharIndex = 0; CharIndex < NumChars; ++CharIndex) {
  1214. unsigned NextChar =
  1215. decodeMultiByteChar(StringBytes, CharIndex, CharBytes);
  1216. if (CharIndex + 1 < NumChars || Result->IsTruncated)
  1217. outputEscapedChar(OS, NextChar);
  1218. }
  1219. }
  1220. OS << '\0';
  1221. ResultBuffer = OS.getBuffer();
  1222. Result->DecodedString = copyString(ResultBuffer);
  1223. std::free(ResultBuffer);
  1224. return Result;
  1225. StringLiteralError:
  1226. Error = true;
  1227. std::free(OS.getBuffer());
  1228. return nullptr;
  1229. }
  1230. // Returns MangledName's prefix before the first '@', or an error if
  1231. // MangledName contains no '@' or the prefix has length 0.
  1232. StringView Demangler::demangleSimpleString(StringView &MangledName,
  1233. bool Memorize) {
  1234. StringView S;
  1235. for (size_t i = 0; i < MangledName.size(); ++i) {
  1236. if (MangledName[i] != '@')
  1237. continue;
  1238. if (i == 0)
  1239. break;
  1240. S = MangledName.substr(0, i);
  1241. MangledName = MangledName.dropFront(i + 1);
  1242. if (Memorize)
  1243. memorizeString(S);
  1244. return S;
  1245. }
  1246. Error = true;
  1247. return {};
  1248. }
  1249. NamedIdentifierNode *
  1250. Demangler::demangleAnonymousNamespaceName(StringView &MangledName) {
  1251. assert(MangledName.startsWith("?A"));
  1252. MangledName.consumeFront("?A");
  1253. NamedIdentifierNode *Node = Arena.alloc<NamedIdentifierNode>();
  1254. Node->Name = "`anonymous namespace'";
  1255. size_t EndPos = MangledName.find('@');
  1256. if (EndPos == StringView::npos) {
  1257. Error = true;
  1258. return nullptr;
  1259. }
  1260. StringView NamespaceKey = MangledName.substr(0, EndPos);
  1261. memorizeString(NamespaceKey);
  1262. MangledName = MangledName.substr(EndPos + 1);
  1263. return Node;
  1264. }
  1265. NamedIdentifierNode *
  1266. Demangler::demangleLocallyScopedNamePiece(StringView &MangledName) {
  1267. assert(startsWithLocalScopePattern(MangledName));
  1268. NamedIdentifierNode *Identifier = Arena.alloc<NamedIdentifierNode>();
  1269. MangledName.consumeFront('?');
  1270. uint64_t Number = 0;
  1271. bool IsNegative = false;
  1272. std::tie(Number, IsNegative) = demangleNumber(MangledName);
  1273. assert(!IsNegative);
  1274. // One ? to terminate the number
  1275. MangledName.consumeFront('?');
  1276. assert(!Error);
  1277. Node *Scope = parse(MangledName);
  1278. if (Error)
  1279. return nullptr;
  1280. // Render the parent symbol's name into a buffer.
  1281. OutputStream OS;
  1282. if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
  1283. // FIXME: Propagate out-of-memory as an error?
  1284. std::terminate();
  1285. OS << '`';
  1286. Scope->output(OS, OF_Default);
  1287. OS << '\'';
  1288. OS << "::`" << Number << "'";
  1289. OS << '\0';
  1290. char *Result = OS.getBuffer();
  1291. Identifier->Name = copyString(Result);
  1292. std::free(Result);
  1293. return Identifier;
  1294. }
  1295. // Parses a type name in the form of A@B@C@@ which represents C::B::A.
  1296. QualifiedNameNode *
  1297. Demangler::demangleFullyQualifiedTypeName(StringView &MangledName) {
  1298. IdentifierNode *Identifier =
  1299. demangleUnqualifiedTypeName(MangledName, /*Memorize=*/true);
  1300. if (Error)
  1301. return nullptr;
  1302. assert(Identifier);
  1303. QualifiedNameNode *QN = demangleNameScopeChain(MangledName, Identifier);
  1304. if (Error)
  1305. return nullptr;
  1306. assert(QN);
  1307. return QN;
  1308. }
  1309. // Parses a symbol name in the form of A@B@C@@ which represents C::B::A.
  1310. // Symbol names have slightly different rules regarding what can appear
  1311. // so we separate out the implementations for flexibility.
  1312. QualifiedNameNode *
  1313. Demangler::demangleFullyQualifiedSymbolName(StringView &MangledName) {
  1314. // This is the final component of a symbol name (i.e. the leftmost component
  1315. // of a mangled name. Since the only possible template instantiation that
  1316. // can appear in this context is a function template, and since those are
  1317. // not saved for the purposes of name backreferences, only backref simple
  1318. // names.
  1319. IdentifierNode *Identifier =
  1320. demangleUnqualifiedSymbolName(MangledName, NBB_Simple);
  1321. if (Error)
  1322. return nullptr;
  1323. QualifiedNameNode *QN = demangleNameScopeChain(MangledName, Identifier);
  1324. if (Error)
  1325. return nullptr;
  1326. if (Identifier->kind() == NodeKind::StructorIdentifier) {
  1327. if (QN->Components->Count < 2) {
  1328. Error = true;
  1329. return nullptr;
  1330. }
  1331. StructorIdentifierNode *SIN =
  1332. static_cast<StructorIdentifierNode *>(Identifier);
  1333. Node *ClassNode = QN->Components->Nodes[QN->Components->Count - 2];
  1334. SIN->Class = static_cast<IdentifierNode *>(ClassNode);
  1335. }
  1336. assert(QN);
  1337. return QN;
  1338. }
  1339. IdentifierNode *Demangler::demangleUnqualifiedTypeName(StringView &MangledName,
  1340. bool Memorize) {
  1341. // An inner-most name can be a back-reference, because a fully-qualified name
  1342. // (e.g. Scope + Inner) can contain other fully qualified names inside of
  1343. // them (for example template parameters), and these nested parameters can
  1344. // refer to previously mangled types.
  1345. if (startsWithDigit(MangledName))
  1346. return demangleBackRefName(MangledName);
  1347. if (MangledName.startsWith("?$"))
  1348. return demangleTemplateInstantiationName(MangledName, NBB_Template);
  1349. return demangleSimpleName(MangledName, Memorize);
  1350. }
  1351. IdentifierNode *
  1352. Demangler::demangleUnqualifiedSymbolName(StringView &MangledName,
  1353. NameBackrefBehavior NBB) {
  1354. if (startsWithDigit(MangledName))
  1355. return demangleBackRefName(MangledName);
  1356. if (MangledName.startsWith("?$"))
  1357. return demangleTemplateInstantiationName(MangledName, NBB);
  1358. if (MangledName.startsWith('?'))
  1359. return demangleFunctionIdentifierCode(MangledName);
  1360. return demangleSimpleName(MangledName, /*Memorize=*/(NBB & NBB_Simple) != 0);
  1361. }
  1362. IdentifierNode *Demangler::demangleNameScopePiece(StringView &MangledName) {
  1363. if (startsWithDigit(MangledName))
  1364. return demangleBackRefName(MangledName);
  1365. if (MangledName.startsWith("?$"))
  1366. return demangleTemplateInstantiationName(MangledName, NBB_Template);
  1367. if (MangledName.startsWith("?A"))
  1368. return demangleAnonymousNamespaceName(MangledName);
  1369. if (startsWithLocalScopePattern(MangledName))
  1370. return demangleLocallyScopedNamePiece(MangledName);
  1371. return demangleSimpleName(MangledName, /*Memorize=*/true);
  1372. }
  1373. static NodeArrayNode *nodeListToNodeArray(ArenaAllocator &Arena, NodeList *Head,
  1374. size_t Count) {
  1375. NodeArrayNode *N = Arena.alloc<NodeArrayNode>();
  1376. N->Count = Count;
  1377. N->Nodes = Arena.allocArray<Node *>(Count);
  1378. for (size_t I = 0; I < Count; ++I) {
  1379. N->Nodes[I] = Head->N;
  1380. Head = Head->Next;
  1381. }
  1382. return N;
  1383. }
  1384. QualifiedNameNode *
  1385. Demangler::demangleNameScopeChain(StringView &MangledName,
  1386. IdentifierNode *UnqualifiedName) {
  1387. NodeList *Head = Arena.alloc<NodeList>();
  1388. Head->N = UnqualifiedName;
  1389. size_t Count = 1;
  1390. while (!MangledName.consumeFront("@")) {
  1391. ++Count;
  1392. NodeList *NewHead = Arena.alloc<NodeList>();
  1393. NewHead->Next = Head;
  1394. Head = NewHead;
  1395. if (MangledName.empty()) {
  1396. Error = true;
  1397. return nullptr;
  1398. }
  1399. assert(!Error);
  1400. IdentifierNode *Elem = demangleNameScopePiece(MangledName);
  1401. if (Error)
  1402. return nullptr;
  1403. Head->N = Elem;
  1404. }
  1405. QualifiedNameNode *QN = Arena.alloc<QualifiedNameNode>();
  1406. QN->Components = nodeListToNodeArray(Arena, Head, Count);
  1407. return QN;
  1408. }
  1409. FuncClass Demangler::demangleFunctionClass(StringView &MangledName) {
  1410. switch (MangledName.popFront()) {
  1411. case '9':
  1412. return FuncClass(FC_ExternC | FC_NoParameterList);
  1413. case 'A':
  1414. return FC_Private;
  1415. case 'B':
  1416. return FuncClass(FC_Private | FC_Far);
  1417. case 'C':
  1418. return FuncClass(FC_Private | FC_Static);
  1419. case 'D':
  1420. return FuncClass(FC_Private | FC_Static | FC_Far);
  1421. case 'E':
  1422. return FuncClass(FC_Private | FC_Virtual);
  1423. case 'F':
  1424. return FuncClass(FC_Private | FC_Virtual | FC_Far);
  1425. case 'G':
  1426. return FuncClass(FC_Private | FC_StaticThisAdjust);
  1427. case 'H':
  1428. return FuncClass(FC_Private | FC_StaticThisAdjust | FC_Far);
  1429. case 'I':
  1430. return FuncClass(FC_Protected);
  1431. case 'J':
  1432. return FuncClass(FC_Protected | FC_Far);
  1433. case 'K':
  1434. return FuncClass(FC_Protected | FC_Static);
  1435. case 'L':
  1436. return FuncClass(FC_Protected | FC_Static | FC_Far);
  1437. case 'M':
  1438. return FuncClass(FC_Protected | FC_Virtual);
  1439. case 'N':
  1440. return FuncClass(FC_Protected | FC_Virtual | FC_Far);
  1441. case 'O':
  1442. return FuncClass(FC_Protected | FC_Virtual | FC_StaticThisAdjust);
  1443. case 'P':
  1444. return FuncClass(FC_Protected | FC_Virtual | FC_StaticThisAdjust | FC_Far);
  1445. case 'Q':
  1446. return FuncClass(FC_Public);
  1447. case 'R':
  1448. return FuncClass(FC_Public | FC_Far);
  1449. case 'S':
  1450. return FuncClass(FC_Public | FC_Static);
  1451. case 'T':
  1452. return FuncClass(FC_Public | FC_Static | FC_Far);
  1453. case 'U':
  1454. return FuncClass(FC_Public | FC_Virtual);
  1455. case 'V':
  1456. return FuncClass(FC_Public | FC_Virtual | FC_Far);
  1457. case 'W':
  1458. return FuncClass(FC_Public | FC_Virtual | FC_StaticThisAdjust);
  1459. case 'X':
  1460. return FuncClass(FC_Public | FC_Virtual | FC_StaticThisAdjust | FC_Far);
  1461. case 'Y':
  1462. return FuncClass(FC_Global);
  1463. case 'Z':
  1464. return FuncClass(FC_Global | FC_Far);
  1465. case '$': {
  1466. FuncClass VFlag = FC_VirtualThisAdjust;
  1467. if (MangledName.consumeFront('R'))
  1468. VFlag = FuncClass(VFlag | FC_VirtualThisAdjustEx);
  1469. if (MangledName.empty())
  1470. break;
  1471. switch (MangledName.popFront()) {
  1472. case '0':
  1473. return FuncClass(FC_Private | FC_Virtual | VFlag);
  1474. case '1':
  1475. return FuncClass(FC_Private | FC_Virtual | VFlag | FC_Far);
  1476. case '2':
  1477. return FuncClass(FC_Protected | FC_Virtual | VFlag);
  1478. case '3':
  1479. return FuncClass(FC_Protected | FC_Virtual | VFlag | FC_Far);
  1480. case '4':
  1481. return FuncClass(FC_Public | FC_Virtual | VFlag);
  1482. case '5':
  1483. return FuncClass(FC_Public | FC_Virtual | VFlag | FC_Far);
  1484. }
  1485. }
  1486. }
  1487. Error = true;
  1488. return FC_Public;
  1489. }
  1490. CallingConv Demangler::demangleCallingConvention(StringView &MangledName) {
  1491. if (MangledName.empty()) {
  1492. Error = true;
  1493. return CallingConv::None;
  1494. }
  1495. switch (MangledName.popFront()) {
  1496. case 'A':
  1497. case 'B':
  1498. return CallingConv::Cdecl;
  1499. case 'C':
  1500. case 'D':
  1501. return CallingConv::Pascal;
  1502. case 'E':
  1503. case 'F':
  1504. return CallingConv::Thiscall;
  1505. case 'G':
  1506. case 'H':
  1507. return CallingConv::Stdcall;
  1508. case 'I':
  1509. case 'J':
  1510. return CallingConv::Fastcall;
  1511. case 'M':
  1512. case 'N':
  1513. return CallingConv::Clrcall;
  1514. case 'O':
  1515. case 'P':
  1516. return CallingConv::Eabi;
  1517. case 'Q':
  1518. return CallingConv::Vectorcall;
  1519. }
  1520. return CallingConv::None;
  1521. }
  1522. StorageClass Demangler::demangleVariableStorageClass(StringView &MangledName) {
  1523. assert(MangledName.front() >= '0' && MangledName.front() <= '4');
  1524. switch (MangledName.popFront()) {
  1525. case '0':
  1526. return StorageClass::PrivateStatic;
  1527. case '1':
  1528. return StorageClass::ProtectedStatic;
  1529. case '2':
  1530. return StorageClass::PublicStatic;
  1531. case '3':
  1532. return StorageClass::Global;
  1533. case '4':
  1534. return StorageClass::FunctionLocalStatic;
  1535. }
  1536. DEMANGLE_UNREACHABLE;
  1537. }
  1538. std::pair<Qualifiers, bool>
  1539. Demangler::demangleQualifiers(StringView &MangledName) {
  1540. if (MangledName.empty()) {
  1541. Error = true;
  1542. return std::make_pair(Q_None, false);
  1543. }
  1544. switch (MangledName.popFront()) {
  1545. // Member qualifiers
  1546. case 'Q':
  1547. return std::make_pair(Q_None, true);
  1548. case 'R':
  1549. return std::make_pair(Q_Const, true);
  1550. case 'S':
  1551. return std::make_pair(Q_Volatile, true);
  1552. case 'T':
  1553. return std::make_pair(Qualifiers(Q_Const | Q_Volatile), true);
  1554. // Non-Member qualifiers
  1555. case 'A':
  1556. return std::make_pair(Q_None, false);
  1557. case 'B':
  1558. return std::make_pair(Q_Const, false);
  1559. case 'C':
  1560. return std::make_pair(Q_Volatile, false);
  1561. case 'D':
  1562. return std::make_pair(Qualifiers(Q_Const | Q_Volatile), false);
  1563. }
  1564. Error = true;
  1565. return std::make_pair(Q_None, false);
  1566. }
  1567. // <variable-type> ::= <type> <cvr-qualifiers>
  1568. // ::= <type> <pointee-cvr-qualifiers> # pointers, references
  1569. TypeNode *Demangler::demangleType(StringView &MangledName,
  1570. QualifierMangleMode QMM) {
  1571. Qualifiers Quals = Q_None;
  1572. bool IsMember = false;
  1573. if (QMM == QualifierMangleMode::Mangle) {
  1574. std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
  1575. } else if (QMM == QualifierMangleMode::Result) {
  1576. if (MangledName.consumeFront('?'))
  1577. std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
  1578. }
  1579. if (MangledName.empty()) {
  1580. Error = true;
  1581. return nullptr;
  1582. }
  1583. TypeNode *Ty = nullptr;
  1584. if (isTagType(MangledName))
  1585. Ty = demangleClassType(MangledName);
  1586. else if (isPointerType(MangledName)) {
  1587. if (isMemberPointer(MangledName, Error))
  1588. Ty = demangleMemberPointerType(MangledName);
  1589. else if (!Error)
  1590. Ty = demanglePointerType(MangledName);
  1591. else
  1592. return nullptr;
  1593. } else if (isArrayType(MangledName))
  1594. Ty = demangleArrayType(MangledName);
  1595. else if (isFunctionType(MangledName)) {
  1596. if (MangledName.consumeFront("$$A8@@"))
  1597. Ty = demangleFunctionType(MangledName, true);
  1598. else {
  1599. assert(MangledName.startsWith("$$A6"));
  1600. MangledName.consumeFront("$$A6");
  1601. Ty = demangleFunctionType(MangledName, false);
  1602. }
  1603. } else if (isCustomType(MangledName)) {
  1604. Ty = demangleCustomType(MangledName);
  1605. } else {
  1606. Ty = demanglePrimitiveType(MangledName);
  1607. }
  1608. if (!Ty || Error)
  1609. return Ty;
  1610. Ty->Quals = Qualifiers(Ty->Quals | Quals);
  1611. return Ty;
  1612. }
  1613. bool Demangler::demangleThrowSpecification(StringView &MangledName) {
  1614. if (MangledName.consumeFront("_E"))
  1615. return true;
  1616. if (MangledName.consumeFront('Z'))
  1617. return false;
  1618. Error = true;
  1619. return false;
  1620. }
  1621. FunctionSignatureNode *Demangler::demangleFunctionType(StringView &MangledName,
  1622. bool HasThisQuals) {
  1623. FunctionSignatureNode *FTy = Arena.alloc<FunctionSignatureNode>();
  1624. if (HasThisQuals) {
  1625. FTy->Quals = demanglePointerExtQualifiers(MangledName);
  1626. FTy->RefQualifier = demangleFunctionRefQualifier(MangledName);
  1627. FTy->Quals = Qualifiers(FTy->Quals | demangleQualifiers(MangledName).first);
  1628. }
  1629. // Fields that appear on both member and non-member functions.
  1630. FTy->CallConvention = demangleCallingConvention(MangledName);
  1631. // <return-type> ::= <type>
  1632. // ::= @ # structors (they have no declared return type)
  1633. bool IsStructor = MangledName.consumeFront('@');
  1634. if (!IsStructor)
  1635. FTy->ReturnType = demangleType(MangledName, QualifierMangleMode::Result);
  1636. FTy->Params = demangleFunctionParameterList(MangledName, FTy->IsVariadic);
  1637. FTy->IsNoexcept = demangleThrowSpecification(MangledName);
  1638. return FTy;
  1639. }
  1640. FunctionSymbolNode *
  1641. Demangler::demangleFunctionEncoding(StringView &MangledName) {
  1642. FuncClass ExtraFlags = FC_None;
  1643. if (MangledName.consumeFront("$$J0"))
  1644. ExtraFlags = FC_ExternC;
  1645. if (MangledName.empty()) {
  1646. Error = true;
  1647. return nullptr;
  1648. }
  1649. FuncClass FC = demangleFunctionClass(MangledName);
  1650. FC = FuncClass(ExtraFlags | FC);
  1651. FunctionSignatureNode *FSN = nullptr;
  1652. ThunkSignatureNode *TTN = nullptr;
  1653. if (FC & FC_StaticThisAdjust) {
  1654. TTN = Arena.alloc<ThunkSignatureNode>();
  1655. TTN->ThisAdjust.StaticOffset = demangleSigned(MangledName);
  1656. } else if (FC & FC_VirtualThisAdjust) {
  1657. TTN = Arena.alloc<ThunkSignatureNode>();
  1658. if (FC & FC_VirtualThisAdjustEx) {
  1659. TTN->ThisAdjust.VBPtrOffset = demangleSigned(MangledName);
  1660. TTN->ThisAdjust.VBOffsetOffset = demangleSigned(MangledName);
  1661. }
  1662. TTN->ThisAdjust.VtordispOffset = demangleSigned(MangledName);
  1663. TTN->ThisAdjust.StaticOffset = demangleSigned(MangledName);
  1664. }
  1665. if (FC & FC_NoParameterList) {
  1666. // This is an extern "C" function whose full signature hasn't been mangled.
  1667. // This happens when we need to mangle a local symbol inside of an extern
  1668. // "C" function.
  1669. FSN = Arena.alloc<FunctionSignatureNode>();
  1670. } else {
  1671. bool HasThisQuals = !(FC & (FC_Global | FC_Static));
  1672. FSN = demangleFunctionType(MangledName, HasThisQuals);
  1673. }
  1674. if (Error)
  1675. return nullptr;
  1676. if (TTN) {
  1677. *static_cast<FunctionSignatureNode *>(TTN) = *FSN;
  1678. FSN = TTN;
  1679. }
  1680. FSN->FunctionClass = FC;
  1681. FunctionSymbolNode *Symbol = Arena.alloc<FunctionSymbolNode>();
  1682. Symbol->Signature = FSN;
  1683. return Symbol;
  1684. }
  1685. CustomTypeNode *Demangler::demangleCustomType(StringView &MangledName) {
  1686. assert(MangledName.startsWith('?'));
  1687. MangledName.popFront();
  1688. CustomTypeNode *CTN = Arena.alloc<CustomTypeNode>();
  1689. CTN->Identifier = demangleUnqualifiedTypeName(MangledName, /*Memorize=*/true);
  1690. if (!MangledName.consumeFront('@'))
  1691. Error = true;
  1692. if (Error)
  1693. return nullptr;
  1694. return CTN;
  1695. }
  1696. // Reads a primitive type.
  1697. PrimitiveTypeNode *Demangler::demanglePrimitiveType(StringView &MangledName) {
  1698. if (MangledName.consumeFront("$$T"))
  1699. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Nullptr);
  1700. switch (MangledName.popFront()) {
  1701. case 'X':
  1702. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Void);
  1703. case 'D':
  1704. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Char);
  1705. case 'C':
  1706. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Schar);
  1707. case 'E':
  1708. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Uchar);
  1709. case 'F':
  1710. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Short);
  1711. case 'G':
  1712. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Ushort);
  1713. case 'H':
  1714. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Int);
  1715. case 'I':
  1716. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Uint);
  1717. case 'J':
  1718. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Long);
  1719. case 'K':
  1720. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Ulong);
  1721. case 'M':
  1722. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Float);
  1723. case 'N':
  1724. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Double);
  1725. case 'O':
  1726. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Ldouble);
  1727. case '_': {
  1728. if (MangledName.empty()) {
  1729. Error = true;
  1730. return nullptr;
  1731. }
  1732. switch (MangledName.popFront()) {
  1733. case 'N':
  1734. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Bool);
  1735. case 'J':
  1736. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Int64);
  1737. case 'K':
  1738. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Uint64);
  1739. case 'W':
  1740. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Wchar);
  1741. case 'Q':
  1742. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Char8);
  1743. case 'S':
  1744. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Char16);
  1745. case 'U':
  1746. return Arena.alloc<PrimitiveTypeNode>(PrimitiveKind::Char32);
  1747. }
  1748. break;
  1749. }
  1750. }
  1751. Error = true;
  1752. return nullptr;
  1753. }
  1754. TagTypeNode *Demangler::demangleClassType(StringView &MangledName) {
  1755. TagTypeNode *TT = nullptr;
  1756. switch (MangledName.popFront()) {
  1757. case 'T':
  1758. TT = Arena.alloc<TagTypeNode>(TagKind::Union);
  1759. break;
  1760. case 'U':
  1761. TT = Arena.alloc<TagTypeNode>(TagKind::Struct);
  1762. break;
  1763. case 'V':
  1764. TT = Arena.alloc<TagTypeNode>(TagKind::Class);
  1765. break;
  1766. case 'W':
  1767. if (!MangledName.consumeFront('4')) {
  1768. Error = true;
  1769. return nullptr;
  1770. }
  1771. TT = Arena.alloc<TagTypeNode>(TagKind::Enum);
  1772. break;
  1773. default:
  1774. assert(false);
  1775. }
  1776. TT->QualifiedName = demangleFullyQualifiedTypeName(MangledName);
  1777. return TT;
  1778. }
  1779. // <pointer-type> ::= E? <pointer-cvr-qualifiers> <ext-qualifiers> <type>
  1780. // # the E is required for 64-bit non-static pointers
  1781. PointerTypeNode *Demangler::demanglePointerType(StringView &MangledName) {
  1782. PointerTypeNode *Pointer = Arena.alloc<PointerTypeNode>();
  1783. std::tie(Pointer->Quals, Pointer->Affinity) =
  1784. demanglePointerCVQualifiers(MangledName);
  1785. if (MangledName.consumeFront("6")) {
  1786. Pointer->Pointee = demangleFunctionType(MangledName, false);
  1787. return Pointer;
  1788. }
  1789. Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
  1790. Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
  1791. Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Mangle);
  1792. return Pointer;
  1793. }
  1794. PointerTypeNode *Demangler::demangleMemberPointerType(StringView &MangledName) {
  1795. PointerTypeNode *Pointer = Arena.alloc<PointerTypeNode>();
  1796. std::tie(Pointer->Quals, Pointer->Affinity) =
  1797. demanglePointerCVQualifiers(MangledName);
  1798. assert(Pointer->Affinity == PointerAffinity::Pointer);
  1799. Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
  1800. Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
  1801. // isMemberPointer() only returns true if there is at least one character
  1802. // after the qualifiers.
  1803. if (MangledName.consumeFront("8")) {
  1804. Pointer->ClassParent = demangleFullyQualifiedTypeName(MangledName);
  1805. Pointer->Pointee = demangleFunctionType(MangledName, true);
  1806. } else {
  1807. Qualifiers PointeeQuals = Q_None;
  1808. bool IsMember = false;
  1809. std::tie(PointeeQuals, IsMember) = demangleQualifiers(MangledName);
  1810. assert(IsMember || Error);
  1811. Pointer->ClassParent = demangleFullyQualifiedTypeName(MangledName);
  1812. Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Drop);
  1813. if (Pointer->Pointee)
  1814. Pointer->Pointee->Quals = PointeeQuals;
  1815. }
  1816. return Pointer;
  1817. }
  1818. Qualifiers Demangler::demanglePointerExtQualifiers(StringView &MangledName) {
  1819. Qualifiers Quals = Q_None;
  1820. if (MangledName.consumeFront('E'))
  1821. Quals = Qualifiers(Quals | Q_Pointer64);
  1822. if (MangledName.consumeFront('I'))
  1823. Quals = Qualifiers(Quals | Q_Restrict);
  1824. if (MangledName.consumeFront('F'))
  1825. Quals = Qualifiers(Quals | Q_Unaligned);
  1826. return Quals;
  1827. }
  1828. ArrayTypeNode *Demangler::demangleArrayType(StringView &MangledName) {
  1829. assert(MangledName.front() == 'Y');
  1830. MangledName.popFront();
  1831. uint64_t Rank = 0;
  1832. bool IsNegative = false;
  1833. std::tie(Rank, IsNegative) = demangleNumber(MangledName);
  1834. if (IsNegative || Rank == 0) {
  1835. Error = true;
  1836. return nullptr;
  1837. }
  1838. ArrayTypeNode *ATy = Arena.alloc<ArrayTypeNode>();
  1839. NodeList *Head = Arena.alloc<NodeList>();
  1840. NodeList *Tail = Head;
  1841. for (uint64_t I = 0; I < Rank; ++I) {
  1842. uint64_t D = 0;
  1843. std::tie(D, IsNegative) = demangleNumber(MangledName);
  1844. if (Error || IsNegative) {
  1845. Error = true;
  1846. return nullptr;
  1847. }
  1848. Tail->N = Arena.alloc<IntegerLiteralNode>(D, IsNegative);
  1849. if (I + 1 < Rank) {
  1850. Tail->Next = Arena.alloc<NodeList>();
  1851. Tail = Tail->Next;
  1852. }
  1853. }
  1854. ATy->Dimensions = nodeListToNodeArray(Arena, Head, Rank);
  1855. if (MangledName.consumeFront("$$C")) {
  1856. bool IsMember = false;
  1857. std::tie(ATy->Quals, IsMember) = demangleQualifiers(MangledName);
  1858. if (IsMember) {
  1859. Error = true;
  1860. return nullptr;
  1861. }
  1862. }
  1863. ATy->ElementType = demangleType(MangledName, QualifierMangleMode::Drop);
  1864. return ATy;
  1865. }
  1866. // Reads a function's parameters.
  1867. NodeArrayNode *Demangler::demangleFunctionParameterList(StringView &MangledName,
  1868. bool &IsVariadic) {
  1869. // Empty parameter list.
  1870. if (MangledName.consumeFront('X'))
  1871. return nullptr;
  1872. NodeList *Head = Arena.alloc<NodeList>();
  1873. NodeList **Current = &Head;
  1874. size_t Count = 0;
  1875. while (!Error && !MangledName.startsWith('@') &&
  1876. !MangledName.startsWith('Z')) {
  1877. ++Count;
  1878. if (startsWithDigit(MangledName)) {
  1879. size_t N = MangledName[0] - '0';
  1880. if (N >= Backrefs.FunctionParamCount) {
  1881. Error = true;
  1882. return nullptr;
  1883. }
  1884. MangledName = MangledName.dropFront();
  1885. *Current = Arena.alloc<NodeList>();
  1886. (*Current)->N = Backrefs.FunctionParams[N];
  1887. Current = &(*Current)->Next;
  1888. continue;
  1889. }
  1890. size_t OldSize = MangledName.size();
  1891. *Current = Arena.alloc<NodeList>();
  1892. TypeNode *TN = demangleType(MangledName, QualifierMangleMode::Drop);
  1893. if (!TN || Error)
  1894. return nullptr;
  1895. (*Current)->N = TN;
  1896. size_t CharsConsumed = OldSize - MangledName.size();
  1897. assert(CharsConsumed != 0);
  1898. // Single-letter types are ignored for backreferences because memorizing
  1899. // them doesn't save anything.
  1900. if (Backrefs.FunctionParamCount <= 9 && CharsConsumed > 1)
  1901. Backrefs.FunctionParams[Backrefs.FunctionParamCount++] = TN;
  1902. Current = &(*Current)->Next;
  1903. }
  1904. if (Error)
  1905. return nullptr;
  1906. NodeArrayNode *NA = nodeListToNodeArray(Arena, Head, Count);
  1907. // A non-empty parameter list is terminated by either 'Z' (variadic) parameter
  1908. // list or '@' (non variadic). Careful not to consume "@Z", as in that case
  1909. // the following Z could be a throw specifier.
  1910. if (MangledName.consumeFront('@'))
  1911. return NA;
  1912. if (MangledName.consumeFront('Z')) {
  1913. IsVariadic = true;
  1914. return NA;
  1915. }
  1916. DEMANGLE_UNREACHABLE;
  1917. }
  1918. NodeArrayNode *
  1919. Demangler::demangleTemplateParameterList(StringView &MangledName) {
  1920. NodeList *Head = nullptr;
  1921. NodeList **Current = &Head;
  1922. size_t Count = 0;
  1923. while (!MangledName.startsWith('@')) {
  1924. if (MangledName.consumeFront("$S") || MangledName.consumeFront("$$V") ||
  1925. MangledName.consumeFront("$$$V") || MangledName.consumeFront("$$Z")) {
  1926. // parameter pack separator
  1927. continue;
  1928. }
  1929. ++Count;
  1930. // Template parameter lists don't participate in back-referencing.
  1931. *Current = Arena.alloc<NodeList>();
  1932. NodeList &TP = **Current;
  1933. TemplateParameterReferenceNode *TPRN = nullptr;
  1934. if (MangledName.consumeFront("$$Y")) {
  1935. // Template alias
  1936. TP.N = demangleFullyQualifiedTypeName(MangledName);
  1937. } else if (MangledName.consumeFront("$$B")) {
  1938. // Array
  1939. TP.N = demangleType(MangledName, QualifierMangleMode::Drop);
  1940. } else if (MangledName.consumeFront("$$C")) {
  1941. // Type has qualifiers.
  1942. TP.N = demangleType(MangledName, QualifierMangleMode::Mangle);
  1943. } else if (MangledName.startsWith("$1") || MangledName.startsWith("$H") ||
  1944. MangledName.startsWith("$I") || MangledName.startsWith("$J")) {
  1945. // Pointer to member
  1946. TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>();
  1947. TPRN->IsMemberPointer = true;
  1948. MangledName = MangledName.dropFront();
  1949. // 1 - single inheritance <name>
  1950. // H - multiple inheritance <name> <number>
  1951. // I - virtual inheritance <name> <number> <number>
  1952. // J - unspecified inheritance <name> <number> <number> <number>
  1953. char InheritanceSpecifier = MangledName.popFront();
  1954. SymbolNode *S = nullptr;
  1955. if (MangledName.startsWith('?')) {
  1956. S = parse(MangledName);
  1957. if (Error || !S->Name) {
  1958. Error = true;
  1959. return nullptr;
  1960. }
  1961. memorizeIdentifier(S->Name->getUnqualifiedIdentifier());
  1962. }
  1963. switch (InheritanceSpecifier) {
  1964. case 'J':
  1965. TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
  1966. demangleSigned(MangledName);
  1967. DEMANGLE_FALLTHROUGH;
  1968. case 'I':
  1969. TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
  1970. demangleSigned(MangledName);
  1971. DEMANGLE_FALLTHROUGH;
  1972. case 'H':
  1973. TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
  1974. demangleSigned(MangledName);
  1975. DEMANGLE_FALLTHROUGH;
  1976. case '1':
  1977. break;
  1978. default:
  1979. DEMANGLE_UNREACHABLE;
  1980. }
  1981. TPRN->Affinity = PointerAffinity::Pointer;
  1982. TPRN->Symbol = S;
  1983. } else if (MangledName.startsWith("$E?")) {
  1984. MangledName.consumeFront("$E");
  1985. // Reference to symbol
  1986. TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>();
  1987. TPRN->Symbol = parse(MangledName);
  1988. TPRN->Affinity = PointerAffinity::Reference;
  1989. } else if (MangledName.startsWith("$F") || MangledName.startsWith("$G")) {
  1990. TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>();
  1991. // Data member pointer.
  1992. MangledName = MangledName.dropFront();
  1993. char InheritanceSpecifier = MangledName.popFront();
  1994. switch (InheritanceSpecifier) {
  1995. case 'G':
  1996. TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
  1997. demangleSigned(MangledName);
  1998. DEMANGLE_FALLTHROUGH;
  1999. case 'F':
  2000. TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
  2001. demangleSigned(MangledName);
  2002. TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
  2003. demangleSigned(MangledName);
  2004. break;
  2005. default:
  2006. DEMANGLE_UNREACHABLE;
  2007. }
  2008. TPRN->IsMemberPointer = true;
  2009. } else if (MangledName.consumeFront("$0")) {
  2010. // Integral non-type template parameter
  2011. bool IsNegative = false;
  2012. uint64_t Value = 0;
  2013. std::tie(Value, IsNegative) = demangleNumber(MangledName);
  2014. TP.N = Arena.alloc<IntegerLiteralNode>(Value, IsNegative);
  2015. } else {
  2016. TP.N = demangleType(MangledName, QualifierMangleMode::Drop);
  2017. }
  2018. if (Error)
  2019. return nullptr;
  2020. Current = &TP.Next;
  2021. }
  2022. // The loop above returns nullptr on Error.
  2023. assert(!Error);
  2024. // Template parameter lists cannot be variadic, so it can only be terminated
  2025. // by @ (as opposed to 'Z' in the function parameter case).
  2026. assert(MangledName.startsWith('@')); // The above loop exits only on '@'.
  2027. MangledName.consumeFront('@');
  2028. return nodeListToNodeArray(Arena, Head, Count);
  2029. }
  2030. void Demangler::dumpBackReferences() {
  2031. std::printf("%d function parameter backreferences\n",
  2032. (int)Backrefs.FunctionParamCount);
  2033. // Create an output stream so we can render each type.
  2034. OutputStream OS;
  2035. if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
  2036. std::terminate();
  2037. for (size_t I = 0; I < Backrefs.FunctionParamCount; ++I) {
  2038. OS.setCurrentPosition(0);
  2039. TypeNode *T = Backrefs.FunctionParams[I];
  2040. T->output(OS, OF_Default);
  2041. std::printf(" [%d] - %.*s\n", (int)I, (int)OS.getCurrentPosition(),
  2042. OS.getBuffer());
  2043. }
  2044. std::free(OS.getBuffer());
  2045. if (Backrefs.FunctionParamCount > 0)
  2046. std::printf("\n");
  2047. std::printf("%d name backreferences\n", (int)Backrefs.NamesCount);
  2048. for (size_t I = 0; I < Backrefs.NamesCount; ++I) {
  2049. std::printf(" [%d] - %.*s\n", (int)I, (int)Backrefs.Names[I]->Name.size(),
  2050. Backrefs.Names[I]->Name.begin());
  2051. }
  2052. if (Backrefs.NamesCount > 0)
  2053. std::printf("\n");
  2054. }
  2055. char *llvm::microsoftDemangle(const char *MangledName, size_t *NMangled,
  2056. char *Buf, size_t *N,
  2057. int *Status, MSDemangleFlags Flags) {
  2058. Demangler D;
  2059. OutputStream S;
  2060. StringView Name{MangledName};
  2061. SymbolNode *AST = D.parse(Name);
  2062. if (!D.Error && NMangled)
  2063. *NMangled = Name.begin() - MangledName;
  2064. if (Flags & MSDF_DumpBackrefs)
  2065. D.dumpBackReferences();
  2066. OutputFlags OF = OF_Default;
  2067. if (Flags & MSDF_NoCallingConvention)
  2068. OF = OutputFlags(OF | OF_NoCallingConvention);
  2069. if (Flags & MSDF_NoAccessSpecifier)
  2070. OF = OutputFlags(OF | OF_NoAccessSpecifier);
  2071. if (Flags & MSDF_NoReturnType)
  2072. OF = OutputFlags(OF | OF_NoReturnType);
  2073. if (Flags & MSDF_NoMemberType)
  2074. OF = OutputFlags(OF | OF_NoMemberType);
  2075. int InternalStatus = demangle_success;
  2076. if (D.Error)
  2077. InternalStatus = demangle_invalid_mangled_name;
  2078. else if (!initializeOutputStream(Buf, N, S, 1024))
  2079. InternalStatus = demangle_memory_alloc_failure;
  2080. else {
  2081. AST->output(S, OF);
  2082. S += '\0';
  2083. if (N != nullptr)
  2084. *N = S.getCurrentPosition();
  2085. Buf = S.getBuffer();
  2086. }
  2087. if (Status)
  2088. *Status = InternalStatus;
  2089. return InternalStatus == demangle_success ? Buf : nullptr;
  2090. }