-
Notifications
You must be signed in to change notification settings - Fork 24
/
demangle.go
3700 lines (3442 loc) · 94.5 KB
/
demangle.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 defines functions that demangle GCC/LLVM
// C++ and Rust symbol names.
// This package recognizes names that were mangled according to the C++ ABI
// defined at http://codesourcery.com/cxx-abi/ and the Rust ABI
// defined at
// https://rust-lang.github.io/rfcs/2603-rust-symbol-name-mangling-v0.html
//
// Most programs will want to call Filter or ToString.
package demangle
import (
"errors"
"fmt"
"strings"
)
// ErrNotMangledName is returned by CheckedDemangle if the string does
// not appear to be a C++ symbol name.
var ErrNotMangledName = errors.New("not a C++ or Rust mangled name")
// Option is the type of demangler options.
type Option int
const (
// The NoParams option disables demangling of function parameters.
// It only omits the parameters of the function name being demangled,
// not the parameter types of other functions that may be mentioned.
// Using the option will speed up the demangler and cause it to
// use less memory.
NoParams Option = iota
// The NoTemplateParams option disables demangling of template parameters.
// This applies to both C++ and Rust.
NoTemplateParams
// The NoEnclosingParams option disables demangling of the function
// parameter types of the enclosing function when demangling a
// local name defined within a function.
NoEnclosingParams
// The NoClones option disables inclusion of clone suffixes.
// NoParams implies NoClones.
NoClones
// The NoRust option disables demangling of old-style Rust
// mangled names, which can be confused with C++ style mangled
// names. New style Rust mangled names are still recognized.
NoRust
// The Verbose option turns on more verbose demangling.
Verbose
// LLVMStyle tries to translate an AST to a string in the
// style of the LLVM demangler. This does not affect
// the parsing of the AST, only the conversion of the AST
// to a string.
LLVMStyle
)
// maxLengthShift is how we shift the MaxLength value.
const maxLengthShift = 16
// maxLengthMask is a mask for the maxLength value.
const maxLengthMask = 0x1f << maxLengthShift
// MaxLength returns an Option that limits the maximum length of a
// demangled string. The maximum length is expressed as a power of 2,
// so a value of 1 limits the returned string to 2 characters, and
// a value of 16 limits the returned string to 65,536 characters.
// The value must be between 1 and 30.
func MaxLength(pow int) Option {
if pow <= 0 || pow > 30 {
panic("demangle: invalid MaxLength value")
}
return Option(pow << maxLengthShift)
}
// isMaxLength reports whether an Option holds a maximum length.
func isMaxLength(opt Option) bool {
return opt&maxLengthMask != 0
}
// maxLength returns the maximum length stored in an Option.
func maxLength(opt Option) int {
return 1 << ((opt & maxLengthMask) >> maxLengthShift)
}
// Filter demangles a C++ or Rust symbol name,
// returning the human-readable C++ or Rust name.
// If any error occurs during demangling, the input string is returned.
func Filter(name string, options ...Option) string {
ret, err := ToString(name, options...)
if err != nil {
return name
}
return ret
}
// ToString demangles a C++ or Rust symbol name,
// returning a human-readable C++ or Rust name or an error.
// If the name does not appear to be a C++ or Rust symbol name at all,
// the error will be ErrNotMangledName.
func ToString(name string, options ...Option) (string, error) {
if strings.HasPrefix(name, "_R") {
return rustToString(name, options)
}
// Check for an old-style Rust mangled name.
// It starts with _ZN and ends with "17h" followed by 16 hex digits
// followed by "E" followed by an optional suffix starting with "."
// (which we ignore).
if strings.HasPrefix(name, "_ZN") {
rname := name
if pos := strings.LastIndex(rname, "E."); pos > 0 {
rname = rname[:pos+1]
}
if strings.HasSuffix(rname, "E") && len(rname) > 23 && rname[len(rname)-20:len(rname)-17] == "17h" {
noRust := false
for _, o := range options {
if o == NoRust {
noRust = true
break
}
}
if !noRust {
s, ok := oldRustToString(rname, options)
if ok {
return s, nil
}
}
}
}
a, err := ToAST(name, options...)
if err != nil {
return "", err
}
return ASTToString(a, options...), nil
}
// ToAST demangles a C++ symbol name into an abstract syntax tree
// representing the symbol.
// If the NoParams option is passed, and the name has a function type,
// the parameter types are not demangled.
// If the name does not appear to be a C++ symbol name at all, the
// error will be ErrNotMangledName.
// This function does not currently support Rust symbol names.
func ToAST(name string, options ...Option) (AST, error) {
if strings.HasPrefix(name, "_Z") {
a, err := doDemangle(name[2:], options...)
return a, adjustErr(err, 2)
}
if strings.HasPrefix(name, "___Z") {
// clang extensions
block := strings.LastIndex(name, "_block_invoke")
if block == -1 {
return nil, ErrNotMangledName
}
a, err := doDemangle(name[4:block], options...)
if err != nil {
return a, adjustErr(err, 4)
}
name = strings.TrimPrefix(name[block:], "_block_invoke")
if len(name) > 0 && name[0] == '_' {
name = name[1:]
}
for len(name) > 0 && isDigit(name[0]) {
name = name[1:]
}
if len(name) > 0 && name[0] != '.' {
return nil, errors.New("unparsed characters at end of mangled name")
}
a = &Special{Prefix: "invocation function for block in ", Val: a}
return a, nil
}
const prefix = "_GLOBAL_"
if strings.HasPrefix(name, prefix) {
// The standard demangler ignores NoParams for global
// constructors. We are compatible.
i := 0
for i < len(options) {
if options[i] == NoParams {
options = append(options[:i], options[i+1:]...)
} else {
i++
}
}
a, err := globalCDtorName(name[len(prefix):], options...)
return a, adjustErr(err, len(prefix))
}
return nil, ErrNotMangledName
}
// globalCDtorName demangles a global constructor/destructor symbol name.
// The parameter is the string following the "_GLOBAL_" prefix.
func globalCDtorName(name string, options ...Option) (AST, error) {
if len(name) < 4 {
return nil, ErrNotMangledName
}
switch name[0] {
case '.', '_', '$':
default:
return nil, ErrNotMangledName
}
var ctor bool
switch name[1] {
case 'I':
ctor = true
case 'D':
ctor = false
default:
return nil, ErrNotMangledName
}
if name[2] != '_' {
return nil, ErrNotMangledName
}
if !strings.HasPrefix(name[3:], "_Z") {
return &GlobalCDtor{Ctor: ctor, Key: &Name{Name: name}}, nil
} else {
a, err := doDemangle(name[5:], options...)
if err != nil {
return nil, adjustErr(err, 5)
}
return &GlobalCDtor{Ctor: ctor, Key: a}, nil
}
}
// The doDemangle function is the entry point into the demangler proper.
func doDemangle(name string, options ...Option) (ret AST, err error) {
// When the demangling routines encounter an error, they panic
// with a value of type demangleErr.
defer func() {
if r := recover(); r != nil {
if de, ok := r.(demangleErr); ok {
ret = nil
err = de
return
}
panic(r)
}
}()
params := true
clones := true
verbose := false
for _, o := range options {
switch {
case o == NoParams:
params = false
clones = false
case o == NoClones:
clones = false
case o == Verbose:
verbose = true
case o == NoTemplateParams || o == NoEnclosingParams || o == LLVMStyle || isMaxLength(o):
// These are valid options but only affect
// printing of the AST.
case o == NoRust:
// Unimportant here.
default:
return nil, fmt.Errorf("unrecognized demangler option %v", o)
}
}
st := &state{str: name, verbose: verbose}
a := st.encoding(params, notForLocalName)
// Accept a clone suffix.
if clones {
for len(st.str) > 1 && st.str[0] == '.' && (isLower(st.str[1]) || st.str[1] == '_' || isDigit(st.str[1])) {
a = st.cloneSuffix(a)
}
}
if clones && len(st.str) > 0 {
st.fail("unparsed characters at end of mangled name")
}
return a, nil
}
// A state holds the current state of demangling a string.
type state struct {
str string // remainder of string to demangle
verbose bool // whether to use verbose demangling
off int // offset of str within original string
subs substitutions // substitutions
templates []*Template // templates being processed
// The number of entries in templates when we started parsing
// a lambda, plus 1 so that 0 means not parsing a lambda.
lambdaTemplateLevel int
parsingConstraint bool // whether parsing a constraint expression
// Counts of template parameters without template arguments,
// for lambdas.
typeTemplateParamCount int
nonTypeTemplateParamCount int
templateTemplateParamCount int
}
// copy returns a copy of the current state.
func (st *state) copy() *state {
n := new(state)
*n = *st
return n
}
// fail panics with demangleErr, to be caught in doDemangle.
func (st *state) fail(err string) {
panic(demangleErr{err: err, off: st.off})
}
// failEarlier is like fail, but decrements the offset to indicate
// that the point of failure occurred earlier in the string.
func (st *state) failEarlier(err string, dec int) {
if st.off < dec {
panic("internal error")
}
panic(demangleErr{err: err, off: st.off - dec})
}
// advance advances the current string offset.
func (st *state) advance(add int) {
if len(st.str) < add {
panic("internal error")
}
st.str = st.str[add:]
st.off += add
}
// checkChar requires that the next character in the string be c, and
// advances past it.
func (st *state) checkChar(c byte) {
if len(st.str) == 0 || st.str[0] != c {
panic("internal error")
}
st.advance(1)
}
// A demangleErr is an error at a specific offset in the mangled
// string.
type demangleErr struct {
err string
off int
}
// Error implements the builtin error interface for demangleErr.
func (de demangleErr) Error() string {
return fmt.Sprintf("%s at %d", de.err, de.off)
}
// adjustErr adjusts the position of err, if it is a demangleErr,
// and returns err.
func adjustErr(err error, adj int) error {
if err == nil {
return nil
}
if de, ok := err.(demangleErr); ok {
de.off += adj
return de
}
return err
}
type forLocalNameType int
const (
forLocalName forLocalNameType = iota
notForLocalName
)
// encoding parses:
//
// encoding ::= <(function) name> <bare-function-type>
// <(data) name>
// <special-name>
func (st *state) encoding(params bool, local forLocalNameType) AST {
if len(st.str) < 1 {
st.fail("expected encoding")
}
if st.str[0] == 'G' || st.str[0] == 'T' {
return st.specialName()
}
a, explicitObjectParameter := st.name()
a = simplify(a)
if !params {
// Don't demangle the parameters.
// Strip CV-qualifiers, as they apply to the 'this'
// parameter, and are not output by the standard
// demangler without parameters.
if mwq, ok := a.(*MethodWithQualifiers); ok {
a = mwq.Method
}
// If this is a local name, there may be CV-qualifiers
// on the name that really apply to the top level, and
// therefore must be discarded when discarding
// parameters. This can happen when parsing a class
// that is local to a function.
if q, ok := a.(*Qualified); ok && q.LocalName {
p := &q.Name
if da, ok := (*p).(*DefaultArg); ok {
p = &da.Arg
}
if mwq, ok := (*p).(*MethodWithQualifiers); ok {
*p = mwq.Method
}
}
return a
}
if len(st.str) == 0 || st.str[0] == 'E' {
// There are no parameters--this is a data symbol, not
// a function symbol.
return a
}
mwq, _ := a.(*MethodWithQualifiers)
var findTemplate func(AST) *Template
findTemplate = func(check AST) *Template {
switch check := check.(type) {
case *Template:
return check
case *Qualified:
if check.LocalName {
return findTemplate(check.Name)
} else if _, ok := check.Name.(*Constructor); ok {
return findTemplate(check.Name)
}
case *MethodWithQualifiers:
return findTemplate(check.Method)
case *Constructor:
if check.Base != nil {
return findTemplate(check.Base)
}
}
return nil
}
template := findTemplate(a)
var oldLambdaTemplateLevel int
if template != nil {
st.templates = append(st.templates, template)
oldLambdaTemplateLevel = st.lambdaTemplateLevel
st.lambdaTemplateLevel = 0
}
// Checking for the enable_if attribute here is what the LLVM
// demangler does. This is not very general but perhaps it is
// sufficient.
const enableIfPrefix = "Ua9enable_ifI"
var enableIfArgs []AST
if strings.HasPrefix(st.str, enableIfPrefix) {
st.advance(len(enableIfPrefix) - 1)
enableIfArgs = st.templateArgs()
}
ft := st.bareFunctionType(hasReturnType(a), explicitObjectParameter)
var constraint AST
if len(st.str) > 0 && st.str[0] == 'Q' {
constraint = st.constraintExpr()
}
if template != nil {
st.templates = st.templates[:len(st.templates)-1]
st.lambdaTemplateLevel = oldLambdaTemplateLevel
}
ft = simplify(ft)
// For a local name, discard the return type, so that it
// doesn't get confused with the top level return type.
if local == forLocalName {
if functype, ok := ft.(*FunctionType); ok {
functype.ForLocalName = true
}
}
// Any top-level qualifiers belong to the function type.
if mwq != nil {
a = mwq.Method
mwq.Method = ft
ft = mwq
}
if q, ok := a.(*Qualified); ok && q.LocalName {
p := &q.Name
if da, ok := (*p).(*DefaultArg); ok {
p = &da.Arg
}
if mwq, ok := (*p).(*MethodWithQualifiers); ok {
*p = mwq.Method
mwq.Method = ft
ft = mwq
}
}
r := AST(&Typed{Name: a, Type: ft})
if len(enableIfArgs) > 0 {
r = &EnableIf{Type: r, Args: enableIfArgs}
}
if constraint != nil {
r = &Constraint{Name: r, Requires: constraint}
}
return r
}
// hasReturnType returns whether the mangled form of a will have a
// return type.
func hasReturnType(a AST) bool {
switch a := a.(type) {
case *Qualified:
if a.LocalName {
return hasReturnType(a.Name)
}
return false
case *Template:
return !isCDtorConversion(a.Name)
case *TypeWithQualifiers:
return hasReturnType(a.Base)
case *MethodWithQualifiers:
return hasReturnType(a.Method)
default:
return false
}
}
// isCDtorConversion returns when an AST is a constructor, a
// destructor, or a conversion operator.
func isCDtorConversion(a AST) bool {
switch a := a.(type) {
case *Qualified:
return isCDtorConversion(a.Name)
case *Constructor, *Destructor, *Cast:
return true
default:
return false
}
}
// taggedName parses:
//
// <tagged-name> ::= <name> B <source-name>
func (st *state) taggedName(a AST) AST {
for len(st.str) > 0 && st.str[0] == 'B' {
st.advance(1)
tag := st.sourceName()
a = &TaggedName{Name: a, Tag: tag}
}
return a
}
// name parses:
//
// <name> ::= <nested-name>
// ::= <unscoped-name>
// ::= <unscoped-template-name> <template-args>
// ::= <local-name>
//
// <unscoped-name> ::= <unqualified-name>
// ::= St <unqualified-name>
//
// <unscoped-template-name> ::= <unscoped-name>
// ::= <substitution>
//
// Besides the name, this returns whether it saw the code indicating
// a C++23 explicit object parameter.
func (st *state) name() (AST, bool) {
if len(st.str) < 1 {
st.fail("expected name")
}
var module AST
switch st.str[0] {
case 'N':
return st.nestedName()
case 'Z':
return st.localName()
case 'U':
a, isCast := st.unqualifiedName(nil)
if isCast {
st.setTemplate(a, nil)
}
return a, false
case 'S':
if len(st.str) < 2 {
st.advance(1)
st.fail("expected substitution index")
}
var a AST
isCast := false
subst := false
if st.str[1] == 't' {
st.advance(2)
a, isCast = st.unqualifiedName(nil)
a = &Qualified{Scope: &Name{Name: "std"}, Name: a, LocalName: false}
} else {
a = st.substitution(false)
if mn, ok := a.(*ModuleName); ok {
module = mn
break
}
subst = true
}
if len(st.str) > 0 && st.str[0] == 'I' {
// This can only happen if we saw
// <unscoped-template-name> and are about to see
// <template-args>. <unscoped-template-name> is a
// substitution candidate if it did not come from a
// substitution.
if !subst {
st.subs.add(a)
}
args := st.templateArgs()
tmpl := &Template{Name: a, Args: args}
if isCast {
st.setTemplate(a, tmpl)
st.clearTemplateArgs(args)
isCast = false
}
a = tmpl
}
if isCast {
st.setTemplate(a, nil)
}
return a, false
}
a, isCast := st.unqualifiedName(module)
if len(st.str) > 0 && st.str[0] == 'I' {
st.subs.add(a)
args := st.templateArgs()
tmpl := &Template{Name: a, Args: args}
if isCast {
st.setTemplate(a, tmpl)
st.clearTemplateArgs(args)
isCast = false
}
a = tmpl
}
if isCast {
st.setTemplate(a, nil)
}
return a, false
}
// nestedName parses:
//
// <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
// ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
//
// Besides the name, this returns whether it saw the code indicating
// a C++23 explicit object parameter.
func (st *state) nestedName() (AST, bool) {
st.checkChar('N')
var q AST
var r string
explicitObjectParameter := false
if len(st.str) > 0 && st.str[0] == 'H' {
st.advance(1)
explicitObjectParameter = true
} else {
q = st.cvQualifiers()
r = st.refQualifier()
}
a := st.prefix()
if q != nil || r != "" {
a = &MethodWithQualifiers{Method: a, Qualifiers: q, RefQualifier: r}
}
if len(st.str) == 0 || st.str[0] != 'E' {
st.fail("expected E after nested name")
}
st.advance(1)
return a, explicitObjectParameter
}
// prefix parses:
//
// <prefix> ::= <prefix> <unqualified-name>
// ::= <template-prefix> <template-args>
// ::= <template-param>
// ::= <decltype>
// ::=
// ::= <substitution>
//
// <template-prefix> ::= <prefix> <(template) unqualified-name>
// ::= <template-param>
// ::= <substitution>
//
// <decltype> ::= Dt <expression> E
// ::= DT <expression> E
func (st *state) prefix() AST {
var a AST
// The last name seen, for a constructor/destructor.
var last AST
var module AST
getLast := func(a AST) AST {
for {
if t, ok := a.(*Template); ok {
a = t.Name
} else if q, ok := a.(*Qualified); ok {
a = q.Name
} else if t, ok := a.(*TaggedName); ok {
a = t.Name
} else {
return a
}
}
}
var cast *Cast
for {
if len(st.str) == 0 {
st.fail("expected prefix")
}
var next AST
c := st.str[0]
if isDigit(c) || isLower(c) || c == 'U' || c == 'L' || c == 'F' || c == 'W' || (c == 'D' && len(st.str) > 1 && st.str[1] == 'C') {
un, isUnCast := st.unqualifiedName(module)
next = un
module = nil
if isUnCast {
if tn, ok := un.(*TaggedName); ok {
un = tn.Name
}
cast = un.(*Cast)
}
} else {
switch st.str[0] {
case 'C':
inheriting := false
st.advance(1)
if len(st.str) > 0 && st.str[0] == 'I' {
inheriting = true
st.advance(1)
}
if len(st.str) < 1 {
st.fail("expected constructor type")
}
if last == nil {
st.fail("constructor before name is seen")
}
st.advance(1)
var base AST
if inheriting {
base = st.demangleType(false)
}
next = &Constructor{
Name: getLast(last),
Base: base,
}
if len(st.str) > 0 && st.str[0] == 'B' {
next = st.taggedName(next)
}
case 'D':
if len(st.str) > 1 && (st.str[1] == 'T' || st.str[1] == 't') {
next = st.demangleType(false)
} else {
if len(st.str) < 2 {
st.fail("expected destructor type")
}
if last == nil {
st.fail("destructor before name is seen")
}
st.advance(2)
next = &Destructor{Name: getLast(last)}
if len(st.str) > 0 && st.str[0] == 'B' {
next = st.taggedName(next)
}
}
case 'S':
next = st.substitution(true)
if mn, ok := next.(*ModuleName); ok {
module = mn
next = nil
}
case 'I':
if a == nil {
st.fail("unexpected template arguments")
}
var args []AST
args = st.templateArgs()
tmpl := &Template{Name: a, Args: args}
if cast != nil {
st.setTemplate(cast, tmpl)
st.clearTemplateArgs(args)
cast = nil
}
a = nil
next = tmpl
case 'T':
next = st.templateParam()
case 'E':
if a == nil {
st.fail("expected prefix")
}
if cast != nil {
var toTmpl *Template
if castTempl, ok := cast.To.(*Template); ok {
toTmpl = castTempl
}
st.setTemplate(cast, toTmpl)
}
return a
case 'M':
if a == nil {
st.fail("unexpected lambda initializer")
}
// This is the initializer scope for a
// lambda. We don't need to record
// it. The normal code will treat the
// variable has a type scope, which
// gives appropriate output.
st.advance(1)
continue
case 'J':
// It appears that in some cases clang
// can emit a J for a template arg
// without the expected I. I don't
// know when this happens, but I've
// seen it in some large C++ programs.
if a == nil {
st.fail("unexpected template arguments")
}
var args []AST
for len(st.str) == 0 || st.str[0] != 'E' {
arg := st.templateArg(nil)
args = append(args, arg)
}
st.advance(1)
tmpl := &Template{Name: a, Args: args}
if cast != nil {
st.setTemplate(cast, tmpl)
st.clearTemplateArgs(args)
cast = nil
}
a = nil
next = tmpl
default:
st.fail("unrecognized letter in prefix")
}
}
if next == nil {
continue
}
last = next
if a == nil {
a = next
} else {
a = &Qualified{Scope: a, Name: next, LocalName: false}
}
if c != 'S' && (len(st.str) == 0 || st.str[0] != 'E') {
st.subs.add(a)
}
}
}
// unqualifiedName parses:
//
// <unqualified-name> ::= <operator-name>
// ::= <ctor-dtor-name>
// ::= <source-name>
// ::= <local-source-name>
//
// <local-source-name> ::= L <source-name> <discriminator>
func (st *state) unqualifiedName(module AST) (r AST, isCast bool) {
if len(st.str) < 1 {
st.fail("expected unqualified name")
}
module = st.moduleName(module)
friend := false
if len(st.str) > 0 && st.str[0] == 'F' {
st.advance(1)
friend = true
if len(st.str) < 1 {
st.fail("expected unqualified name")
}
}
var a AST
isCast = false
c := st.str[0]
if isDigit(c) {
a = st.sourceName()
} else if isLower(c) {
a, _ = st.operatorName(false)
if _, ok := a.(*Cast); ok {
isCast = true
}
if op, ok := a.(*Operator); ok && op.Name == `operator"" ` {
n := st.sourceName()
a = &Unary{Op: op, Expr: n, Suffix: false, SizeofType: false}
}
} else if c == 'D' && len(st.str) > 1 && st.str[1] == 'C' {
var bindings []AST
st.advance(2)
for {
binding := st.sourceName()
bindings = append(bindings, binding)
if len(st.str) > 0 && st.str[0] == 'E' {
st.advance(1)
break
}
}
a = &StructuredBindings{Bindings: bindings}
} else {
switch c {
case 'C', 'D':
st.fail("constructor/destructor not in nested name")
case 'L':
st.advance(1)
a = st.sourceName()
a = st.discriminator(a)
case 'U':
if len(st.str) < 2 {
st.advance(1)
st.fail("expected closure or unnamed type")
}
c := st.str[1]
switch c {
case 'b':
st.advance(2)
st.compactNumber()
a = &Name{Name: "'block-literal'"}
case 'l':
a = st.closureTypeName()
case 't':
a = st.unnamedTypeName()
default:
st.advance(1)
st.fail("expected closure or unnamed type")
}
default:
st.fail("expected unqualified name")
}
}
if module != nil {
a = &ModuleEntity{Module: module, Name: a}
}
if len(st.str) > 0 && st.str[0] == 'B' {
a = st.taggedName(a)
}
if friend {
a = &Friend{Name: a}
}
return a, isCast
}
// sourceName parses:
//
// <source-name> ::= <(positive length) number> <identifier>
// identifier ::= <(unqualified source code identifier)>
func (st *state) sourceName() AST {
val := st.number()
if val <= 0 {
st.fail("expected positive number")
}
if len(st.str) < val {
st.fail("not enough characters for identifier")
}
id := st.str[:val]
st.advance(val)