-
Notifications
You must be signed in to change notification settings - Fork 24
/
ast.go
5256 lines (4653 loc) · 106 KB
/
ast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package demangle
import (
"fmt"
"strings"
)
// AST is an abstract syntax tree representing a C++ declaration.
// This is sufficient for the demangler but is by no means a general C++ AST.
// This abstract syntax tree is only used for C++ symbols, not Rust symbols.
type AST interface {
// Internal method to convert to demangled string.
print(*printState)
// Traverse each element of an AST. If the function returns
// false, traversal of children of that element is skipped.
Traverse(func(AST) bool)
// Copy an AST with possible transformations.
// If the skip function returns true, no copy is required.
// If the copy function returns nil, no copy is required.
// The Copy method will do the right thing if copy returns nil
// for some components of an AST but not others, so a good
// copy function will only return non-nil for AST values that
// need to change.
// Copy itself returns either a copy or nil.
Copy(copy func(AST) AST, skip func(AST) bool) AST
// Implement the fmt.GoStringer interface.
GoString() string
goString(indent int, field string) string
}
// ASTToString returns the demangled name of the AST.
func ASTToString(a AST, options ...Option) string {
tparams := true
enclosingParams := true
llvmStyle := false
max := 0
for _, o := range options {
switch {
case o == NoTemplateParams:
tparams = false
case o == NoEnclosingParams:
enclosingParams = false
case o == LLVMStyle:
llvmStyle = true
case isMaxLength(o):
max = maxLength(o)
}
}
ps := printState{
tparams: tparams,
enclosingParams: enclosingParams,
llvmStyle: llvmStyle,
max: max,
scopes: 1,
}
a.print(&ps)
s := ps.buf.String()
if max > 0 && len(s) > max {
s = s[:max]
}
return s
}
// The printState type holds information needed to print an AST.
type printState struct {
tparams bool // whether to print template parameters
enclosingParams bool // whether to print enclosing parameters
llvmStyle bool
max int // maximum output length
// The scopes field is used to avoid unnecessary parentheses
// around expressions that use > (or >>). It is incremented if
// we output a parenthesis or something else that means that >
// or >> won't be treated as ending a template. It starts out
// as 1, and is set to 0 when we start writing template
// arguments. We add parentheses around expressions using > if
// scopes is 0. The effect is that an expression with > gets
// parentheses if used as a template argument that is not
// inside some other set of parentheses.
scopes int
buf strings.Builder
last byte // Last byte written to buffer.
// The inner field is a list of items to print for a type
// name. This is used by types to implement the inside-out
// C++ declaration syntax.
inner []AST
// The printing field is a list of items we are currently
// printing. This avoids endless recursion if a substitution
// reference creates a cycle in the graph.
printing []AST
}
// writeByte adds a byte to the string being printed.
func (ps *printState) writeByte(b byte) {
ps.last = b
ps.buf.WriteByte(b)
}
// writeString adds a string to the string being printed.
func (ps *printState) writeString(s string) {
if len(s) > 0 {
ps.last = s[len(s)-1]
}
ps.buf.WriteString(s)
}
// Print an AST.
func (ps *printState) print(a AST) {
if ps.max > 0 && ps.buf.Len() > ps.max {
return
}
c := 0
for _, v := range ps.printing {
if v == a {
// We permit the type to appear once, and
// return without printing anything if we see
// it twice. This is for a case like
// _Z6outer2IsEPFilES1_, where the
// substitution is printed differently the
// second time because the set of inner types
// is different.
c++
if c > 1 {
return
}
}
}
ps.printing = append(ps.printing, a)
a.print(ps)
ps.printing = ps.printing[:len(ps.printing)-1]
}
// printList prints a list of AST values separated by commas,
// optionally skipping some.
func (ps *printState) printList(args []AST, skip func(AST) bool) {
first := true
for _, a := range args {
if skip != nil && skip(a) {
continue
}
if !first {
ps.writeString(", ")
}
needsParen := false
if ps.llvmStyle {
if p, ok := a.(hasPrec); ok {
if p.prec() >= precComma {
needsParen = true
}
}
}
if needsParen {
ps.startScope('(')
}
ps.print(a)
if needsParen {
ps.endScope(')')
}
first = false
}
}
// startScope starts a scope. This is used to decide whether we need
// to parenthesize an expression using > or >>.
func (ps *printState) startScope(b byte) {
ps.scopes++
ps.writeByte(b)
}
// endScope closes a scope.
func (ps *printState) endScope(b byte) {
ps.scopes--
ps.writeByte(b)
}
// precedence is used for operator precedence. This is used to avoid
// unnecessary parentheses when printing expressions in the LLVM style.
type precedence int
// The precedence values, in order from high to low.
const (
precPrimary precedence = iota
precPostfix
precUnary
precCast
precPtrMem
precMul
precAdd
precShift
precSpaceship
precRel
precEqual
precAnd
precXor
precOr
precLogicalAnd
precLogicalOr
precCond
precAssign
precComma
precDefault
)
// hasPrec matches the AST nodes that have a prec method that returns
// the node's precedence.
type hasPrec interface {
prec() precedence
}
// Name is an unqualified name.
type Name struct {
Name string
}
func (n *Name) print(ps *printState) {
ps.writeString(n.Name)
}
func (n *Name) Traverse(fn func(AST) bool) {
fn(n)
}
func (n *Name) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(n) {
return nil
}
return fn(n)
}
func (n *Name) GoString() string {
return n.goString(0, "Name: ")
}
func (n *Name) goString(indent int, field string) string {
return fmt.Sprintf("%*s%s%s", indent, "", field, n.Name)
}
func (n *Name) prec() precedence {
return precPrimary
}
// Typed is a typed name.
type Typed struct {
Name AST
Type AST
}
func (t *Typed) print(ps *printState) {
// We are printing a typed name, so ignore the current set of
// inner names to print. Pass down our name as the one to use.
holdInner := ps.inner
defer func() { ps.inner = holdInner }()
ps.inner = []AST{t}
ps.print(t.Type)
if len(ps.inner) > 0 {
// The type did not print the name; print it now in
// the default location.
ps.writeByte(' ')
ps.print(t.Name)
}
}
func (t *Typed) printInner(ps *printState) {
ps.print(t.Name)
}
func (t *Typed) Traverse(fn func(AST) bool) {
if fn(t) {
t.Name.Traverse(fn)
t.Type.Traverse(fn)
}
}
func (t *Typed) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(t) {
return nil
}
name := t.Name.Copy(fn, skip)
typ := t.Type.Copy(fn, skip)
if name == nil && typ == nil {
return fn(t)
}
if name == nil {
name = t.Name
}
if typ == nil {
typ = t.Type
}
t = &Typed{Name: name, Type: typ}
if r := fn(t); r != nil {
return r
}
return t
}
func (t *Typed) GoString() string {
return t.goString(0, "")
}
func (t *Typed) goString(indent int, field string) string {
return fmt.Sprintf("%*s%sTyped:\n%s\n%s", indent, "", field,
t.Name.goString(indent+2, "Name: "),
t.Type.goString(indent+2, "Type: "))
}
// Qualified is a name in a scope.
type Qualified struct {
Scope AST
Name AST
// The LocalName field is true if this is parsed as a
// <local-name>. We shouldn't really need this, but in some
// cases (for the unary sizeof operator) the standard
// demangler prints a local name slightly differently. We
// keep track of this for compatibility.
LocalName bool // A full local name encoding
}
func (q *Qualified) print(ps *printState) {
ps.print(q.Scope)
ps.writeString("::")
ps.print(q.Name)
}
func (q *Qualified) Traverse(fn func(AST) bool) {
if fn(q) {
q.Scope.Traverse(fn)
q.Name.Traverse(fn)
}
}
func (q *Qualified) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(q) {
return nil
}
scope := q.Scope.Copy(fn, skip)
name := q.Name.Copy(fn, skip)
if scope == nil && name == nil {
return fn(q)
}
if scope == nil {
scope = q.Scope
}
if name == nil {
name = q.Name
}
q = &Qualified{Scope: scope, Name: name, LocalName: q.LocalName}
if r := fn(q); r != nil {
return r
}
return q
}
func (q *Qualified) GoString() string {
return q.goString(0, "")
}
func (q *Qualified) goString(indent int, field string) string {
s := ""
if q.LocalName {
s = " LocalName: true"
}
return fmt.Sprintf("%*s%sQualified:%s\n%s\n%s", indent, "", field,
s, q.Scope.goString(indent+2, "Scope: "),
q.Name.goString(indent+2, "Name: "))
}
func (q *Qualified) prec() precedence {
return precPrimary
}
// Template is a template with arguments.
type Template struct {
Name AST
Args []AST
}
func (t *Template) print(ps *printState) {
// Inner types apply to the template as a whole, they don't
// cross over into the template.
holdInner := ps.inner
defer func() { ps.inner = holdInner }()
ps.inner = nil
ps.print(t.Name)
if !ps.tparams {
// Do not print template parameters.
return
}
// We need an extra space after operator<.
if ps.last == '<' {
ps.writeByte(' ')
}
scopes := ps.scopes
ps.scopes = 0
ps.writeByte('<')
ps.printList(t.Args, ps.isEmpty)
if ps.last == '>' && !ps.llvmStyle {
// Avoid syntactic ambiguity in old versions of C++.
ps.writeByte(' ')
}
ps.writeByte('>')
ps.scopes = scopes
}
func (t *Template) Traverse(fn func(AST) bool) {
if fn(t) {
t.Name.Traverse(fn)
for _, a := range t.Args {
a.Traverse(fn)
}
}
}
func (t *Template) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(t) {
return nil
}
name := t.Name.Copy(fn, skip)
changed := name != nil
args := make([]AST, len(t.Args))
for i, a := range t.Args {
ac := a.Copy(fn, skip)
if ac == nil {
args[i] = a
} else {
args[i] = ac
changed = true
}
}
if !changed {
return fn(t)
}
if name == nil {
name = t.Name
}
t = &Template{Name: name, Args: args}
if r := fn(t); r != nil {
return r
}
return t
}
func (t *Template) GoString() string {
return t.goString(0, "")
}
func (t *Template) goString(indent int, field string) string {
var args string
if len(t.Args) == 0 {
args = fmt.Sprintf("%*sArgs: nil", indent+2, "")
} else {
args = fmt.Sprintf("%*sArgs:", indent+2, "")
for i, a := range t.Args {
args += "\n"
args += a.goString(indent+4, fmt.Sprintf("%d: ", i))
}
}
return fmt.Sprintf("%*s%sTemplate (%p):\n%s\n%s", indent, "", field, t,
t.Name.goString(indent+2, "Name: "), args)
}
// TemplateParam is a template parameter. The Template field is
// filled in while parsing the demangled string. We don't normally
// see these while printing--they are replaced by the simplify
// function.
type TemplateParam struct {
Index int
Template *Template
}
func (tp *TemplateParam) print(ps *printState) {
if tp.Template == nil {
panic("TemplateParam Template field is nil")
}
if tp.Index >= len(tp.Template.Args) {
panic("TemplateParam Index out of bounds")
}
ps.print(tp.Template.Args[tp.Index])
}
func (tp *TemplateParam) Traverse(fn func(AST) bool) {
fn(tp)
// Don't traverse Template--it points elsewhere in the AST.
}
func (tp *TemplateParam) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(tp) {
return nil
}
return fn(tp)
}
func (tp *TemplateParam) GoString() string {
return tp.goString(0, "")
}
func (tp *TemplateParam) goString(indent int, field string) string {
return fmt.Sprintf("%*s%sTemplateParam: Template: %p; Index %d", indent, "", field, tp.Template, tp.Index)
}
// LambdaAuto is a lambda auto parameter.
type LambdaAuto struct {
Index int
}
func (la *LambdaAuto) print(ps *printState) {
// We print the index plus 1 because that is what the standard
// demangler does.
if ps.llvmStyle {
ps.writeString("auto")
} else {
fmt.Fprintf(&ps.buf, "auto:%d", la.Index+1)
}
}
func (la *LambdaAuto) Traverse(fn func(AST) bool) {
fn(la)
}
func (la *LambdaAuto) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(la) {
return nil
}
return fn(la)
}
func (la *LambdaAuto) GoString() string {
return la.goString(0, "")
}
func (la *LambdaAuto) goString(indent int, field string) string {
return fmt.Sprintf("%*s%sLambdaAuto: Index %d", indent, "", field, la.Index)
}
// TemplateParamQualifiedArg is used when the mangled name includes
// both the template parameter declaration and the template argument.
// See https://github.com/itanium-cxx-abi/cxx-abi/issues/47.
type TemplateParamQualifiedArg struct {
Param AST
Arg AST
}
func (tpqa *TemplateParamQualifiedArg) print(ps *printState) {
// We only demangle the actual template argument.
// That is what the LLVM demangler does.
// The parameter disambiguates the argument,
// but is hopefully not required by a human reader.
ps.print(tpqa.Arg)
}
func (tpqa *TemplateParamQualifiedArg) Traverse(fn func(AST) bool) {
if fn(tpqa) {
tpqa.Param.Traverse(fn)
tpqa.Arg.Traverse(fn)
}
}
func (tpqa *TemplateParamQualifiedArg) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(tpqa) {
return nil
}
param := tpqa.Param.Copy(fn, skip)
arg := tpqa.Arg.Copy(fn, skip)
if param == nil && arg == nil {
return fn(tpqa)
}
if param == nil {
param = tpqa.Param
}
if arg == nil {
arg = tpqa.Arg
}
tpqa = &TemplateParamQualifiedArg{Param: param, Arg: arg}
if r := fn(tpqa); r != nil {
return r
}
return tpqa
}
func (tpqa *TemplateParamQualifiedArg) GoString() string {
return tpqa.goString(0, "")
}
func (tpqa *TemplateParamQualifiedArg) goString(indent int, field string) string {
return fmt.Sprintf("%*s%sTemplateParamQualifiedArg:\n%s\n%s", indent, "", field,
tpqa.Param.goString(indent+2, "Param: "),
tpqa.Arg.goString(indent+2, "Arg: "))
}
// Qualifiers is an ordered list of type qualifiers.
type Qualifiers struct {
Qualifiers []AST
}
func (qs *Qualifiers) print(ps *printState) {
first := true
for _, q := range qs.Qualifiers {
if !first {
ps.writeByte(' ')
}
q.print(ps)
first = false
}
}
func (qs *Qualifiers) Traverse(fn func(AST) bool) {
if fn(qs) {
for _, q := range qs.Qualifiers {
q.Traverse(fn)
}
}
}
func (qs *Qualifiers) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(qs) {
return nil
}
changed := false
qualifiers := make([]AST, len(qs.Qualifiers))
for i, q := range qs.Qualifiers {
qc := q.Copy(fn, skip)
if qc == nil {
qualifiers[i] = q
} else {
qualifiers[i] = qc
changed = true
}
}
if !changed {
return fn(qs)
}
qs = &Qualifiers{Qualifiers: qualifiers}
if r := fn(qs); r != nil {
return r
}
return qs
}
func (qs *Qualifiers) GoString() string {
return qs.goString(0, "")
}
func (qs *Qualifiers) goString(indent int, field string) string {
quals := fmt.Sprintf("%*s%s", indent, "", field)
for _, q := range qs.Qualifiers {
quals += "\n"
quals += q.goString(indent+2, "")
}
return quals
}
// Qualifier is a single type qualifier.
type Qualifier struct {
Name string // qualifier name: const, volatile, etc.
Exprs []AST // can be non-nil for noexcept and throw
}
func (q *Qualifier) print(ps *printState) {
ps.writeString(q.Name)
if len(q.Exprs) > 0 {
ps.startScope('(')
first := true
for _, e := range q.Exprs {
if el, ok := e.(*ExprList); ok && len(el.Exprs) == 0 {
continue
}
if !first {
ps.writeString(", ")
}
ps.print(e)
first = false
}
ps.endScope(')')
}
}
func (q *Qualifier) Traverse(fn func(AST) bool) {
if fn(q) {
for _, e := range q.Exprs {
e.Traverse(fn)
}
}
}
func (q *Qualifier) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(q) {
return nil
}
exprs := make([]AST, len(q.Exprs))
changed := false
for i, e := range q.Exprs {
ec := e.Copy(fn, skip)
if ec == nil {
exprs[i] = e
} else {
exprs[i] = ec
changed = true
}
}
if !changed {
return fn(q)
}
q = &Qualifier{Name: q.Name, Exprs: exprs}
if r := fn(q); r != nil {
return r
}
return q
}
func (q *Qualifier) GoString() string {
return q.goString(0, "Qualifier: ")
}
func (q *Qualifier) goString(indent int, field string) string {
qs := fmt.Sprintf("%*s%s%s", indent, "", field, q.Name)
if len(q.Exprs) > 0 {
for i, e := range q.Exprs {
qs += "\n"
qs += e.goString(indent+2, fmt.Sprintf("%d: ", i))
}
}
return qs
}
// TypeWithQualifiers is a type with standard qualifiers.
type TypeWithQualifiers struct {
Base AST
Qualifiers AST
}
func (twq *TypeWithQualifiers) print(ps *printState) {
// Give the base type a chance to print the inner types.
ps.inner = append(ps.inner, twq)
ps.print(twq.Base)
if len(ps.inner) > 0 {
// The qualifier wasn't printed by Base.
ps.writeByte(' ')
ps.print(twq.Qualifiers)
ps.inner = ps.inner[:len(ps.inner)-1]
}
}
// Print qualifiers as an inner type by just printing the qualifiers.
func (twq *TypeWithQualifiers) printInner(ps *printState) {
ps.writeByte(' ')
ps.print(twq.Qualifiers)
}
func (twq *TypeWithQualifiers) Traverse(fn func(AST) bool) {
if fn(twq) {
twq.Base.Traverse(fn)
}
}
func (twq *TypeWithQualifiers) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(twq) {
return nil
}
base := twq.Base.Copy(fn, skip)
quals := twq.Qualifiers.Copy(fn, skip)
if base == nil && quals == nil {
return fn(twq)
}
if base == nil {
base = twq.Base
}
if quals == nil {
quals = twq.Qualifiers
}
twq = &TypeWithQualifiers{Base: base, Qualifiers: quals}
if r := fn(twq); r != nil {
return r
}
return twq
}
func (twq *TypeWithQualifiers) GoString() string {
return twq.goString(0, "")
}
func (twq *TypeWithQualifiers) goString(indent int, field string) string {
return fmt.Sprintf("%*s%sTypeWithQualifiers:\n%s\n%s", indent, "", field,
twq.Qualifiers.goString(indent+2, "Qualifiers: "),
twq.Base.goString(indent+2, "Base: "))
}
// MethodWithQualifiers is a method with qualifiers.
type MethodWithQualifiers struct {
Method AST
Qualifiers AST
RefQualifier string // "" or "&" or "&&"
}
func (mwq *MethodWithQualifiers) print(ps *printState) {
// Give the base type a chance to print the inner types.
ps.inner = append(ps.inner, mwq)
ps.print(mwq.Method)
if len(ps.inner) > 0 {
if mwq.Qualifiers != nil {
ps.writeByte(' ')
ps.print(mwq.Qualifiers)
}
if mwq.RefQualifier != "" {
ps.writeByte(' ')
ps.writeString(mwq.RefQualifier)
}
ps.inner = ps.inner[:len(ps.inner)-1]
}
}
func (mwq *MethodWithQualifiers) printInner(ps *printState) {
if mwq.Qualifiers != nil {
ps.writeByte(' ')
ps.print(mwq.Qualifiers)
}
if mwq.RefQualifier != "" {
ps.writeByte(' ')
ps.writeString(mwq.RefQualifier)
}
}
func (mwq *MethodWithQualifiers) Traverse(fn func(AST) bool) {
if fn(mwq) {
mwq.Method.Traverse(fn)
}
}
func (mwq *MethodWithQualifiers) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(mwq) {
return nil
}
method := mwq.Method.Copy(fn, skip)
var quals AST
if mwq.Qualifiers != nil {
quals = mwq.Qualifiers.Copy(fn, skip)
}
if method == nil && quals == nil {
return fn(mwq)
}
if method == nil {
method = mwq.Method
}
if quals == nil {
quals = mwq.Qualifiers
}
mwq = &MethodWithQualifiers{Method: method, Qualifiers: quals, RefQualifier: mwq.RefQualifier}
if r := fn(mwq); r != nil {
return r
}
return mwq
}
func (mwq *MethodWithQualifiers) GoString() string {
return mwq.goString(0, "")
}
func (mwq *MethodWithQualifiers) goString(indent int, field string) string {
var q string
if mwq.Qualifiers != nil {
q += "\n" + mwq.Qualifiers.goString(indent+2, "Qualifiers: ")
}
if mwq.RefQualifier != "" {
if q != "" {
q += "\n"
}
q += fmt.Sprintf("%*s%s%s", indent+2, "", "RefQualifier: ", mwq.RefQualifier)
}
return fmt.Sprintf("%*s%sMethodWithQualifiers:%s\n%s", indent, "", field,
q, mwq.Method.goString(indent+2, "Method: "))
}
// BuiltinType is a builtin type, like "int".
type BuiltinType struct {
Name string
}
func (bt *BuiltinType) print(ps *printState) {
name := bt.Name
if ps.llvmStyle && name == "decltype(nullptr)" {
name = "std::nullptr_t"
}
ps.writeString(name)
}
func (bt *BuiltinType) Traverse(fn func(AST) bool) {
fn(bt)
}
func (bt *BuiltinType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(bt) {
return nil
}
return fn(bt)
}
func (bt *BuiltinType) GoString() string {
return bt.goString(0, "")
}
func (bt *BuiltinType) goString(indent int, field string) string {
return fmt.Sprintf("%*s%sBuiltinType: %s", indent, "", field, bt.Name)
}
func (bt *BuiltinType) prec() precedence {
return precPrimary
}
// printBase is common print code for types that are printed with a
// simple suffix.
func printBase(ps *printState, qual, base AST) {
ps.inner = append(ps.inner, qual)
ps.print(base)
if len(ps.inner) > 0 {
qual.(innerPrinter).printInner(ps)
ps.inner = ps.inner[:len(ps.inner)-1]
}
}
// PointerType is a pointer type.
type PointerType struct {
Base AST
}
func (pt *PointerType) print(ps *printState) {
printBase(ps, pt, pt.Base)
}
func (pt *PointerType) printInner(ps *printState) {
ps.writeString("*")
}
func (pt *PointerType) Traverse(fn func(AST) bool) {
if fn(pt) {
pt.Base.Traverse(fn)
}
}
func (pt *PointerType) Copy(fn func(AST) AST, skip func(AST) bool) AST {
if skip(pt) {
return nil
}
base := pt.Base.Copy(fn, skip)
if base == nil {
return fn(pt)
}
pt = &PointerType{Base: base}
if r := fn(pt); r != nil {
return r
}
return pt
}
func (pt *PointerType) GoString() string {
return pt.goString(0, "")
}
func (pt *PointerType) goString(indent int, field string) string {
return fmt.Sprintf("%*s%sPointerType:\n%s", indent, "", field,
pt.Base.goString(indent+2, ""))
}
// ReferenceType is a reference type.
type ReferenceType struct {
Base AST
}
func (rt *ReferenceType) print(ps *printState) {
printBase(ps, rt, rt.Base)
}
func (rt *ReferenceType) printInner(ps *printState) {
ps.writeString("&")
}
func (rt *ReferenceType) Traverse(fn func(AST) bool) {
if fn(rt) {