/
package.go
1015 lines (842 loc) · 22.4 KB
/
package.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
package code
import (
"go/ast"
"go/token"
"go/types"
"log"
"reflect"
"strings"
"golang.org/x/tools/go/packages"
)
type Module struct {
Dir string
Root string // root import path
Version string
}
type Package struct {
Index int
PPkg *packages.Package // ToDo: renamed to PP to be consistent with TypeInfo.TT?
Mod *Module
Deps []*Package
DepLevel int // 0 means the level is not determined yet
DepedBys []*Package
// This field might be shared with PackageForDisplay
// for concurrent reads.
*PackageAnalyzeResult
}
func (p *Package) Path() string {
return p.PPkg.PkgPath // might be prefixed with "vendor/", which is different from import path.
}
type PackageAnalyzeResult struct {
AllTypeNames []*TypeName
AllFunctions []*Function
AllVariables []*Variable
AllConstants []*Constant
AllImports []*Import
SourceFiles []SourceFileInfo
Directory string
}
func NewPackageAnalyzeResult() *PackageAnalyzeResult {
// ToDo: maybe it is better to run a statistic phase firstly,
// so that the length of each slice will get knowledged.
return &PackageAnalyzeResult{
AllTypeNames: make([]*TypeName, 0, 64),
AllFunctions: make([]*Function, 0, 64),
AllVariables: make([]*Variable, 0, 64),
AllConstants: make([]*Constant, 0, 64),
AllImports: make([]*Import, 0, 64),
}
}
func (r *PackageAnalyzeResult) SourceFileInfoByBareFilename(bareFilename string) *SourceFileInfo {
for _, info := range r.SourceFiles {
//if info.OriginalGoFile == srcPath {
// return &info
//}
//if info.GeneratedFile == srcPath {
// return &info
//}
if info.BareFilename == bareFilename {
return &info
}
if info.BareGeneratedFilename == bareFilename {
return &info
}
}
return nil
}
// ToDo: better to maintain a global sourceFilePath => SourceFileInfo table?
//func (r *PackageAnalyzeResult) SourceFileInfo(srcPath string) *SourceFileInfo {
func (r *PackageAnalyzeResult) SourceFileInfoByFilePath(srcPath string) *SourceFileInfo {
for _, info := range r.SourceFiles {
if info.OriginalFile == srcPath {
return &info
}
if info.GeneratedFile == srcPath {
return &info
}
}
return nil
}
type RefPos struct {
Pkg *Package
Pos token.Pos
}
type AstNode struct {
Pkg *Package
Node ast.Node
}
type Resource interface {
Name() string
Exported() bool
//IndexString() string
Documentation() string
Comment() string
Position() token.Position
Package() *Package
}
type ValueResource interface {
Resource
TType() types.Type // The result should not be used in comparisons.
TypeInfo(d *CodeAnalyzer) *TypeInfo
}
type FunctionResource interface {
ValueResource
IsMethod() bool
ReceiverTypeName() (paramField *ast.Field, typeIdent *ast.Ident, isStar bool)
AstFuncType() *ast.FuncType
// For *Function, the result is the same as ValueResource.Package().
// For *InterfaceMethod, this might be different (caused by embedding, or other reasons).
AstPackage() *Package
}
var (
_ FunctionResource = (*Function)(nil)
_ FunctionResource = (*InterfaceMethod)(nil)
)
type AstValueSpecOwner interface {
AstValueSpec() *ast.ValueSpec
Package() *Package
}
var (
_ AstValueSpecOwner = (*Variable)(nil)
_ AstValueSpecOwner = (*Constant)(nil)
)
type Attribute uint32
const (
// Runtime only flags.
analyseCompleted Attribute = 1 << (31 - iota)
directSelectorsCollected
promotedSelectorsCollected
// Higher bits are for runtime-only flags.
AtributesPersistentMask Attribute = (1 << 25) - 1
// Caching individual packages seperately might be not a good idea.
// There are many complexities here.
// * implementation relations become larger along with more packages are involved.
// Caching by arguments starting packages, as one file, is simpler.
// For functions, type aliases and named types.
Builtin Attribute = 1 << 0
// For type aliases and named types.
Embeddable Attribute = 1 << 1
PtrEmbeddable Attribute = 1 << 2
// For unnamed struct and interface types.
HasUnexporteds Attribute = 1 << 3
// For all types.
Defined Attribute = 1 << 4
Comparable Attribute = 1 << 5
// For channel types.
Sendable Attribute = 1 << 6
Receivable Attribute = 1 << 7
// For funcitons.
Variadic Attribute = 1 << 8
)
type TypeSource struct {
TypeName *TypeName
UnnamedType *TypeInfo
}
//func (ts *TypeSource) Denoting(d *CodeAnalyzer) *TypeInfo {
// if ts.UnnamedType != nil {
// return ts.UnnamedType
// }
// return ts.TypeName.Denoting(d)
//}
type TypeName struct {
// One and only one of the two is nil.
Alias *TypeAlias
Named *TypeInfo
//index uint32 // the global index
// ToDo: simplify the source defintion.
// Four kinds of sources to affect promoted selectors:
// 1. typename
// 2. *typename
// 3. unnamed type
// 4. *unname type
Source TypeSource
StarSource *TypeSource
UsePositions []token.Position
*types.TypeName
index uint32 // ToDo: any useful?
Pkg *Package // some duplicated with types.TypeName.Pkg(), except builtin types
AstDecl *ast.GenDecl
AstSpec *ast.TypeSpec
}
//func (tn *TypeName) IndexString() string {
// var b strings.Builder
//
// b.WriteString(tn.Name())
// if tn.Alias != nil {
// b.WriteString(" = ")
// } else {
// b.WriteString(" ")
// }
// WriteType(&b, tn.AstSpec.Type, tn.Pkg.PPkg.TypesInfo, true)
//
// return b.String()
//}
//func (tn *TypeName) Id() string {
// return tn.obj.Id()
//}
//func (tn *TypeName) Name() string {
// return tn.obj.Name()
//}
func (tn *TypeName) Exported() bool {
if tn.Pkg.Path() == "builtin" {
return !token.IsExported(tn.Name())
}
return tn.TypeName.Exported()
}
func (tn *TypeName) Position() token.Position {
return tn.Pkg.PPkg.Fset.PositionFor(tn.AstSpec.Name.Pos(), false)
}
func (tn *TypeName) Documentation() string {
//doc := tn.AstDecl.Doc.Text()
//if t := tn.AstSpec.Doc.Text(); t != "" {
// doc = doc + "\n\n" + t
//}
//return doc
doc := tn.AstSpec.Doc.Text()
if doc == "" {
doc = tn.AstDecl.Doc.Text()
}
return doc
}
func (tn *TypeName) Comment() string {
return tn.AstSpec.Comment.Text()
}
func (tn *TypeName) Package() *Package {
return tn.Pkg
}
//func (tn *TypeName) Comment() string {
// return tn.AstSpec.Comment.Text()
//}
//func (tn *TypeName) Denoting(d *CodeAnalyzer) *TypeInfo {
// if tn.Named != nil {
// return tn.Named
// }
//
// if tn.StarSource != nil {
// return d.RegisterType(types.NewPointer(tn.StarSource.Denoting(d).TT))
// }
//
// return tn.Source.Denoting(d)
//}
func (tn *TypeName) Denoting() *TypeInfo {
if tn.Named != nil {
return tn.Named
}
return tn.Alias.Denoting
}
//func (tn *TypeName) Underlying(d *CodeAnalyzer) *TypeInfo {
// if tn.StarSource != nil || tn.Source.UnnamedType != nil {
// return tn.Denoting(d)
// }
// return tn.Source.TypeName.Underlying(d)
//}
type TypeAlias struct {
Denoting *TypeInfo
// For named and basic types.
TypeName *TypeName
// Builtin, Embeddable.
attributes Attribute
}
//func (a *TypeAlias) Embeddable() bool {
// var tc = a.Denoting.Common()
// if tc.Attributes&Embeddable != 0 {
// return true
// }
// if tc.Kind != Pointer {
// return false
// }
// if _, ok := a.Denoting.(*Type_Named); !ok {
// return false
// }
// tc = a.Denoting.(*Type_Pointer).Common()
// return tc.Kind&(Ptr|Interface) == 0
//}
type TypeInfo struct {
TT types.Type
Underlying *TypeInfo
//Implements []*TypeInfo
///StarImplements []*TypeInfo // if TT is neither pointer nor interface.
Implements []Implementation
// For interface types.
ImplementedBys []*TypeInfo
// For builtin and unnamed types only.
Aliases []*TypeAlias // ToDo:
// For named and basic types.
TypeName *TypeName
// For unnamed types.
UsePositions []token.Position
// For unnamed interfaces and structs, this field must be nil.
//Pkg *Package // Looks this field is never used. (It really should not exist in this type.)
// Explicit fields and methods.
// * For named types, only explicitly declared methods are included.
// The field is only built for T. (*T).DirectSelectors is always nil.
// * For named interface types, all explicitly specified methods and embedded types (as fields).
// * For unnamed struct types, only direct fields. Only built for strct{...}, not for *struct{...}.
DirectSelectors []*Selector
EmbeddingFields int32 // for struct types only now. ToDo: also for interface types.
// All methods, including extended/promoted ones.
AllMethods []*Selector
// All fields, including promoted ones.
AllFields []*Selector
// Including promoted ones. For both T and *T.
//Methods []*Method
// For .TypeName != nil
AsTypesOf []ValueResource // variables and constants
AsInputsOf []ValueResource // variables and functions
AsOutputsOf []ValueResource // variables and functions
// ToDo: register variables (of function types) for AsInputsOf and AsOutputsOf
attributes Attribute // ToDo: fill the bits
// The global type index. It will be
// used in calculating method signatures.
// ToDo: check if it is problematic to allow index == 0.
index uint32
// Used in several scenarios.
counter uint32
//counter2 int32
}
func (t *TypeInfo) Kind() reflect.Kind {
return Kind(t.TT)
}
func Kind(tt types.Type) reflect.Kind {
switch tt := tt.Underlying().(type) {
default:
log.Printf("unknown kind of type: %T", tt)
return reflect.Invalid
case *types.Basic:
switch bt := tt.Kind(); bt {
default: // t.TT: builtin.Type, unsafe.ArbitraryType, etc.
//log.Printf("bad basic kind: %v, %v", bt, t.TT)
return reflect.Invalid
case types.Bool:
return reflect.Bool
case types.Int:
return reflect.Int
case types.Int8:
return reflect.Int8
case types.Int16:
return reflect.Int16
case types.Int32:
return reflect.Int32
case types.Int64:
return reflect.Int64
case types.Uint:
return reflect.Uint
case types.Uint8:
return reflect.Uint8
case types.Uint16:
return reflect.Uint16
case types.Uint32:
return reflect.Uint32
case types.Uint64:
return reflect.Uint64
case types.Uintptr:
return reflect.Uintptr
case types.Float32:
return reflect.Float32
case types.Float64:
return reflect.Float64
case types.Complex64:
return reflect.Complex64
case types.Complex128:
return reflect.Complex128
case types.String:
return reflect.String
case types.UnsafePointer:
return reflect.UnsafePointer
}
case *types.Pointer:
return reflect.Ptr
case *types.Struct:
return reflect.Struct
case *types.Array:
return reflect.Array
case *types.Slice:
return reflect.Slice
case *types.Map:
return reflect.Map
case *types.Chan:
return reflect.Chan
case *types.Signature:
return reflect.Func
case *types.Interface:
return reflect.Interface
}
}
type Implementation struct {
Impler *TypeInfo // a struct or named type (same as the owner), or a pointer to such a type
Interface *TypeInfo // an interface type
}
type Import struct {
*types.PkgName
Pkg *Package // some duplicated with types.PkgName.Pkg()
AstDecl *ast.GenDecl
AstSpec *ast.ImportSpec
}
type Constant struct {
*types.Const
Type *TypeInfo
Pkg *Package // some duplicated with types.Const.Pkg()
AstDecl *ast.GenDecl
AstSpec *ast.ValueSpec
}
func (c *Constant) Position() token.Position {
for _, n := range c.AstSpec.Names {
if n.Name == c.Name() {
return c.Pkg.PPkg.Fset.PositionFor(n.Pos(), false)
}
}
panic("should not")
}
func (c *Constant) Documentation() string {
doc := c.AstSpec.Doc.Text()
if doc == "" {
doc = c.AstDecl.Doc.Text()
}
return doc
}
func (c *Constant) Comment() string {
return c.AstSpec.Comment.Text()
}
func (c *Constant) Package() *Package {
return c.Pkg
}
func (c *Constant) Exported() bool {
if c.Pkg.Path() == "builtin" {
return !token.IsExported(c.Name())
}
return c.Const.Exported()
}
func (c *Constant) TType() types.Type {
return c.Const.Type()
}
func (c *Constant) TypeInfo(d *CodeAnalyzer) *TypeInfo {
if c.Type == nil {
c.Type = d.RegisterType(c.TType())
}
return c.Type
}
func (c *Constant) AstValueSpec() *ast.ValueSpec {
return c.AstSpec
}
//func (c *Constant) IndexString() string {
// btt, ok := c.Type().(*types.Basic)
// if !ok {
// panic("constant should be always of basic type")
// }
// isTyped := btt.Info()&types.IsUntyped == 0
//
// var b strings.Builder
//
// b.WriteString(c.Name())
// if isTyped {
// b.WriteByte(' ')
// b.WriteString(c.Type().String())
// }
// b.WriteString(" = ")
// b.WriteString(c.Val().String())
//
// return b.String()
//}
type Variable struct {
*types.Var
Type *TypeInfo
Pkg *Package // some duplicated with types.Var.Pkg()
AstDecl *ast.GenDecl
AstSpec *ast.ValueSpec
}
func (v *Variable) Position() token.Position {
for _, n := range v.AstSpec.Names {
if n.Name == v.Name() {
return v.Pkg.PPkg.Fset.PositionFor(n.Pos(), false)
}
}
panic("should not")
}
func (v *Variable) Documentation() string {
doc := v.AstSpec.Doc.Text()
if doc == "" {
doc = v.AstDecl.Doc.Text()
}
return doc
}
func (v *Variable) Comment() string {
return v.AstSpec.Comment.Text()
}
func (v *Variable) Package() *Package {
return v.Pkg
}
func (v *Variable) Exported() bool {
if v.Pkg.Path() == "builtin" {
return !token.IsExported(v.Name())
}
return v.Var.Exported()
}
func (v *Variable) TType() types.Type {
return v.Var.Type()
}
func (v *Variable) TypeInfo(d *CodeAnalyzer) *TypeInfo {
if v.Type == nil {
v.Type = d.RegisterType(v.TType())
}
return v.Type
}
func (v *Variable) AstValueSpec() *ast.ValueSpec {
return v.AstSpec
}
//func (v *Variable) IndexString() string {
// var b strings.Builder
//
// b.WriteString(v.Name())
// b.WriteByte(' ')
// b.WriteString(v.Type().String())
//
// s := b.String()
// println(s)
// return s
//}
type Function struct {
*types.Func
*types.Builtin // for builtin functions
// Builtin, Variadic.
attributes Attribute
// ToDo: maintain parameter and result TypeInfos, for performance.
// ToDo
fSigIndex uint32 // as package function
mSigIndex uint32 // as method, (ToDo: make 0 as invalid function index)
Type *TypeInfo
Pkg *Package // some duplicated with types.Func.Pkg(), except builtin functions
AstDecl *ast.FuncDecl
}
func (f *Function) Name() string {
if f.Func != nil {
return f.Func.Name()
}
return f.Builtin.Name()
}
func (f *Function) Exported() bool {
if f.Builtin != nil {
return true
}
if f.Pkg.Path() == "builtin" {
return !token.IsExported(f.Name())
}
return f.Func.Exported()
}
func (f *Function) Position() token.Position {
return f.Pkg.PPkg.Fset.PositionFor(f.AstDecl.Name.Pos(), false)
}
func (f *Function) Documentation() string {
// ToDo: html escape
return f.AstDecl.Doc.Text()
}
func (f *Function) Comment() string {
return ""
}
func (f *Function) Package() *Package {
return f.Pkg
}
func (f *Function) TType() types.Type {
if f.Func != nil {
return f.Func.Type()
}
return f.Builtin.Type()
}
func (f *Function) TypeInfo(d *CodeAnalyzer) *TypeInfo {
if f.Type == nil {
f.Type = d.RegisterType(f.TType())
}
return f.Type
}
func (f *Function) IsMethod() bool {
return f.Func != nil && f.Func.Type().(*types.Signature).Recv() != nil
}
func (f *Function) String() string {
if f.Func != nil {
return f.Func.String()
}
return f.Builtin.String()
}
//func (f *Function) IndexString() string {
// var b strings.Builder
// b.WriteString(f.Name())
// b.WriteByte(' ')
// WriteType(&b, f.AstDecl.Type, f.Pkg.PPkg.TypesInfo, true)
// return b.String()
//}
// Please make sure the Funciton is a method when calling this method.
func (f *Function) ReceiverTypeName() (paramField *ast.Field, typeIdent *ast.Ident, isStar bool) {
if f.AstDecl.Recv == nil {
panic("should not")
}
if len(f.AstDecl.Recv.List) != 1 {
panic("should not")
}
paramField = f.AstDecl.Recv.List[0]
switch expr := paramField.Type.(type) {
default:
panic("should not")
case *ast.Ident:
typeIdent = expr
isStar = false
return
case *ast.StarExpr:
tid, ok := expr.X.(*ast.Ident)
if !ok {
panic("should not")
}
typeIdent = tid
isStar = true
return
}
}
func (f *Function) AstFuncType() *ast.FuncType {
return f.AstDecl.Type
}
func (f *Function) AstPackage() *Package {
return f.Package()
}
// As Function.
type InterfaceMethod struct {
InterfaceTypeName *TypeName
Method *Method // .AstFunc == nil, .AstInterface != nil
// ToDo: an interface method might have several ast sources,
// so there should be multiple Methods ([]*Method).
}
func (im *InterfaceMethod) Name() string {
return im.Method.Name
}
func (im *InterfaceMethod) Exported() bool {
return token.IsExported(im.Name())
}
func (im *InterfaceMethod) Position() token.Position {
return im.Method.Pkg.PPkg.Fset.PositionFor(im.Method.AstField.Pos(), false)
}
func (im *InterfaceMethod) Documentation() string {
return im.Method.AstField.Doc.Text()
}
func (im *InterfaceMethod) Comment() string {
return im.Method.AstField.Comment.Text()
}
func (im *InterfaceMethod) Package() *Package {
return im.InterfaceTypeName.Pkg
}
func (im *InterfaceMethod) TType() types.Type {
return im.Method.Type.TT
}
func (im *InterfaceMethod) TypeInfo(d *CodeAnalyzer) *TypeInfo {
return im.Method.Type
}
func (im *InterfaceMethod) IsMethod() bool {
return true
}
func (im *InterfaceMethod) String() string {
// ToDo: show the inteface receiver in result.
return im.Method.Type.TT.String()
}
//func (im *InterfaceMethod) IndexString() string {
// var b strings.Builder
// b.WriteString(f.Name())
// b.WriteByte(' ')
// WriteType(&b, f.AstDecl.Type, f.Pkg.PPkg.TypesInfo, true)
// return b.String()
//}
// Please make sure the Funciton is a method when calling this method.
func (im *InterfaceMethod) ReceiverTypeName() (paramField *ast.Field, typeIdent *ast.Ident, isStar bool) {
return nil, im.InterfaceTypeName.AstSpec.Name, false
}
func (im *InterfaceMethod) AstFuncType() *ast.FuncType {
return im.Method.AstField.Type.(*ast.FuncType)
}
func (im *InterfaceMethod) AstPackage() *Package {
return im.Method.Pkg
}
type MethodSignature struct {
Name string // must be an identifier other than "_"
Pkg string // the import path, for unepxorted method names only
// ToDo: the above two can be replaced with two int32 IDs.
//InOutTypes []int32 // global type indexes
InOutTypes string
NumInOutAndVariadic int
}
type EmbedMode uint8
const (
EmbedMode_None EmbedMode = iota
EmbedMode_Direct
EmbedMode_Indirect
)
type Field struct {
astStruct *ast.StructType
AstField *ast.Field
AstInterface *ast.InterfaceType // for embedding interface in interface (the owner interface)
Pkg *Package // (nil for exported. ??? Seems not true.)
Name string
Type *TypeInfo
Tag string
Mode EmbedMode
}
func (fld *Field) Position() token.Position {
return fld.Pkg.PPkg.Fset.PositionFor(fld.AstField.Pos(), false)
}
type Method struct {
AstFunc *ast.FuncDecl // for concrete methods
AstInterface *ast.InterfaceType // for interface methods (the owner interface)
AstField *ast.Field // for interface methods
Pkg *Package // (nil for exported. ??? Seems not true.)
Name string
Type *TypeInfo // ToDo: use custom struct including PointerRecv instead.
PointerRecv bool // duplicated info, for faster access
ImplementsSomething bool // false if the method is unimportant for its reveiver to implement some interface type
index uint32 // 0 means this method doesn;t contribute to any type implementations for sure.
}
func (mthd *Method) Position() token.Position {
return mthd.Pkg.PPkg.Fset.PositionFor(mthd.AstFunc.Pos(), false)
}
type EmbeddedField struct {
*Field
Prev *EmbeddedField
}
type SelectorCond uint8
const (
SelectorCond_Normal SelectorCond = iota
SelectorCond_Hidden
)
type Selector struct {
Id string
// One and only one of the two is nil.
*Field
*Method
// EmbeddedField is nil means this is not an promoted selector.
//EmbeddedFields []*Field
EmbeddingChain *EmbeddedField // in the inverse order
Depth uint16 // the chain length
Indirect bool // whether the chain contains indirects or not
// colliding or shadowed susposed promoted selector?
//shadowed bool // used in collecting phase.
cond SelectorCond
}
func (s *Selector) Reset() {
*s = Selector{}
}
func (s *Selector) Object() types.Object {
if s.Field != nil {
for _, ident := range s.Field.AstField.Names {
if ident.Name == s.Field.Name {
return s.Field.Pkg.PPkg.TypesInfo.ObjectOf(ident)
}
}
return nil // ToDo: handle the embedded field case
}
// Non-interface method
if s.Method.AstFunc != nil {
return s.Method.Pkg.PPkg.TypesInfo.ObjectOf(s.Method.AstFunc.Name)
}
// Interface method
return s.Method.Pkg.PPkg.TypesInfo.ObjectOf(s.Method.AstField.Names[0])
}
func (s *Selector) Position() token.Position {
if s.Field != nil {
return s.Field.Pkg.PPkg.Fset.PositionFor(s.Field.AstField.Pos(), false)
} else if s.Method.AstFunc != nil { // method declaration
return s.Method.Pkg.PPkg.Fset.PositionFor(s.Method.AstFunc.Pos(), false)
} else { // if s.Method.AstField != nil //initerface method specification
return s.Method.Pkg.PPkg.Fset.PositionFor(s.Method.AstField.Pos(), false)
}
}
func (s *Selector) Name() string {
if s.Field != nil {
return s.Field.Name
} else {
return s.Method.Name
}
}
// ToDo: change to Package() for consistency.
func (s *Selector) Package() *Package {
if s.Field != nil {
return s.Field.Pkg
} else {
return s.Method.Pkg
}
}
//func (s *Selector) Depth() int {
// return len(s.EmbeddedFields)
//}
func (s *Selector) PointerReceiverOnly() bool {
if s.Method == nil {
panic("not a method selector")
}
return !s.Indirect && s.Method.PointerRecv
}
func (s *Selector) String() string {
return EmbededFieldsPath(s.EmbeddingChain, nil, s.Name(), s.Field != nil)
}
//func (s *Selector) Comment() string {
// return "" // ToDo:
//}
//
//func (s *Selector) Documentation() string {
// return "" // ToDo:
//}
//
//func (s *Selector) Exported() bool {
// if s.Field != nil {
// return token.IsExported(s.Field.Name)
// } else {
// return token.IsExported(s.Method.Name)
// }
//}
func EmbededFieldsPath(embedding *EmbeddedField, b *strings.Builder, selName string, isField bool) (r string) {
if embedding == nil {
if isField {
return "[field] " + selName
} else {
return "[method] " + selName
}
}
if b == nil {
b = &strings.Builder{}
if isField {
b.WriteString("[field] ")
} else {
b.WriteString("[method] ")
}
defer func() {
b.WriteString(selName)
r = b.String()
}()
}
if p := embedding.Prev; p != nil {
EmbededFieldsPath(p, b, "", isField)