MicrosoftDemangle.cpp 75 KB

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