Brak opisu

ast.go 53KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package demangle
  5. import (
  6. "bytes"
  7. "fmt"
  8. "strings"
  9. )
  10. // AST is an abstract syntax tree representing a C++ declaration.
  11. // This is sufficient for the demangler but is by no means a general C++ AST.
  12. type AST interface {
  13. // Internal method to convert to demangled string.
  14. print(*printState)
  15. // Traverse each element of an AST. If the function returns
  16. // false, traversal of children of that element is skipped.
  17. Traverse(func(AST) bool)
  18. // Copy an AST with possible transformations.
  19. // If the skip function returns true, no copy is required.
  20. // If the copy function returns nil, no copy is required.
  21. // Otherwise the AST returned by copy is used in a copy of the full AST.
  22. // Copy itself returns either a copy or nil.
  23. Copy(copy func(AST) AST, skip func(AST) bool) AST
  24. // Implement the fmt.GoStringer interface.
  25. GoString() string
  26. goString(indent int, field string) string
  27. }
  28. // ASTToString returns the demangled name of the AST.
  29. func ASTToString(a AST, options ...Option) string {
  30. tparams := true
  31. for _, o := range options {
  32. switch o {
  33. case NoTemplateParams:
  34. tparams = false
  35. }
  36. }
  37. ps := printState{tparams: tparams}
  38. a.print(&ps)
  39. return ps.buf.String()
  40. }
  41. // The printState type holds information needed to print an AST.
  42. type printState struct {
  43. tparams bool // whether to print template parameters
  44. buf bytes.Buffer
  45. last byte // Last byte written to buffer.
  46. // The inner field is a list of items to print for a type
  47. // name. This is used by types to implement the inside-out
  48. // C++ declaration syntax.
  49. inner []AST
  50. }
  51. // writeByte adds a byte to the string being printed.
  52. func (ps *printState) writeByte(b byte) {
  53. ps.last = b
  54. ps.buf.WriteByte(b)
  55. }
  56. // writeString adds a string to the string being printed.
  57. func (ps *printState) writeString(s string) {
  58. if len(s) > 0 {
  59. ps.last = s[len(s)-1]
  60. }
  61. ps.buf.WriteString(s)
  62. }
  63. // Name is an unqualified name.
  64. type Name struct {
  65. Name string
  66. }
  67. func (n *Name) print(ps *printState) {
  68. ps.writeString(n.Name)
  69. }
  70. func (n *Name) Traverse(fn func(AST) bool) {
  71. fn(n)
  72. }
  73. func (n *Name) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  74. if skip(n) {
  75. return nil
  76. }
  77. return fn(n)
  78. }
  79. func (n *Name) GoString() string {
  80. return n.goString(0, "Name: ")
  81. }
  82. func (n *Name) goString(indent int, field string) string {
  83. return fmt.Sprintf("%*s%s%s", indent, "", field, n.Name)
  84. }
  85. // Typed is a typed name.
  86. type Typed struct {
  87. Name AST
  88. Type AST
  89. }
  90. func (t *Typed) print(ps *printState) {
  91. // We are printing a typed name, so ignore the current set of
  92. // inner names to print. Pass down our name as the one to use.
  93. holdInner := ps.inner
  94. defer func() { ps.inner = holdInner }()
  95. ps.inner = []AST{t}
  96. t.Type.print(ps)
  97. if len(ps.inner) > 0 {
  98. // The type did not print the name; print it now in
  99. // the default location.
  100. ps.writeByte(' ')
  101. t.Name.print(ps)
  102. }
  103. }
  104. func (t *Typed) printInner(ps *printState) {
  105. t.Name.print(ps)
  106. }
  107. func (t *Typed) Traverse(fn func(AST) bool) {
  108. if fn(t) {
  109. t.Name.Traverse(fn)
  110. t.Type.Traverse(fn)
  111. }
  112. }
  113. func (t *Typed) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  114. if skip(t) {
  115. return nil
  116. }
  117. name := t.Name.Copy(fn, skip)
  118. typ := t.Type.Copy(fn, skip)
  119. if name == nil && typ == nil {
  120. return fn(t)
  121. }
  122. if name == nil {
  123. name = t.Name
  124. }
  125. if typ == nil {
  126. typ = t.Type
  127. }
  128. t = &Typed{Name: name, Type: typ}
  129. if r := fn(t); r != nil {
  130. return r
  131. }
  132. return t
  133. }
  134. func (t *Typed) GoString() string {
  135. return t.goString(0, "")
  136. }
  137. func (t *Typed) goString(indent int, field string) string {
  138. return fmt.Sprintf("%*s%sTyped:\n%s\n%s", indent, "", field,
  139. t.Name.goString(indent+2, "Name: "),
  140. t.Type.goString(indent+2, "Type: "))
  141. }
  142. // Qualified is a name in a scope.
  143. type Qualified struct {
  144. Scope AST
  145. Name AST
  146. // The LocalName field is true if this is parsed as a
  147. // <local-name>. We shouldn't really need this, but in some
  148. // cases (for the unary sizeof operator) the standard
  149. // demangler prints a local name slightly differently. We
  150. // keep track of this for compatibility.
  151. LocalName bool // A full local name encoding
  152. }
  153. func (q *Qualified) print(ps *printState) {
  154. q.Scope.print(ps)
  155. ps.writeString("::")
  156. q.Name.print(ps)
  157. }
  158. func (q *Qualified) Traverse(fn func(AST) bool) {
  159. if fn(q) {
  160. q.Scope.Traverse(fn)
  161. q.Name.Traverse(fn)
  162. }
  163. }
  164. func (q *Qualified) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  165. if skip(q) {
  166. return nil
  167. }
  168. scope := q.Scope.Copy(fn, skip)
  169. name := q.Name.Copy(fn, skip)
  170. if scope == nil && name == nil {
  171. return fn(q)
  172. }
  173. if scope == nil {
  174. scope = q.Scope
  175. }
  176. if name == nil {
  177. name = q.Name
  178. }
  179. q = &Qualified{Scope: scope, Name: name, LocalName: q.LocalName}
  180. if r := fn(q); r != nil {
  181. return r
  182. }
  183. return q
  184. }
  185. func (q *Qualified) GoString() string {
  186. return q.goString(0, "")
  187. }
  188. func (q *Qualified) goString(indent int, field string) string {
  189. s := ""
  190. if q.LocalName {
  191. s = " LocalName: true"
  192. }
  193. return fmt.Sprintf("%*s%sQualified:%s\n%s\n%s", indent, "", field,
  194. s, q.Scope.goString(indent+2, "Scope: "),
  195. q.Name.goString(indent+2, "Name: "))
  196. }
  197. // Template is a template with arguments.
  198. type Template struct {
  199. Name AST
  200. Args []AST
  201. }
  202. func (t *Template) print(ps *printState) {
  203. // Inner types apply to the template as a whole, they don't
  204. // cross over into the template.
  205. holdInner := ps.inner
  206. defer func() { ps.inner = holdInner }()
  207. ps.inner = nil
  208. t.Name.print(ps)
  209. if !ps.tparams {
  210. // Do not print template parameters.
  211. return
  212. }
  213. // We need an extra space after operator<.
  214. if ps.last == '<' {
  215. ps.writeByte(' ')
  216. }
  217. ps.writeByte('<')
  218. first := true
  219. for _, a := range t.Args {
  220. if ps.isEmpty(a) {
  221. continue
  222. }
  223. if !first {
  224. ps.writeString(", ")
  225. }
  226. a.print(ps)
  227. first = false
  228. }
  229. if ps.last == '>' {
  230. // Avoid syntactic ambiguity in old versions of C++.
  231. ps.writeByte(' ')
  232. }
  233. ps.writeByte('>')
  234. }
  235. func (t *Template) Traverse(fn func(AST) bool) {
  236. if fn(t) {
  237. t.Name.Traverse(fn)
  238. for _, a := range t.Args {
  239. a.Traverse(fn)
  240. }
  241. }
  242. }
  243. func (t *Template) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  244. if skip(t) {
  245. return nil
  246. }
  247. name := t.Name.Copy(fn, skip)
  248. changed := name != nil
  249. args := make([]AST, len(t.Args))
  250. for i, a := range t.Args {
  251. ac := a.Copy(fn, skip)
  252. if ac == nil {
  253. args[i] = a
  254. } else {
  255. args[i] = ac
  256. changed = true
  257. }
  258. }
  259. if !changed {
  260. return fn(t)
  261. }
  262. if name == nil {
  263. name = t.Name
  264. }
  265. t = &Template{Name: name, Args: args}
  266. if r := fn(t); r != nil {
  267. return r
  268. }
  269. return t
  270. }
  271. func (t *Template) GoString() string {
  272. return t.goString(0, "")
  273. }
  274. func (t *Template) goString(indent int, field string) string {
  275. var args string
  276. if len(t.Args) == 0 {
  277. args = fmt.Sprintf("%*sArgs: nil", indent+2, "")
  278. } else {
  279. args = fmt.Sprintf("%*sArgs:", indent+2, "")
  280. for i, a := range t.Args {
  281. args += "\n"
  282. args += a.goString(indent+4, fmt.Sprintf("%d: ", i))
  283. }
  284. }
  285. return fmt.Sprintf("%*s%sTemplate (%p):\n%s\n%s", indent, "", field, t,
  286. t.Name.goString(indent+2, "Name: "), args)
  287. }
  288. // TemplateParam is a template parameter. The Template field is
  289. // filled in while parsing the demangled string. We don't normally
  290. // see these while printing--they are replaced by the simplify
  291. // function.
  292. type TemplateParam struct {
  293. Index int
  294. Template *Template
  295. }
  296. func (tp *TemplateParam) print(ps *printState) {
  297. if tp.Template == nil {
  298. panic("TemplateParam Template field is nil")
  299. }
  300. if tp.Index >= len(tp.Template.Args) {
  301. panic("TemplateParam Index out of bounds")
  302. }
  303. tp.Template.Args[tp.Index].print(ps)
  304. }
  305. func (tp *TemplateParam) Traverse(fn func(AST) bool) {
  306. fn(tp)
  307. // Don't traverse Template--it points elsewhere in the AST.
  308. }
  309. func (tp *TemplateParam) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  310. if skip(tp) {
  311. return nil
  312. }
  313. return fn(tp)
  314. }
  315. func (tp *TemplateParam) GoString() string {
  316. return tp.goString(0, "")
  317. }
  318. func (tp *TemplateParam) goString(indent int, field string) string {
  319. return fmt.Sprintf("%*s%sTemplateParam: Template: %p; Index %d", indent, "", field, tp.Template, tp.Index)
  320. }
  321. // Qualifiers is an ordered list of type qualifiers.
  322. type Qualifiers []string
  323. // TypeWithQualifiers is a type with standard qualifiers.
  324. type TypeWithQualifiers struct {
  325. Base AST
  326. Qualifiers Qualifiers
  327. }
  328. func (twq *TypeWithQualifiers) print(ps *printState) {
  329. // Give the base type a chance to print the inner types.
  330. ps.inner = append(ps.inner, twq)
  331. twq.Base.print(ps)
  332. if len(ps.inner) > 0 {
  333. // The qualifier wasn't printed by Base.
  334. ps.writeByte(' ')
  335. ps.writeString(strings.Join(twq.Qualifiers, " "))
  336. ps.inner = ps.inner[:len(ps.inner)-1]
  337. }
  338. }
  339. // Print qualifiers as an inner type by just printing the qualifiers.
  340. func (twq *TypeWithQualifiers) printInner(ps *printState) {
  341. ps.writeByte(' ')
  342. ps.writeString(strings.Join(twq.Qualifiers, " "))
  343. }
  344. func (twq *TypeWithQualifiers) Traverse(fn func(AST) bool) {
  345. if fn(twq) {
  346. twq.Base.Traverse(fn)
  347. }
  348. }
  349. func (twq *TypeWithQualifiers) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  350. if skip(twq) {
  351. return nil
  352. }
  353. base := twq.Base.Copy(fn, skip)
  354. if base == nil {
  355. return fn(twq)
  356. }
  357. twq = &TypeWithQualifiers{Base: base, Qualifiers: twq.Qualifiers}
  358. if r := fn(twq); r != nil {
  359. return r
  360. }
  361. return twq
  362. }
  363. func (twq *TypeWithQualifiers) GoString() string {
  364. return twq.goString(0, "")
  365. }
  366. func (twq *TypeWithQualifiers) goString(indent int, field string) string {
  367. return fmt.Sprintf("%*s%sTypeWithQualifiers: Qualifiers: %s\n%s", indent, "", field,
  368. twq.Qualifiers, twq.Base.goString(indent+2, "Base: "))
  369. }
  370. // MethodWithQualifiers is a method with qualifiers.
  371. type MethodWithQualifiers struct {
  372. Method AST
  373. Qualifiers Qualifiers
  374. RefQualifier string // "" or "&" or "&&"
  375. }
  376. func (mwq *MethodWithQualifiers) print(ps *printState) {
  377. // Give the base type a chance to print the inner types.
  378. ps.inner = append(ps.inner, mwq)
  379. mwq.Method.print(ps)
  380. if len(ps.inner) > 0 {
  381. if len(mwq.Qualifiers) > 0 {
  382. ps.writeByte(' ')
  383. ps.writeString(strings.Join(mwq.Qualifiers, " "))
  384. }
  385. if mwq.RefQualifier != "" {
  386. ps.writeByte(' ')
  387. ps.writeString(mwq.RefQualifier)
  388. }
  389. ps.inner = ps.inner[:len(ps.inner)-1]
  390. }
  391. }
  392. func (mwq *MethodWithQualifiers) printInner(ps *printState) {
  393. if len(mwq.Qualifiers) > 0 {
  394. ps.writeByte(' ')
  395. ps.writeString(strings.Join(mwq.Qualifiers, " "))
  396. }
  397. if mwq.RefQualifier != "" {
  398. ps.writeByte(' ')
  399. ps.writeString(mwq.RefQualifier)
  400. }
  401. }
  402. func (mwq *MethodWithQualifiers) Traverse(fn func(AST) bool) {
  403. if fn(mwq) {
  404. mwq.Method.Traverse(fn)
  405. }
  406. }
  407. func (mwq *MethodWithQualifiers) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  408. if skip(mwq) {
  409. return nil
  410. }
  411. method := mwq.Method.Copy(fn, skip)
  412. if method == nil {
  413. return fn(mwq)
  414. }
  415. mwq = &MethodWithQualifiers{Method: method, Qualifiers: mwq.Qualifiers, RefQualifier: mwq.RefQualifier}
  416. if r := fn(mwq); r != nil {
  417. return r
  418. }
  419. return mwq
  420. }
  421. func (mwq *MethodWithQualifiers) GoString() string {
  422. return mwq.goString(0, "")
  423. }
  424. func (mwq *MethodWithQualifiers) goString(indent int, field string) string {
  425. var q string
  426. if len(mwq.Qualifiers) > 0 {
  427. q += fmt.Sprintf(" Qualifiers: %v", mwq.Qualifiers)
  428. }
  429. if mwq.RefQualifier != "" {
  430. if q != "" {
  431. q += ";"
  432. }
  433. q += " RefQualifier: " + mwq.RefQualifier
  434. }
  435. return fmt.Sprintf("%*s%sMethodWithQualifiers:%s\n%s", indent, "", field,
  436. q, mwq.Method.goString(indent+2, "Method: "))
  437. }
  438. // BuiltinType is a builtin type, like "int".
  439. type BuiltinType struct {
  440. Name string
  441. }
  442. func (bt *BuiltinType) print(ps *printState) {
  443. ps.writeString(bt.Name)
  444. }
  445. func (bt *BuiltinType) Traverse(fn func(AST) bool) {
  446. fn(bt)
  447. }
  448. func (bt *BuiltinType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  449. if skip(bt) {
  450. return nil
  451. }
  452. return fn(bt)
  453. }
  454. func (bt *BuiltinType) GoString() string {
  455. return bt.goString(0, "")
  456. }
  457. func (bt *BuiltinType) goString(indent int, field string) string {
  458. return fmt.Sprintf("%*s%sBuiltinType: %s", indent, "", field, bt.Name)
  459. }
  460. // printBase is common print code for types that are printed with a
  461. // simple suffix.
  462. func printBase(ps *printState, qual, base AST) {
  463. ps.inner = append(ps.inner, qual)
  464. base.print(ps)
  465. if len(ps.inner) > 0 {
  466. qual.(innerPrinter).printInner(ps)
  467. ps.inner = ps.inner[:len(ps.inner)-1]
  468. }
  469. }
  470. // PointerType is a pointer type.
  471. type PointerType struct {
  472. Base AST
  473. }
  474. func (pt *PointerType) print(ps *printState) {
  475. printBase(ps, pt, pt.Base)
  476. }
  477. func (pt *PointerType) printInner(ps *printState) {
  478. ps.writeString("*")
  479. }
  480. func (pt *PointerType) Traverse(fn func(AST) bool) {
  481. if fn(pt) {
  482. pt.Base.Traverse(fn)
  483. }
  484. }
  485. func (pt *PointerType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  486. if skip(pt) {
  487. return nil
  488. }
  489. base := pt.Base.Copy(fn, skip)
  490. if base == nil {
  491. return fn(pt)
  492. }
  493. pt = &PointerType{Base: base}
  494. if r := fn(pt); r != nil {
  495. return r
  496. }
  497. return pt
  498. }
  499. func (pt *PointerType) GoString() string {
  500. return pt.goString(0, "")
  501. }
  502. func (pt *PointerType) goString(indent int, field string) string {
  503. return fmt.Sprintf("%*s%sPointerType:\n%s", indent, "", field,
  504. pt.Base.goString(indent+2, ""))
  505. }
  506. // ReferenceType is a reference type.
  507. type ReferenceType struct {
  508. Base AST
  509. }
  510. func (rt *ReferenceType) print(ps *printState) {
  511. printBase(ps, rt, rt.Base)
  512. }
  513. func (rt *ReferenceType) printInner(ps *printState) {
  514. ps.writeString("&")
  515. }
  516. func (rt *ReferenceType) Traverse(fn func(AST) bool) {
  517. if fn(rt) {
  518. rt.Base.Traverse(fn)
  519. }
  520. }
  521. func (rt *ReferenceType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  522. if skip(rt) {
  523. return nil
  524. }
  525. base := rt.Base.Copy(fn, skip)
  526. if base == nil {
  527. return fn(rt)
  528. }
  529. rt = &ReferenceType{Base: base}
  530. if r := fn(rt); r != nil {
  531. return r
  532. }
  533. return rt
  534. }
  535. func (rt *ReferenceType) GoString() string {
  536. return rt.goString(0, "")
  537. }
  538. func (rt *ReferenceType) goString(indent int, field string) string {
  539. return fmt.Sprintf("%*s%sReferenceType:\n%s", indent, "", field,
  540. rt.Base.goString(indent+2, ""))
  541. }
  542. // RvalueReferenceType is an rvalue reference type.
  543. type RvalueReferenceType struct {
  544. Base AST
  545. }
  546. func (rt *RvalueReferenceType) print(ps *printState) {
  547. printBase(ps, rt, rt.Base)
  548. }
  549. func (rt *RvalueReferenceType) printInner(ps *printState) {
  550. ps.writeString("&&")
  551. }
  552. func (rt *RvalueReferenceType) Traverse(fn func(AST) bool) {
  553. if fn(rt) {
  554. rt.Base.Traverse(fn)
  555. }
  556. }
  557. func (rt *RvalueReferenceType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  558. if skip(rt) {
  559. return nil
  560. }
  561. base := rt.Base.Copy(fn, skip)
  562. if base == nil {
  563. return fn(rt)
  564. }
  565. rt = &RvalueReferenceType{Base: base}
  566. if r := fn(rt); r != nil {
  567. return r
  568. }
  569. return rt
  570. }
  571. func (rt *RvalueReferenceType) GoString() string {
  572. return rt.goString(0, "")
  573. }
  574. func (rt *RvalueReferenceType) goString(indent int, field string) string {
  575. return fmt.Sprintf("%*s%sRvalueReferenceType:\n%s", indent, "", field,
  576. rt.Base.goString(indent+2, ""))
  577. }
  578. // ComplexType is a complex type.
  579. type ComplexType struct {
  580. Base AST
  581. }
  582. func (ct *ComplexType) print(ps *printState) {
  583. printBase(ps, ct, ct.Base)
  584. }
  585. func (ct *ComplexType) printInner(ps *printState) {
  586. ps.writeString(" _Complex")
  587. }
  588. func (ct *ComplexType) Traverse(fn func(AST) bool) {
  589. if fn(ct) {
  590. ct.Base.Traverse(fn)
  591. }
  592. }
  593. func (ct *ComplexType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  594. if skip(ct) {
  595. return nil
  596. }
  597. base := ct.Base.Copy(fn, skip)
  598. if base == nil {
  599. return fn(ct)
  600. }
  601. ct = &ComplexType{Base: base}
  602. if r := fn(ct); r != nil {
  603. return r
  604. }
  605. return ct
  606. }
  607. func (ct *ComplexType) GoString() string {
  608. return ct.goString(0, "")
  609. }
  610. func (ct *ComplexType) goString(indent int, field string) string {
  611. return fmt.Sprintf("%*s%sComplexType:\n%s", indent, "", field,
  612. ct.Base.goString(indent+2, ""))
  613. }
  614. // ImaginaryType is an imaginary type.
  615. type ImaginaryType struct {
  616. Base AST
  617. }
  618. func (it *ImaginaryType) print(ps *printState) {
  619. printBase(ps, it, it.Base)
  620. }
  621. func (it *ImaginaryType) printInner(ps *printState) {
  622. ps.writeString(" _Imaginary")
  623. }
  624. func (it *ImaginaryType) Traverse(fn func(AST) bool) {
  625. if fn(it) {
  626. it.Base.Traverse(fn)
  627. }
  628. }
  629. func (it *ImaginaryType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  630. if skip(it) {
  631. return nil
  632. }
  633. base := it.Base.Copy(fn, skip)
  634. if base == nil {
  635. return fn(it)
  636. }
  637. it = &ImaginaryType{Base: base}
  638. if r := fn(it); r != nil {
  639. return r
  640. }
  641. return it
  642. }
  643. func (it *ImaginaryType) GoString() string {
  644. return it.goString(0, "")
  645. }
  646. func (it *ImaginaryType) goString(indent int, field string) string {
  647. return fmt.Sprintf("%*s%sImaginaryType:\n%s", indent, "", field,
  648. it.Base.goString(indent+2, ""))
  649. }
  650. // VendorQualifier is a type qualified by a vendor-specific qualifier.
  651. type VendorQualifier struct {
  652. Qualifier AST
  653. Type AST
  654. }
  655. func (vq *VendorQualifier) print(ps *printState) {
  656. ps.inner = append(ps.inner, vq)
  657. vq.Type.print(ps)
  658. if len(ps.inner) > 0 {
  659. ps.printOneInner(nil)
  660. }
  661. }
  662. func (vq *VendorQualifier) printInner(ps *printState) {
  663. ps.writeByte(' ')
  664. vq.Qualifier.print(ps)
  665. }
  666. func (vq *VendorQualifier) Traverse(fn func(AST) bool) {
  667. if fn(vq) {
  668. vq.Qualifier.Traverse(fn)
  669. vq.Type.Traverse(fn)
  670. }
  671. }
  672. func (vq *VendorQualifier) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  673. if skip(vq) {
  674. return nil
  675. }
  676. qualifier := vq.Qualifier.Copy(fn, skip)
  677. typ := vq.Type.Copy(fn, skip)
  678. if qualifier == nil && typ == nil {
  679. return fn(vq)
  680. }
  681. if qualifier == nil {
  682. qualifier = vq.Qualifier
  683. }
  684. if typ == nil {
  685. typ = vq.Type
  686. }
  687. vq = &VendorQualifier{Qualifier: qualifier, Type: vq.Type}
  688. if r := fn(vq); r != nil {
  689. return r
  690. }
  691. return vq
  692. }
  693. func (vq *VendorQualifier) GoString() string {
  694. return vq.goString(0, "")
  695. }
  696. func (vq *VendorQualifier) goString(indent int, field string) string {
  697. return fmt.Sprintf("%*s%sVendorQualifier:\n%s\n%s", indent, "", field,
  698. vq.Qualifier.goString(indent+2, "Qualifier: "),
  699. vq.Type.goString(indent+2, "Type: "))
  700. }
  701. // ArrayType is an array type.
  702. type ArrayType struct {
  703. Dimension AST
  704. Element AST
  705. }
  706. func (at *ArrayType) print(ps *printState) {
  707. // Pass the array type down as an inner type so that we print
  708. // multi-dimensional arrays correctly.
  709. ps.inner = append(ps.inner, at)
  710. at.Element.print(ps)
  711. if ln := len(ps.inner); ln > 0 {
  712. ps.inner = ps.inner[:ln-1]
  713. at.printDimension(ps)
  714. }
  715. }
  716. func (at *ArrayType) printInner(ps *printState) {
  717. at.printDimension(ps)
  718. }
  719. // Print the array dimension.
  720. func (at *ArrayType) printDimension(ps *printState) {
  721. space := " "
  722. for len(ps.inner) > 0 {
  723. // We haven't gotten to the real type yet. Use
  724. // parentheses around that type, except that if it is
  725. // an array type we print it as a multi-dimensional
  726. // array
  727. in := ps.inner[len(ps.inner)-1]
  728. if twq, ok := in.(*TypeWithQualifiers); ok {
  729. in = twq.Base
  730. }
  731. if _, ok := in.(*ArrayType); ok {
  732. if in == ps.inner[len(ps.inner)-1] {
  733. space = ""
  734. }
  735. ps.printOneInner(nil)
  736. } else {
  737. ps.writeString(" (")
  738. ps.printInner(false)
  739. ps.writeByte(')')
  740. }
  741. }
  742. ps.writeString(space)
  743. ps.writeByte('[')
  744. at.Dimension.print(ps)
  745. ps.writeByte(']')
  746. }
  747. func (at *ArrayType) Traverse(fn func(AST) bool) {
  748. if fn(at) {
  749. at.Dimension.Traverse(fn)
  750. at.Element.Traverse(fn)
  751. }
  752. }
  753. func (at *ArrayType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  754. if skip(at) {
  755. return nil
  756. }
  757. dimension := at.Dimension.Copy(fn, skip)
  758. element := at.Element.Copy(fn, skip)
  759. if dimension == nil && element == nil {
  760. return fn(at)
  761. }
  762. if dimension == nil {
  763. dimension = at.Dimension
  764. }
  765. if element == nil {
  766. element = at.Element
  767. }
  768. at = &ArrayType{Dimension: dimension, Element: element}
  769. if r := fn(at); r != nil {
  770. return r
  771. }
  772. return at
  773. }
  774. func (at *ArrayType) GoString() string {
  775. return at.goString(0, "")
  776. }
  777. func (at *ArrayType) goString(indent int, field string) string {
  778. return fmt.Sprintf("%*s%sArrayType:\n%s\n%s", indent, "", field,
  779. at.Dimension.goString(indent+2, "Dimension: "),
  780. at.Element.goString(indent+2, "Element: "))
  781. }
  782. // FunctionType is a function type. The Return field may be nil for
  783. // cases where the return type is not part of the mangled name.
  784. type FunctionType struct {
  785. Return AST
  786. Args []AST
  787. }
  788. func (ft *FunctionType) print(ps *printState) {
  789. if ft.Return != nil {
  790. // Pass the return type as an inner type in order to
  791. // print the arguments in the right location.
  792. ps.inner = append(ps.inner, ft)
  793. ft.Return.print(ps)
  794. if len(ps.inner) == 0 {
  795. // Everything was printed.
  796. return
  797. }
  798. ps.inner = ps.inner[:len(ps.inner)-1]
  799. ps.writeByte(' ')
  800. }
  801. ft.printArgs(ps)
  802. }
  803. func (ft *FunctionType) printInner(ps *printState) {
  804. ft.printArgs(ps)
  805. }
  806. // printArgs prints the arguments of a function type. It looks at the
  807. // inner types for spacing.
  808. func (ft *FunctionType) printArgs(ps *printState) {
  809. paren := false
  810. space := false
  811. for i := len(ps.inner) - 1; i >= 0; i-- {
  812. switch ps.inner[i].(type) {
  813. case *PointerType, *ReferenceType, *RvalueReferenceType:
  814. paren = true
  815. case *TypeWithQualifiers, *ComplexType, *ImaginaryType, *PtrMem:
  816. space = true
  817. paren = true
  818. }
  819. if paren {
  820. break
  821. }
  822. }
  823. if paren {
  824. if !space && (ps.last != '(' && ps.last != '*') {
  825. space = true
  826. }
  827. if space && ps.last != ' ' {
  828. ps.writeByte(' ')
  829. }
  830. ps.writeByte('(')
  831. }
  832. save := ps.printInner(true)
  833. if paren {
  834. ps.writeByte(')')
  835. }
  836. ps.writeByte('(')
  837. first := true
  838. for _, a := range ft.Args {
  839. if ps.isEmpty(a) {
  840. continue
  841. }
  842. if !first {
  843. ps.writeString(", ")
  844. }
  845. a.print(ps)
  846. first = false
  847. }
  848. ps.writeByte(')')
  849. ps.inner = save
  850. ps.printInner(false)
  851. }
  852. func (ft *FunctionType) Traverse(fn func(AST) bool) {
  853. if fn(ft) {
  854. if ft.Return != nil {
  855. ft.Return.Traverse(fn)
  856. }
  857. for _, a := range ft.Args {
  858. a.Traverse(fn)
  859. }
  860. }
  861. }
  862. func (ft *FunctionType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  863. if skip(ft) {
  864. return nil
  865. }
  866. changed := false
  867. var ret AST
  868. if ft.Return != nil {
  869. ret = ft.Return.Copy(fn, skip)
  870. if ret == nil {
  871. ret = ft.Return
  872. } else {
  873. changed = true
  874. }
  875. }
  876. args := make([]AST, len(ft.Args))
  877. for i, a := range ft.Args {
  878. ac := a.Copy(fn, skip)
  879. if ac == nil {
  880. args[i] = a
  881. } else {
  882. args[i] = ac
  883. changed = true
  884. }
  885. }
  886. if !changed {
  887. return fn(ft)
  888. }
  889. ft = &FunctionType{Return: ret, Args: args}
  890. if r := fn(ft); r != nil {
  891. return r
  892. }
  893. return ft
  894. }
  895. func (ft *FunctionType) GoString() string {
  896. return ft.goString(0, "")
  897. }
  898. func (ft *FunctionType) goString(indent int, field string) string {
  899. var r string
  900. if ft.Return == nil {
  901. r = fmt.Sprintf("%*sReturn: nil", indent+2, "")
  902. } else {
  903. r = ft.Return.goString(indent+2, "Return: ")
  904. }
  905. var args string
  906. if len(ft.Args) == 0 {
  907. args = fmt.Sprintf("%*sArgs: nil", indent+2, "")
  908. } else {
  909. args = fmt.Sprintf("%*sArgs:", indent+2, "")
  910. for i, a := range ft.Args {
  911. args += "\n"
  912. args += a.goString(indent+4, fmt.Sprintf("%d: ", i))
  913. }
  914. }
  915. return fmt.Sprintf("%*s%sFunctionType:\n%s\n%s", indent, "", field, r, args)
  916. }
  917. // FunctionParam is a parameter of a function, used for last-specified
  918. // return type in a closure.
  919. type FunctionParam struct {
  920. Index int
  921. }
  922. func (fp *FunctionParam) print(ps *printState) {
  923. if fp.Index == 0 {
  924. ps.writeString("this")
  925. } else {
  926. fmt.Fprintf(&ps.buf, "{parm#%d}", fp.Index)
  927. }
  928. }
  929. func (fp *FunctionParam) Traverse(fn func(AST) bool) {
  930. fn(fp)
  931. }
  932. func (fp *FunctionParam) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  933. if skip(fp) {
  934. return nil
  935. }
  936. return fn(fp)
  937. }
  938. func (fp *FunctionParam) GoString() string {
  939. return fp.goString(0, "")
  940. }
  941. func (fp *FunctionParam) goString(indent int, field string) string {
  942. return fmt.Sprintf("%*s%sFunctionParam: %d", indent, "", field, fp.Index)
  943. }
  944. // PtrMem is a pointer-to-member expression.
  945. type PtrMem struct {
  946. Class AST
  947. Member AST
  948. }
  949. func (pm *PtrMem) print(ps *printState) {
  950. ps.inner = append(ps.inner, pm)
  951. pm.Member.print(ps)
  952. if len(ps.inner) > 0 {
  953. ps.printOneInner(nil)
  954. }
  955. }
  956. func (pm *PtrMem) printInner(ps *printState) {
  957. if ps.last != '(' {
  958. ps.writeByte(' ')
  959. }
  960. pm.Class.print(ps)
  961. ps.writeString("::*")
  962. }
  963. func (pm *PtrMem) Traverse(fn func(AST) bool) {
  964. if fn(pm) {
  965. pm.Class.Traverse(fn)
  966. pm.Member.Traverse(fn)
  967. }
  968. }
  969. func (pm *PtrMem) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  970. if skip(pm) {
  971. return nil
  972. }
  973. class := pm.Class.Copy(fn, skip)
  974. member := pm.Member.Copy(fn, skip)
  975. if class == nil && member == nil {
  976. return fn(pm)
  977. }
  978. if class == nil {
  979. class = pm.Class
  980. }
  981. if member == nil {
  982. member = pm.Member
  983. }
  984. pm = &PtrMem{Class: class, Member: member}
  985. if r := fn(pm); r != nil {
  986. return r
  987. }
  988. return pm
  989. }
  990. func (pm *PtrMem) GoString() string {
  991. return pm.goString(0, "")
  992. }
  993. func (pm *PtrMem) goString(indent int, field string) string {
  994. return fmt.Sprintf("%*s%sPtrMem:\n%s\n%s", indent, "", field,
  995. pm.Class.goString(indent+2, "Class: "),
  996. pm.Member.goString(indent+2, "Member: "))
  997. }
  998. // FixedType is a fixed numeric type of unknown size.
  999. type FixedType struct {
  1000. Base AST
  1001. Accum bool
  1002. Sat bool
  1003. }
  1004. func (ft *FixedType) print(ps *printState) {
  1005. if ft.Sat {
  1006. ps.writeString("_Sat ")
  1007. }
  1008. if bt, ok := ft.Base.(*BuiltinType); ok && bt.Name == "int" {
  1009. // The standard demangler skips printing "int".
  1010. } else {
  1011. ft.Base.print(ps)
  1012. ps.writeByte(' ')
  1013. }
  1014. if ft.Accum {
  1015. ps.writeString("_Accum")
  1016. } else {
  1017. ps.writeString("_Fract")
  1018. }
  1019. }
  1020. func (ft *FixedType) Traverse(fn func(AST) bool) {
  1021. if fn(ft) {
  1022. ft.Base.Traverse(fn)
  1023. }
  1024. }
  1025. func (ft *FixedType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1026. if skip(ft) {
  1027. return nil
  1028. }
  1029. base := ft.Base.Copy(fn, skip)
  1030. if base == nil {
  1031. return fn(ft)
  1032. }
  1033. ft = &FixedType{Base: base, Accum: ft.Accum, Sat: ft.Sat}
  1034. if r := fn(ft); r != nil {
  1035. return r
  1036. }
  1037. return ft
  1038. }
  1039. func (ft *FixedType) GoString() string {
  1040. return ft.goString(0, "")
  1041. }
  1042. func (ft *FixedType) goString(indent int, field string) string {
  1043. return fmt.Sprintf("%*s%sFixedType: Accum: %t; Sat: %t\n%s", indent, "", field,
  1044. ft.Accum, ft.Sat,
  1045. ft.Base.goString(indent+2, "Base: "))
  1046. }
  1047. // VectorType is a vector type.
  1048. type VectorType struct {
  1049. Dimension AST
  1050. Base AST
  1051. }
  1052. func (vt *VectorType) print(ps *printState) {
  1053. ps.inner = append(ps.inner, vt)
  1054. vt.Base.print(ps)
  1055. if len(ps.inner) > 0 {
  1056. ps.printOneInner(nil)
  1057. }
  1058. }
  1059. func (vt *VectorType) printInner(ps *printState) {
  1060. ps.writeString(" __vector(")
  1061. vt.Dimension.print(ps)
  1062. ps.writeByte(')')
  1063. }
  1064. func (vt *VectorType) Traverse(fn func(AST) bool) {
  1065. if fn(vt) {
  1066. vt.Dimension.Traverse(fn)
  1067. vt.Base.Traverse(fn)
  1068. }
  1069. }
  1070. func (vt *VectorType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1071. if skip(vt) {
  1072. return nil
  1073. }
  1074. dimension := vt.Dimension.Copy(fn, skip)
  1075. base := vt.Base.Copy(fn, skip)
  1076. if dimension == nil && base == nil {
  1077. return fn(vt)
  1078. }
  1079. if dimension == nil {
  1080. dimension = vt.Dimension
  1081. }
  1082. if base == nil {
  1083. base = vt.Base
  1084. }
  1085. vt = &VectorType{Dimension: dimension, Base: base}
  1086. if r := fn(vt); r != nil {
  1087. return r
  1088. }
  1089. return vt
  1090. }
  1091. func (vt *VectorType) GoString() string {
  1092. return vt.goString(0, "")
  1093. }
  1094. func (vt *VectorType) goString(indent int, field string) string {
  1095. return fmt.Sprintf("%*s%sVectorType:\n%s\n%s", indent, "", field,
  1096. vt.Dimension.goString(indent+2, "Dimension: "),
  1097. vt.Base.goString(indent+2, "Base: "))
  1098. }
  1099. // Decltype is the decltype operator.
  1100. type Decltype struct {
  1101. Expr AST
  1102. }
  1103. func (dt *Decltype) print(ps *printState) {
  1104. ps.writeString("decltype (")
  1105. dt.Expr.print(ps)
  1106. ps.writeByte(')')
  1107. }
  1108. func (dt *Decltype) Traverse(fn func(AST) bool) {
  1109. if fn(dt) {
  1110. dt.Expr.Traverse(fn)
  1111. }
  1112. }
  1113. func (dt *Decltype) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1114. if skip(dt) {
  1115. return nil
  1116. }
  1117. expr := dt.Expr.Copy(fn, skip)
  1118. if expr == nil {
  1119. return fn(dt)
  1120. }
  1121. dt = &Decltype{Expr: expr}
  1122. if r := fn(dt); r != nil {
  1123. return r
  1124. }
  1125. return dt
  1126. }
  1127. func (dt *Decltype) GoString() string {
  1128. return dt.goString(0, "")
  1129. }
  1130. func (dt *Decltype) goString(indent int, field string) string {
  1131. return fmt.Sprintf("%*s%sDecltype:\n%s", indent, "", field,
  1132. dt.Expr.goString(indent+2, "Expr: "))
  1133. }
  1134. // Operator is an operator.
  1135. type Operator struct {
  1136. Name string
  1137. }
  1138. func (op *Operator) print(ps *printState) {
  1139. ps.writeString("operator")
  1140. if isLower(op.Name[0]) {
  1141. ps.writeByte(' ')
  1142. }
  1143. n := op.Name
  1144. n = strings.TrimSuffix(n, " ")
  1145. ps.writeString(n)
  1146. }
  1147. func (op *Operator) Traverse(fn func(AST) bool) {
  1148. fn(op)
  1149. }
  1150. func (op *Operator) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1151. if skip(op) {
  1152. return nil
  1153. }
  1154. return fn(op)
  1155. }
  1156. func (op *Operator) GoString() string {
  1157. return op.goString(0, "")
  1158. }
  1159. func (op *Operator) goString(indent int, field string) string {
  1160. return fmt.Sprintf("%*s%sOperator: %s", indent, "", field, op.Name)
  1161. }
  1162. // Constructor is a constructor.
  1163. type Constructor struct {
  1164. Name AST
  1165. }
  1166. func (c *Constructor) print(ps *printState) {
  1167. c.Name.print(ps)
  1168. }
  1169. func (c *Constructor) Traverse(fn func(AST) bool) {
  1170. if fn(c) {
  1171. c.Name.Traverse(fn)
  1172. }
  1173. }
  1174. func (c *Constructor) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1175. if skip(c) {
  1176. return nil
  1177. }
  1178. name := c.Name.Copy(fn, skip)
  1179. if name == nil {
  1180. return fn(c)
  1181. }
  1182. c = &Constructor{Name: name}
  1183. if r := fn(c); r != nil {
  1184. return r
  1185. }
  1186. return c
  1187. }
  1188. func (c *Constructor) GoString() string {
  1189. return c.goString(0, "")
  1190. }
  1191. func (c *Constructor) goString(indent int, field string) string {
  1192. return fmt.Sprintf("%*s%sConstructor:\n%s", indent, "", field, c.Name.goString(indent+2, "Name: "))
  1193. }
  1194. // Destructor is a destructor.
  1195. type Destructor struct {
  1196. Name AST
  1197. }
  1198. func (d *Destructor) print(ps *printState) {
  1199. ps.writeByte('~')
  1200. d.Name.print(ps)
  1201. }
  1202. func (d *Destructor) Traverse(fn func(AST) bool) {
  1203. if fn(d) {
  1204. d.Name.Traverse(fn)
  1205. }
  1206. }
  1207. func (d *Destructor) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1208. if skip(d) {
  1209. return nil
  1210. }
  1211. name := d.Name.Copy(fn, skip)
  1212. if name == nil {
  1213. return fn(d)
  1214. }
  1215. d = &Destructor{Name: name}
  1216. if r := fn(d); r != nil {
  1217. return r
  1218. }
  1219. return d
  1220. }
  1221. func (d *Destructor) GoString() string {
  1222. return d.goString(0, "")
  1223. }
  1224. func (d *Destructor) goString(indent int, field string) string {
  1225. return fmt.Sprintf("%*s%sDestructor:\n%s", indent, "", field, d.Name.goString(indent+2, "Name: "))
  1226. }
  1227. // GlobalCDtor is a global constructor or destructor.
  1228. type GlobalCDtor struct {
  1229. Ctor bool
  1230. Key AST
  1231. }
  1232. func (gcd *GlobalCDtor) print(ps *printState) {
  1233. ps.writeString("global ")
  1234. if gcd.Ctor {
  1235. ps.writeString("constructors")
  1236. } else {
  1237. ps.writeString("destructors")
  1238. }
  1239. ps.writeString(" keyed to ")
  1240. gcd.Key.print(ps)
  1241. }
  1242. func (gcd *GlobalCDtor) Traverse(fn func(AST) bool) {
  1243. if fn(gcd) {
  1244. gcd.Key.Traverse(fn)
  1245. }
  1246. }
  1247. func (gcd *GlobalCDtor) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1248. if skip(gcd) {
  1249. return nil
  1250. }
  1251. key := gcd.Key.Copy(fn, skip)
  1252. if key == nil {
  1253. return fn(gcd)
  1254. }
  1255. gcd = &GlobalCDtor{Ctor: gcd.Ctor, Key: key}
  1256. if r := fn(gcd); r != nil {
  1257. return r
  1258. }
  1259. return gcd
  1260. }
  1261. func (gcd *GlobalCDtor) GoString() string {
  1262. return gcd.goString(0, "")
  1263. }
  1264. func (gcd *GlobalCDtor) goString(indent int, field string) string {
  1265. return fmt.Sprintf("%*s%sGlobalCDtor: Ctor: %t\n%s", indent, "", field,
  1266. gcd.Ctor, gcd.Key.goString(indent+2, "Key: "))
  1267. }
  1268. // TaggedName is a name with an ABI tag.
  1269. type TaggedName struct {
  1270. Name AST
  1271. Tag AST
  1272. }
  1273. func (t *TaggedName) print(ps *printState) {
  1274. t.Name.print(ps)
  1275. ps.writeString("[abi:")
  1276. t.Tag.print(ps)
  1277. ps.writeByte(']')
  1278. }
  1279. func (t *TaggedName) Traverse(fn func(AST) bool) {
  1280. if fn(t) {
  1281. t.Name.Traverse(fn)
  1282. t.Tag.Traverse(fn)
  1283. }
  1284. }
  1285. func (t *TaggedName) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1286. if skip(t) {
  1287. return nil
  1288. }
  1289. name := t.Name.Copy(fn, skip)
  1290. tag := t.Tag.Copy(fn, skip)
  1291. if name == nil && tag == nil {
  1292. return fn(t)
  1293. }
  1294. if name == nil {
  1295. name = t.Name
  1296. }
  1297. if tag == nil {
  1298. tag = t.Tag
  1299. }
  1300. t = &TaggedName{Name: name, Tag: tag}
  1301. if r := fn(t); r != nil {
  1302. return r
  1303. }
  1304. return t
  1305. }
  1306. func (t *TaggedName) GoString() string {
  1307. return t.goString(0, "")
  1308. }
  1309. func (t *TaggedName) goString(indent int, field string) string {
  1310. return fmt.Sprintf("%*s%sTaggedName:\n%s\n%s", indent, "", field,
  1311. t.Name.goString(indent+2, "Name: "),
  1312. t.Tag.goString(indent+2, "Tag: "))
  1313. }
  1314. // PackExpansion is a pack expansion. The Pack field may be nil.
  1315. type PackExpansion struct {
  1316. Base AST
  1317. Pack *ArgumentPack
  1318. }
  1319. func (pe *PackExpansion) print(ps *printState) {
  1320. // We normally only get here if the simplify function was
  1321. // unable to locate and expand the pack.
  1322. if pe.Pack == nil {
  1323. parenthesize(ps, pe.Base)
  1324. ps.writeString("...")
  1325. } else {
  1326. pe.Base.print(ps)
  1327. }
  1328. }
  1329. func (pe *PackExpansion) Traverse(fn func(AST) bool) {
  1330. if fn(pe) {
  1331. pe.Base.Traverse(fn)
  1332. // Don't traverse Template--it points elsewhere in the AST.
  1333. }
  1334. }
  1335. func (pe *PackExpansion) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1336. if skip(pe) {
  1337. return nil
  1338. }
  1339. base := pe.Base.Copy(fn, skip)
  1340. if base == nil {
  1341. return fn(pe)
  1342. }
  1343. pe = &PackExpansion{Base: base, Pack: pe.Pack}
  1344. if r := fn(pe); r != nil {
  1345. return r
  1346. }
  1347. return pe
  1348. }
  1349. func (pe *PackExpansion) GoString() string {
  1350. return pe.goString(0, "")
  1351. }
  1352. func (pe *PackExpansion) goString(indent int, field string) string {
  1353. return fmt.Sprintf("%*s%sPackExpansion: Pack: %p\n%s", indent, "", field,
  1354. pe.Pack, pe.Base.goString(indent+2, "Base: "))
  1355. }
  1356. // ArgumentPack is an argument pack.
  1357. type ArgumentPack struct {
  1358. Args []AST
  1359. }
  1360. func (ap *ArgumentPack) print(ps *printState) {
  1361. for i, a := range ap.Args {
  1362. if i > 0 {
  1363. ps.writeString(", ")
  1364. }
  1365. a.print(ps)
  1366. }
  1367. }
  1368. func (ap *ArgumentPack) Traverse(fn func(AST) bool) {
  1369. if fn(ap) {
  1370. for _, a := range ap.Args {
  1371. a.Traverse(fn)
  1372. }
  1373. }
  1374. }
  1375. func (ap *ArgumentPack) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1376. if skip(ap) {
  1377. return nil
  1378. }
  1379. args := make([]AST, len(ap.Args))
  1380. changed := false
  1381. for i, a := range ap.Args {
  1382. ac := a.Copy(fn, skip)
  1383. if ac == nil {
  1384. args[i] = a
  1385. } else {
  1386. args[i] = ac
  1387. changed = true
  1388. }
  1389. }
  1390. if !changed {
  1391. return fn(ap)
  1392. }
  1393. ap = &ArgumentPack{Args: args}
  1394. if r := fn(ap); r != nil {
  1395. return r
  1396. }
  1397. return ap
  1398. }
  1399. func (ap *ArgumentPack) GoString() string {
  1400. return ap.goString(0, "")
  1401. }
  1402. func (ap *ArgumentPack) goString(indent int, field string) string {
  1403. if len(ap.Args) == 0 {
  1404. return fmt.Sprintf("%*s%sArgumentPack: nil", indent, "", field)
  1405. }
  1406. s := fmt.Sprintf("%*s%sArgumentPack:", indent, "", field)
  1407. for i, a := range ap.Args {
  1408. s += "\n"
  1409. s += a.goString(indent+2, fmt.Sprintf("%d: ", i))
  1410. }
  1411. return s
  1412. }
  1413. // Cast is a type cast.
  1414. type Cast struct {
  1415. To AST
  1416. }
  1417. func (c *Cast) print(ps *printState) {
  1418. ps.writeString("operator ")
  1419. c.To.print(ps)
  1420. }
  1421. func (c *Cast) Traverse(fn func(AST) bool) {
  1422. if fn(c) {
  1423. c.To.Traverse(fn)
  1424. }
  1425. }
  1426. func (c *Cast) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1427. if skip(c) {
  1428. return nil
  1429. }
  1430. to := c.To.Copy(fn, skip)
  1431. if to == nil {
  1432. return fn(c)
  1433. }
  1434. c = &Cast{To: to}
  1435. if r := fn(c); r != nil {
  1436. return r
  1437. }
  1438. return c
  1439. }
  1440. func (c *Cast) GoString() string {
  1441. return c.goString(0, "")
  1442. }
  1443. func (c *Cast) goString(indent int, field string) string {
  1444. return fmt.Sprintf("%*s%sCast\n%s", indent, "", field,
  1445. c.To.goString(indent+2, "To: "))
  1446. }
  1447. // The parenthesize function prints the string for val, wrapped in
  1448. // parentheses if necessary.
  1449. func parenthesize(ps *printState, val AST) {
  1450. paren := false
  1451. switch v := val.(type) {
  1452. case *Name, *InitializerList, *FunctionParam:
  1453. case *Qualified:
  1454. if v.LocalName {
  1455. paren = true
  1456. }
  1457. default:
  1458. paren = true
  1459. }
  1460. if paren {
  1461. ps.writeByte('(')
  1462. }
  1463. val.print(ps)
  1464. if paren {
  1465. ps.writeByte(')')
  1466. }
  1467. }
  1468. // Nullary is an operator in an expression with no arguments, such as
  1469. // throw.
  1470. type Nullary struct {
  1471. Op AST
  1472. }
  1473. func (n *Nullary) print(ps *printState) {
  1474. if op, ok := n.Op.(*Operator); ok {
  1475. ps.writeString(op.Name)
  1476. } else {
  1477. n.Op.print(ps)
  1478. }
  1479. }
  1480. func (n *Nullary) Traverse(fn func(AST) bool) {
  1481. if fn(n) {
  1482. n.Op.Traverse(fn)
  1483. }
  1484. }
  1485. func (n *Nullary) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1486. if skip(n) {
  1487. return nil
  1488. }
  1489. op := n.Op.Copy(fn, skip)
  1490. if op == nil {
  1491. return fn(n)
  1492. }
  1493. n = &Nullary{Op: op}
  1494. if r := fn(n); r != nil {
  1495. return r
  1496. }
  1497. return n
  1498. }
  1499. func (n *Nullary) GoString() string {
  1500. return n.goString(0, "")
  1501. }
  1502. func (n *Nullary) goString(indent int, field string) string {
  1503. return fmt.Sprintf("%*s%sNullary:\n%s", indent, "", field,
  1504. n.Op.goString(indent+2, "Op: "))
  1505. }
  1506. // Unary is a unary operation in an expression.
  1507. type Unary struct {
  1508. Op AST
  1509. Expr AST
  1510. Suffix bool // true for ++ -- when used as postfix
  1511. SizeofType bool // true for sizeof (type)
  1512. }
  1513. func (u *Unary) print(ps *printState) {
  1514. expr := u.Expr
  1515. // Don't print the argument list when taking the address of a
  1516. // function.
  1517. if op, ok := u.Op.(*Operator); ok && op.Name == "&" {
  1518. if t, ok := expr.(*Typed); ok {
  1519. if _, ok := t.Type.(*FunctionType); ok {
  1520. expr = t.Name
  1521. }
  1522. }
  1523. }
  1524. if u.Suffix {
  1525. parenthesize(ps, expr)
  1526. }
  1527. if op, ok := u.Op.(*Operator); ok {
  1528. ps.writeString(op.Name)
  1529. } else if c, ok := u.Op.(*Cast); ok {
  1530. ps.writeByte('(')
  1531. c.To.print(ps)
  1532. ps.writeByte(')')
  1533. } else {
  1534. u.Op.print(ps)
  1535. }
  1536. if !u.Suffix {
  1537. if op, ok := u.Op.(*Operator); ok && op.Name == "::" {
  1538. // Don't use parentheses after ::.
  1539. expr.print(ps)
  1540. } else if u.SizeofType {
  1541. // Always use parentheses for sizeof argument.
  1542. ps.writeByte('(')
  1543. expr.print(ps)
  1544. ps.writeByte(')')
  1545. } else {
  1546. parenthesize(ps, expr)
  1547. }
  1548. }
  1549. }
  1550. func (u *Unary) Traverse(fn func(AST) bool) {
  1551. if fn(u) {
  1552. u.Op.Traverse(fn)
  1553. u.Expr.Traverse(fn)
  1554. }
  1555. }
  1556. func (u *Unary) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1557. if skip(u) {
  1558. return nil
  1559. }
  1560. op := u.Op.Copy(fn, skip)
  1561. expr := u.Expr.Copy(fn, skip)
  1562. if op == nil && expr == nil {
  1563. return fn(u)
  1564. }
  1565. if op == nil {
  1566. op = u.Op
  1567. }
  1568. if expr == nil {
  1569. expr = u.Expr
  1570. }
  1571. u = &Unary{Op: op, Expr: expr, Suffix: u.Suffix, SizeofType: u.SizeofType}
  1572. if r := fn(u); r != nil {
  1573. return r
  1574. }
  1575. return u
  1576. }
  1577. func (u *Unary) GoString() string {
  1578. return u.goString(0, "")
  1579. }
  1580. func (u *Unary) goString(indent int, field string) string {
  1581. var s string
  1582. if u.Suffix {
  1583. s = " Suffix: true"
  1584. }
  1585. if u.SizeofType {
  1586. s += " SizeofType: true"
  1587. }
  1588. return fmt.Sprintf("%*s%sUnary:%s\n%s\n%s", indent, "", field,
  1589. s, u.Op.goString(indent+2, "Op: "),
  1590. u.Expr.goString(indent+2, "Expr: "))
  1591. }
  1592. // Binary is a binary operation in an expression.
  1593. type Binary struct {
  1594. Op AST
  1595. Left AST
  1596. Right AST
  1597. }
  1598. func (b *Binary) print(ps *printState) {
  1599. op, _ := b.Op.(*Operator)
  1600. if op != nil && strings.Contains(op.Name, "cast") {
  1601. ps.writeString(op.Name)
  1602. ps.writeByte('<')
  1603. b.Left.print(ps)
  1604. ps.writeString(">(")
  1605. b.Right.print(ps)
  1606. ps.writeByte(')')
  1607. return
  1608. }
  1609. // Use an extra set of parentheses around an expression that
  1610. // uses the greater-than operator, so that it does not get
  1611. // confused with the '>' that ends template parameters.
  1612. if op != nil && op.Name == ">" {
  1613. ps.writeByte('(')
  1614. }
  1615. left := b.Left
  1616. // A function call in an expression should not print the types
  1617. // of the arguments.
  1618. if op != nil && op.Name == "()" {
  1619. if ty, ok := b.Left.(*Typed); ok {
  1620. left = ty.Name
  1621. }
  1622. }
  1623. parenthesize(ps, left)
  1624. if op != nil && op.Name == "[]" {
  1625. ps.writeByte('[')
  1626. b.Right.print(ps)
  1627. ps.writeByte(']')
  1628. return
  1629. }
  1630. if op != nil {
  1631. if op.Name != "()" {
  1632. ps.writeString(op.Name)
  1633. }
  1634. } else {
  1635. b.Op.print(ps)
  1636. }
  1637. parenthesize(ps, b.Right)
  1638. if op != nil && op.Name == ">" {
  1639. ps.writeByte(')')
  1640. }
  1641. }
  1642. func (b *Binary) Traverse(fn func(AST) bool) {
  1643. if fn(b) {
  1644. b.Op.Traverse(fn)
  1645. b.Left.Traverse(fn)
  1646. b.Right.Traverse(fn)
  1647. }
  1648. }
  1649. func (b *Binary) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1650. if skip(b) {
  1651. return nil
  1652. }
  1653. op := b.Op.Copy(fn, skip)
  1654. left := b.Left.Copy(fn, skip)
  1655. right := b.Right.Copy(fn, skip)
  1656. if op == nil && left == nil && right == nil {
  1657. return fn(b)
  1658. }
  1659. if op == nil {
  1660. op = b.Op
  1661. }
  1662. if left == nil {
  1663. left = b.Left
  1664. }
  1665. if right == nil {
  1666. right = b.Right
  1667. }
  1668. b = &Binary{Op: op, Left: left, Right: right}
  1669. if r := fn(b); r != nil {
  1670. return r
  1671. }
  1672. return b
  1673. }
  1674. func (b *Binary) GoString() string {
  1675. return b.goString(0, "")
  1676. }
  1677. func (b *Binary) goString(indent int, field string) string {
  1678. return fmt.Sprintf("%*s%sBinary:\n%s\n%s\n%s", indent, "", field,
  1679. b.Op.goString(indent+2, "Op: "),
  1680. b.Left.goString(indent+2, "Left: "),
  1681. b.Right.goString(indent+2, "Right: "))
  1682. }
  1683. // Trinary is the ?: trinary operation in an expression.
  1684. type Trinary struct {
  1685. Op AST
  1686. First AST
  1687. Second AST
  1688. Third AST
  1689. }
  1690. func (t *Trinary) print(ps *printState) {
  1691. parenthesize(ps, t.First)
  1692. ps.writeByte('?')
  1693. parenthesize(ps, t.Second)
  1694. ps.writeString(" : ")
  1695. parenthesize(ps, t.Third)
  1696. }
  1697. func (t *Trinary) Traverse(fn func(AST) bool) {
  1698. if fn(t) {
  1699. t.Op.Traverse(fn)
  1700. t.First.Traverse(fn)
  1701. t.Second.Traverse(fn)
  1702. t.Third.Traverse(fn)
  1703. }
  1704. }
  1705. func (t *Trinary) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1706. if skip(t) {
  1707. return nil
  1708. }
  1709. op := t.Op.Copy(fn, skip)
  1710. first := t.First.Copy(fn, skip)
  1711. second := t.Second.Copy(fn, skip)
  1712. third := t.Third.Copy(fn, skip)
  1713. if op == nil && first == nil && second == nil && third == nil {
  1714. return fn(t)
  1715. }
  1716. if op == nil {
  1717. op = t.Op
  1718. }
  1719. if first == nil {
  1720. first = t.First
  1721. }
  1722. if second == nil {
  1723. second = t.Second
  1724. }
  1725. if third == nil {
  1726. third = t.Third
  1727. }
  1728. t = &Trinary{Op: op, First: first, Second: second, Third: third}
  1729. if r := fn(t); r != nil {
  1730. return r
  1731. }
  1732. return t
  1733. }
  1734. func (t *Trinary) GoString() string {
  1735. return t.goString(0, "")
  1736. }
  1737. func (t *Trinary) goString(indent int, field string) string {
  1738. return fmt.Sprintf("%*s%sTrinary:\n%s\n%s\n%s\n%s", indent, "", field,
  1739. t.Op.goString(indent+2, "Op: "),
  1740. t.First.goString(indent+2, "First: "),
  1741. t.Second.goString(indent+2, "Second: "),
  1742. t.Third.goString(indent+2, "Third: "))
  1743. }
  1744. // New is a use of operator new in an expression.
  1745. type New struct {
  1746. Op AST
  1747. Place AST
  1748. Type AST
  1749. Init AST
  1750. }
  1751. func (n *New) print(ps *printState) {
  1752. // Op doesn't really matter for printing--we always print "new".
  1753. ps.writeString("new ")
  1754. if n.Place != nil {
  1755. parenthesize(ps, n.Place)
  1756. ps.writeByte(' ')
  1757. }
  1758. n.Type.print(ps)
  1759. if n.Init != nil {
  1760. parenthesize(ps, n.Init)
  1761. }
  1762. }
  1763. func (n *New) Traverse(fn func(AST) bool) {
  1764. if fn(n) {
  1765. n.Op.Traverse(fn)
  1766. n.Place.Traverse(fn)
  1767. n.Type.Traverse(fn)
  1768. n.Init.Traverse(fn)
  1769. }
  1770. }
  1771. func (n *New) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1772. if skip(n) {
  1773. return nil
  1774. }
  1775. op := n.Op.Copy(fn, skip)
  1776. var place AST
  1777. if n.Place != nil {
  1778. place = n.Place.Copy(fn, skip)
  1779. }
  1780. typ := n.Type.Copy(fn, skip)
  1781. var ini AST
  1782. if n.Init != nil {
  1783. ini = n.Init.Copy(fn, skip)
  1784. }
  1785. if op == nil && place == nil && typ == nil && ini == nil {
  1786. return fn(n)
  1787. }
  1788. if op == nil {
  1789. op = n.Op
  1790. }
  1791. if place == nil {
  1792. place = n.Place
  1793. }
  1794. if typ == nil {
  1795. typ = n.Type
  1796. }
  1797. if ini == nil {
  1798. ini = n.Init
  1799. }
  1800. n = &New{Op: op, Place: place, Type: typ, Init: ini}
  1801. if r := fn(n); r != nil {
  1802. return r
  1803. }
  1804. return n
  1805. }
  1806. func (n *New) GoString() string {
  1807. return n.goString(0, "")
  1808. }
  1809. func (n *New) goString(indent int, field string) string {
  1810. var place string
  1811. if n.Place == nil {
  1812. place = fmt.Sprintf("%*sPlace: nil", indent, "")
  1813. } else {
  1814. place = n.Place.goString(indent+2, "Place: ")
  1815. }
  1816. var ini string
  1817. if n.Init == nil {
  1818. ini = fmt.Sprintf("%*sInit: nil", indent, "")
  1819. } else {
  1820. ini = n.Init.goString(indent+2, "Init: ")
  1821. }
  1822. return fmt.Sprintf("%*s%sNew:\n%s\n%s\n%s\n%s", indent, "", field,
  1823. n.Op.goString(indent+2, "Op: "), place,
  1824. n.Type.goString(indent+2, "Type: "), ini)
  1825. }
  1826. // Literal is a literal in an expression.
  1827. type Literal struct {
  1828. Type AST
  1829. Val string
  1830. Neg bool
  1831. }
  1832. // Suffixes to use for constants of the given integer type.
  1833. var builtinTypeSuffix = map[string]string{
  1834. "int": "",
  1835. "unsigned int": "u",
  1836. "long": "l",
  1837. "unsigned long": "ul",
  1838. "long long": "ll",
  1839. "unsigned long long": "ull",
  1840. }
  1841. // Builtin float types.
  1842. var builtinTypeFloat = map[string]bool{
  1843. "double": true,
  1844. "long double": true,
  1845. "float": true,
  1846. "__float128": true,
  1847. "half": true,
  1848. }
  1849. func (l *Literal) print(ps *printState) {
  1850. isFloat := false
  1851. if b, ok := l.Type.(*BuiltinType); ok {
  1852. if suffix, ok := builtinTypeSuffix[b.Name]; ok {
  1853. if l.Neg {
  1854. ps.writeByte('-')
  1855. }
  1856. ps.writeString(l.Val)
  1857. ps.writeString(suffix)
  1858. return
  1859. } else if b.Name == "bool" && !l.Neg {
  1860. switch l.Val {
  1861. case "0":
  1862. ps.writeString("false")
  1863. return
  1864. case "1":
  1865. ps.writeString("true")
  1866. return
  1867. }
  1868. } else {
  1869. isFloat = builtinTypeFloat[b.Name]
  1870. }
  1871. }
  1872. ps.writeByte('(')
  1873. l.Type.print(ps)
  1874. ps.writeByte(')')
  1875. if isFloat {
  1876. ps.writeByte('[')
  1877. }
  1878. if l.Neg {
  1879. ps.writeByte('-')
  1880. }
  1881. ps.writeString(l.Val)
  1882. if isFloat {
  1883. ps.writeByte(']')
  1884. }
  1885. }
  1886. func (l *Literal) Traverse(fn func(AST) bool) {
  1887. if fn(l) {
  1888. l.Type.Traverse(fn)
  1889. }
  1890. }
  1891. func (l *Literal) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1892. if skip(l) {
  1893. return nil
  1894. }
  1895. typ := l.Type.Copy(fn, skip)
  1896. if typ == nil {
  1897. return fn(l)
  1898. }
  1899. l = &Literal{Type: typ, Val: l.Val, Neg: l.Neg}
  1900. if r := fn(l); r != nil {
  1901. return r
  1902. }
  1903. return l
  1904. }
  1905. func (l *Literal) GoString() string {
  1906. return l.goString(0, "")
  1907. }
  1908. func (l *Literal) goString(indent int, field string) string {
  1909. var neg string
  1910. if l.Neg {
  1911. neg = " Neg: true"
  1912. }
  1913. return fmt.Sprintf("%*s%sLiteral:%s\n%s\n%*sVal: %s", indent, "", field,
  1914. neg, l.Type.goString(indent+2, "Type: "),
  1915. indent+2, "", l.Val)
  1916. }
  1917. // ExprList is a list of expressions, typically arguments to a
  1918. // function call in an expression.
  1919. type ExprList struct {
  1920. Exprs []AST
  1921. }
  1922. func (el *ExprList) print(ps *printState) {
  1923. for i, e := range el.Exprs {
  1924. if i > 0 {
  1925. ps.writeString(", ")
  1926. }
  1927. e.print(ps)
  1928. }
  1929. }
  1930. func (el *ExprList) Traverse(fn func(AST) bool) {
  1931. if fn(el) {
  1932. for _, e := range el.Exprs {
  1933. e.Traverse(fn)
  1934. }
  1935. }
  1936. }
  1937. func (el *ExprList) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1938. if skip(el) {
  1939. return nil
  1940. }
  1941. exprs := make([]AST, len(el.Exprs))
  1942. changed := false
  1943. for i, e := range el.Exprs {
  1944. ec := e.Copy(fn, skip)
  1945. if ec == nil {
  1946. exprs[i] = e
  1947. } else {
  1948. exprs[i] = ec
  1949. changed = true
  1950. }
  1951. }
  1952. if !changed {
  1953. return fn(el)
  1954. }
  1955. el = &ExprList{Exprs: exprs}
  1956. if r := fn(el); r != nil {
  1957. return r
  1958. }
  1959. return el
  1960. }
  1961. func (el *ExprList) GoString() string {
  1962. return el.goString(0, "")
  1963. }
  1964. func (el *ExprList) goString(indent int, field string) string {
  1965. if len(el.Exprs) == 0 {
  1966. return fmt.Sprintf("%*s%sExprList: nil", indent, "", field)
  1967. }
  1968. s := fmt.Sprintf("%*s%sExprList:", indent, "", field)
  1969. for i, e := range el.Exprs {
  1970. s += "\n"
  1971. s += e.goString(indent+2, fmt.Sprintf("%d: ", i))
  1972. }
  1973. return s
  1974. }
  1975. // InitializerList is an initializer list: an optional type with a
  1976. // list of expressions.
  1977. type InitializerList struct {
  1978. Type AST
  1979. Exprs AST
  1980. }
  1981. func (il *InitializerList) print(ps *printState) {
  1982. if il.Type != nil {
  1983. il.Type.print(ps)
  1984. }
  1985. ps.writeByte('{')
  1986. il.Exprs.print(ps)
  1987. ps.writeByte('}')
  1988. }
  1989. func (il *InitializerList) Traverse(fn func(AST) bool) {
  1990. if fn(il) {
  1991. il.Type.Traverse(fn)
  1992. il.Exprs.Traverse(fn)
  1993. }
  1994. }
  1995. func (il *InitializerList) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  1996. if skip(il) {
  1997. return nil
  1998. }
  1999. var typ AST
  2000. if il.Type != nil {
  2001. typ = il.Type.Copy(fn, skip)
  2002. }
  2003. exprs := il.Exprs.Copy(fn, skip)
  2004. if typ == nil && exprs == nil {
  2005. return fn(il)
  2006. }
  2007. if typ == nil {
  2008. typ = il.Type
  2009. }
  2010. if exprs == nil {
  2011. exprs = il.Exprs
  2012. }
  2013. il = &InitializerList{Type: typ, Exprs: exprs}
  2014. if r := fn(il); r != nil {
  2015. return r
  2016. }
  2017. return il
  2018. }
  2019. func (il *InitializerList) GoString() string {
  2020. return il.goString(0, "")
  2021. }
  2022. func (il *InitializerList) goString(indent int, field string) string {
  2023. var t string
  2024. if il.Type == nil {
  2025. t = fmt.Sprintf("%*sType: nil", indent+2, "")
  2026. } else {
  2027. t = il.Type.goString(indent+2, "Type: ")
  2028. }
  2029. return fmt.Sprintf("%*s%sInitializerList:\n%s\n%s", indent, "", field,
  2030. t, il.Exprs.goString(indent+2, "Exprs: "))
  2031. }
  2032. // DefaultArg holds a default argument for a local name.
  2033. type DefaultArg struct {
  2034. Num int
  2035. Arg AST
  2036. }
  2037. func (da *DefaultArg) print(ps *printState) {
  2038. fmt.Fprintf(&ps.buf, "{default arg#%d}::", da.Num+1)
  2039. da.Arg.print(ps)
  2040. }
  2041. func (da *DefaultArg) Traverse(fn func(AST) bool) {
  2042. if fn(da) {
  2043. da.Arg.Traverse(fn)
  2044. }
  2045. }
  2046. func (da *DefaultArg) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  2047. if skip(da) {
  2048. return nil
  2049. }
  2050. arg := da.Arg.Copy(fn, skip)
  2051. if arg == nil {
  2052. return fn(da)
  2053. }
  2054. da = &DefaultArg{Num: da.Num, Arg: arg}
  2055. if r := fn(da); r != nil {
  2056. return r
  2057. }
  2058. return da
  2059. }
  2060. func (da *DefaultArg) GoString() string {
  2061. return da.goString(0, "")
  2062. }
  2063. func (da *DefaultArg) goString(indent int, field string) string {
  2064. return fmt.Sprintf("%*s%sDefaultArg: Num: %d\n%s", indent, "", field, da.Num,
  2065. da.Arg.goString(indent+2, "Arg: "))
  2066. }
  2067. // Closure is a closure, or lambda expression.
  2068. type Closure struct {
  2069. Types []AST
  2070. Num int
  2071. }
  2072. func (cl *Closure) print(ps *printState) {
  2073. ps.writeString("{lambda(")
  2074. for i, t := range cl.Types {
  2075. if i > 0 {
  2076. ps.writeString(", ")
  2077. }
  2078. t.print(ps)
  2079. }
  2080. ps.writeString(fmt.Sprintf(")#%d}", cl.Num+1))
  2081. }
  2082. func (cl *Closure) Traverse(fn func(AST) bool) {
  2083. if fn(cl) {
  2084. for _, t := range cl.Types {
  2085. t.Traverse(fn)
  2086. }
  2087. }
  2088. }
  2089. func (cl *Closure) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  2090. if skip(cl) {
  2091. return nil
  2092. }
  2093. types := make([]AST, len(cl.Types))
  2094. changed := false
  2095. for i, t := range cl.Types {
  2096. tc := t.Copy(fn, skip)
  2097. if tc == nil {
  2098. types[i] = t
  2099. } else {
  2100. types[i] = tc
  2101. changed = true
  2102. }
  2103. }
  2104. if !changed {
  2105. return fn(cl)
  2106. }
  2107. cl = &Closure{Types: types, Num: cl.Num}
  2108. if r := fn(cl); r != nil {
  2109. return r
  2110. }
  2111. return cl
  2112. }
  2113. func (cl *Closure) GoString() string {
  2114. return cl.goString(0, "")
  2115. }
  2116. func (cl *Closure) goString(indent int, field string) string {
  2117. var types string
  2118. if len(cl.Types) == 0 {
  2119. types = fmt.Sprintf("%*sTypes: nil", indent+2, "")
  2120. } else {
  2121. types = fmt.Sprintf("%*sTypes:", indent+2, "")
  2122. for i, t := range cl.Types {
  2123. types += "\n"
  2124. types += t.goString(indent+4, fmt.Sprintf("%d: ", i))
  2125. }
  2126. }
  2127. return fmt.Sprintf("%*s%sClosure: Num: %d\n%s", indent, "", field, cl.Num, types)
  2128. }
  2129. // UnnamedType is an unnamed type, that just has an index.
  2130. type UnnamedType struct {
  2131. Num int
  2132. }
  2133. func (ut *UnnamedType) print(ps *printState) {
  2134. ps.writeString(fmt.Sprintf("{unnamed type#%d}", ut.Num+1))
  2135. }
  2136. func (ut *UnnamedType) Traverse(fn func(AST) bool) {
  2137. fn(ut)
  2138. }
  2139. func (ut *UnnamedType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  2140. if skip(ut) {
  2141. return nil
  2142. }
  2143. return fn(ut)
  2144. }
  2145. func (ut *UnnamedType) GoString() string {
  2146. return ut.goString(0, "")
  2147. }
  2148. func (ut *UnnamedType) goString(indent int, field string) string {
  2149. return fmt.Sprintf("%*s%sUnnamedType: Num: %d", indent, "", field, ut.Num)
  2150. }
  2151. // Clone is a clone of a function, with a distinguishing suffix.
  2152. type Clone struct {
  2153. Base AST
  2154. Suffix string
  2155. }
  2156. func (c *Clone) print(ps *printState) {
  2157. c.Base.print(ps)
  2158. ps.writeString(fmt.Sprintf(" [clone %s]", c.Suffix))
  2159. }
  2160. func (c *Clone) Traverse(fn func(AST) bool) {
  2161. if fn(c) {
  2162. c.Base.Traverse(fn)
  2163. }
  2164. }
  2165. func (c *Clone) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  2166. if skip(c) {
  2167. return nil
  2168. }
  2169. base := c.Base.Copy(fn, skip)
  2170. if base == nil {
  2171. return fn(c)
  2172. }
  2173. c = &Clone{Base: base, Suffix: c.Suffix}
  2174. if r := fn(c); r != nil {
  2175. return r
  2176. }
  2177. return c
  2178. }
  2179. func (c *Clone) GoString() string {
  2180. return c.goString(0, "")
  2181. }
  2182. func (c *Clone) goString(indent int, field string) string {
  2183. return fmt.Sprintf("%*s%sClone: Suffix: %s\n%s", indent, "", field,
  2184. c.Suffix, c.Base.goString(indent+2, "Base: "))
  2185. }
  2186. // Special is a special symbol, printed as a prefix plus another
  2187. // value.
  2188. type Special struct {
  2189. Prefix string
  2190. Val AST
  2191. }
  2192. func (s *Special) print(ps *printState) {
  2193. ps.writeString(s.Prefix)
  2194. s.Val.print(ps)
  2195. }
  2196. func (s *Special) Traverse(fn func(AST) bool) {
  2197. if fn(s) {
  2198. s.Val.Traverse(fn)
  2199. }
  2200. }
  2201. func (s *Special) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  2202. if skip(s) {
  2203. return nil
  2204. }
  2205. val := s.Val.Copy(fn, skip)
  2206. if val == nil {
  2207. return fn(s)
  2208. }
  2209. s = &Special{Prefix: s.Prefix, Val: val}
  2210. if r := fn(s); r != nil {
  2211. return r
  2212. }
  2213. return s
  2214. }
  2215. func (s *Special) GoString() string {
  2216. return s.goString(0, "")
  2217. }
  2218. func (s *Special) goString(indent int, field string) string {
  2219. return fmt.Sprintf("%*s%sSpecial: Prefix: %s\n%s", indent, "", field,
  2220. s.Prefix, s.Val.goString(indent+2, "Val: "))
  2221. }
  2222. // Special2 is like special, but uses two values.
  2223. type Special2 struct {
  2224. Prefix string
  2225. Val1 AST
  2226. Middle string
  2227. Val2 AST
  2228. }
  2229. func (s *Special2) print(ps *printState) {
  2230. ps.writeString(s.Prefix)
  2231. s.Val1.print(ps)
  2232. ps.writeString(s.Middle)
  2233. s.Val2.print(ps)
  2234. }
  2235. func (s *Special2) Traverse(fn func(AST) bool) {
  2236. if fn(s) {
  2237. s.Val1.Traverse(fn)
  2238. s.Val2.Traverse(fn)
  2239. }
  2240. }
  2241. func (s *Special2) Copy(fn func(AST) AST, skip func(AST) bool) AST {
  2242. if skip(s) {
  2243. return nil
  2244. }
  2245. val1 := s.Val1.Copy(fn, skip)
  2246. val2 := s.Val2.Copy(fn, skip)
  2247. if val1 == nil && val2 == nil {
  2248. return fn(s)
  2249. }
  2250. if val1 == nil {
  2251. val1 = s.Val1
  2252. }
  2253. if val2 == nil {
  2254. val2 = s.Val2
  2255. }
  2256. s = &Special2{Prefix: s.Prefix, Val1: val1, Middle: s.Middle, Val2: val2}
  2257. if r := fn(s); r != nil {
  2258. return r
  2259. }
  2260. return s
  2261. }
  2262. func (s *Special2) GoString() string {
  2263. return s.goString(0, "")
  2264. }
  2265. func (s *Special2) goString(indent int, field string) string {
  2266. return fmt.Sprintf("%*s%sSpecial2: Prefix: %s\n%s\n%*sMiddle: %s\n%s", indent, "", field,
  2267. s.Prefix, s.Val1.goString(indent+2, "Val1: "),
  2268. indent+2, "", s.Middle, s.Val2.goString(indent+2, "Val2: "))
  2269. }
  2270. // Print the inner types.
  2271. func (ps *printState) printInner(prefixOnly bool) []AST {
  2272. var save []AST
  2273. var psave *[]AST
  2274. if prefixOnly {
  2275. psave = &save
  2276. }
  2277. for len(ps.inner) > 0 {
  2278. ps.printOneInner(psave)
  2279. }
  2280. return save
  2281. }
  2282. // innerPrinter is an interface for types that can print themselves as
  2283. // inner types.
  2284. type innerPrinter interface {
  2285. printInner(*printState)
  2286. }
  2287. // Print the most recent inner type. If save is not nil, only print
  2288. // prefixes.
  2289. func (ps *printState) printOneInner(save *[]AST) {
  2290. if len(ps.inner) == 0 {
  2291. panic("printOneInner called with no inner types")
  2292. }
  2293. ln := len(ps.inner)
  2294. a := ps.inner[ln-1]
  2295. ps.inner = ps.inner[:ln-1]
  2296. if save != nil {
  2297. if _, ok := a.(*MethodWithQualifiers); ok {
  2298. *save = append(*save, a)
  2299. return
  2300. }
  2301. }
  2302. if ip, ok := a.(innerPrinter); ok {
  2303. ip.printInner(ps)
  2304. } else {
  2305. a.print(ps)
  2306. }
  2307. }
  2308. // isEmpty returns whether printing a will not print anything.
  2309. func (ps *printState) isEmpty(a AST) bool {
  2310. switch a := a.(type) {
  2311. case *ArgumentPack:
  2312. return len(a.Args) == 0
  2313. case *ExprList:
  2314. return len(a.Exprs) == 0
  2315. case *PackExpansion:
  2316. return a.Pack != nil && ps.isEmpty(a.Base)
  2317. default:
  2318. return false
  2319. }
  2320. }