-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastop.go
183 lines (167 loc) · 3.98 KB
/
astop.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
package srcdom
import (
"fmt"
"go/ast"
"strings"
)
// baseTypeName returns the name of the base type of x (or "")
// and whether the type is imported or not.
func baseTypeName(x ast.Expr) (name string, imported bool) {
switch typ := x.(type) {
case *ast.Ident:
return typ.Name, false
case *ast.SelectorExpr:
if _, ok := typ.X.(*ast.Ident); ok {
// only possible for qualified type names;
// assume type is imported
return typ.Sel.Name, true
}
case *ast.StarExpr:
return baseTypeName(typ.X)
}
return
}
func typeString(x ast.Expr) string {
switch typ := x.(type) {
case *ast.Ident:
return typ.Name
case *ast.SelectorExpr:
if _, ok := typ.X.(*ast.Ident); ok {
return typeString(typ.X) + "." + typ.Sel.Name
}
case *ast.StarExpr:
return "*" + typeString(typ.X)
case *ast.FuncType:
fn := toFunc("", typ)
b := &strings.Builder{}
b.WriteString("func (" + typesString(fn.Params) + ")")
fn.writeResults(b)
return b.String()
case *ast.Ellipsis:
return "..." + typeString(typ.Elt)
case *ast.ArrayType:
return "[]" + typeString(typ.Elt)
case *ast.MapType:
return "map[" + typeString(typ.Key) + "]" + typeString(typ.Value)
case *ast.StructType:
if typ.Fields == nil || len(typ.Fields.List) == 0 {
return "struct{}"
}
b := &strings.Builder{}
b.WriteString("struct { ")
for i, f := range typ.Fields.List {
if i > 0 {
b.WriteString("; ")
}
b.WriteString(firstName(f.Names))
b.WriteString(" ")
b.WriteString(typeString(f.Type))
}
b.WriteString(" }")
return b.String()
case *ast.InterfaceType:
if typ.Methods == nil || len(typ.Methods.List) == 0 {
return "interface{}"
}
b := &strings.Builder{}
b.WriteString("interface { ")
for i, m := range typ.Methods.List {
if i > 0 {
b.WriteString("; ")
}
switch mTyp := m.Type.(type) {
case *ast.FuncType:
fn := toFunc("", mTyp)
b.WriteString(firstName(m.Names))
b.WriteString("(" + typesString(fn.Params) + ")")
fn.writeResults(b)
default:
panic(fmt.Sprintf("not supported fields in unnamed interface type: %T", m.Type))
}
}
b.WriteString(" }")
return b.String()
case *ast.ChanType:
var chanLabel string
switch typ.Dir {
case ast.SEND | ast.RECV:
chanLabel = "chan"
case ast.SEND:
chanLabel = "chan<-"
case ast.RECV:
chanLabel = "<-chan"
default:
panic(fmt.Sprintf("illegal channel direction (ast.ChanDir): %d", typ.Dir))
}
return chanLabel + " " + typeString(typ.Value)
case *ast.BinaryExpr:
return typeString(typ.X) + " " + typ.Op.String() + " " + typeString(typ.Y)
case *ast.UnaryExpr:
return typ.Op.String() + typeString(typ.X)
case *ast.IndexExpr:
return typeString(typ.X) + "[" + typeString(typ.Index) + "]"
case *ast.IndexListExpr:
b := &strings.Builder{}
b.WriteString(typeString(typ.X))
b.WriteRune('[')
for i, expr := range typ.Indices {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(typeString(expr))
}
b.WriteRune(']')
return b.String()
default:
warnf("typeString doesn't support: %T", typ)
}
return ""
}
func firstName(names []*ast.Ident) string {
if len(names) == 0 {
return ""
}
return names[0].Name
}
func toVar(f *ast.Field) []*Var {
typ := typeString(f.Type)
if len(f.Names) == 0 {
return []*Var{{Name: "", Type: typ}}
}
vars := make([]*Var, len(f.Names))
for i, n := range f.Names {
vars[i] = &Var{Name: n.Name, Type: typ}
}
return vars
}
func toVarArray(fl *ast.FieldList) []*Var {
if fl == nil || len(fl.List) == 0 {
return nil
}
vars := make([]*Var, 0, len(fl.List))
for _, f := range fl.List {
vars = append(vars, toVar(f)...)
}
return vars
}
func typesString(vars []*Var) string {
if len(vars) == 0 {
return ""
}
b := &strings.Builder{}
for i, v := range vars {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(v.Type)
}
return b.String()
}
func toFunc(name string, funcType *ast.FuncType) *Func {
f := &Func{Name: name}
if funcType != nil {
f.Params = toVarArray(funcType.Params)
f.Results = toVarArray(funcType.Results)
}
return f
}