-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtypesmap.go
228 lines (208 loc) · 5.57 KB
/
typesmap.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
// Copyright 2017 Walter Schulze
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package derive
import (
"bytes"
"fmt"
"go/format"
"go/types"
"strconv"
)
// TypesMap is a map of input types to function names.
// It also keeps track of which functions have been generated.
type TypesMap interface {
SetFuncName(name string, typs ...types.Type) (newName string, err error)
GetFuncName(typs ...types.Type) string
Generating(typs ...types.Type)
ToGenerate() [][]types.Type
Prefix() string
TypeString(typ types.Type) string
FieldStrings(fields []*types.Var) ([]string, error)
IsExternal(typ ObjectGetter) bool
Done() bool
}
// ObjectGetter is implemented by both [types.Named] and [types.Alias].
type ObjectGetter interface {
Obj() *types.TypeName
}
type typesMap struct {
qual types.Qualifier
prefix string
generated map[string]bool
funcToTyps map[string][]types.Type
typss [][]types.Type
reserved map[string]struct{}
autoname bool
dedup bool
}
func newTypesMap(qual types.Qualifier, prefix string, reserved map[string]struct{}, autoname bool, dedup bool) TypesMap {
return &typesMap{
qual: qual,
prefix: prefix,
generated: make(map[string]bool),
funcToTyps: make(map[string][]types.Type),
typss: nil,
reserved: reserved,
autoname: autoname,
dedup: dedup,
}
}
func (tm *typesMap) Prefix() string {
return tm.prefix
}
func (tm *typesMap) TypeString(typ types.Type) string {
return types.TypeString(types.Default(typ), tm.qual)
}
func (tm *typesMap) FieldStrings(fields []*types.Var) ([]string, error) {
strct := types.NewStruct(fields, nil)
strctStr, err := format.Source([]byte("var a " + tm.TypeString(strct)))
if err != nil {
return nil, err
}
strctLines := bytes.Split(strctStr, []byte{'\n'})
ss := make([]string, len(strctLines)-2)
for i := range strctLines[1 : len(strctLines)-1] {
ss[i] = string(bytes.TrimSpace(strctLines[i+1]))
}
return ss, nil
}
func (tm *typesMap) TypeStringBypass(typ types.Type) string {
return types.TypeString(types.Default(typ), bypassQual)
}
func (tm *typesMap) IsExternal(typ ObjectGetter) bool {
q := tm.qual(typ.Obj().Pkg())
return q != ""
}
func (tm *typesMap) SetFuncName(funcName string, typs ...types.Type) (string, error) {
if fName, ok := tm.nameOf(typs); ok {
if fName == funcName {
return funcName, nil
}
if tm.dedup {
return fName, nil
}
return "", fmt.Errorf("ambigious function names for type %s = (%s | %s)", typs, fName, funcName)
}
if ts, ok := tm.funcToTyps[funcName]; ok {
if eq(ts, typs) {
return funcName, nil
}
if tm.autoname {
return tm.GetFuncName(typs...), nil
}
return "", fmt.Errorf("conflicting function names %s(%v) and %s(%v)", funcName, ts, funcName, typs)
}
tm.funcToTyps[funcName] = typs
tm.typss = append(tm.typss, typs)
return funcName, nil
}
func (tm *typesMap) GetFuncName(typs ...types.Type) string {
// log.Printf("GetFuncName: %v", typs)
name, ok := tm.nameOf(typs)
if !ok {
name = tm.newName(typs)
tm.SetFuncName(name, typs...)
}
// log.Printf("GotFuncName: %s(%v)", name, typs)
return name
}
func (tm *typesMap) newName(typs []types.Type) string {
name := ""
if len(typs) > 0 {
switch t := typs[0].(type) {
case *types.Named:
name = t.Obj().Name()
case *types.Basic:
switch t.Kind() {
case types.Bool, types.Int, types.Int8, types.Int16, types.Int32, types.Int64,
types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64,
types.Float32, types.Float64, types.String:
name = tm.TypeString(t)
}
}
}
i := 0
funcName := tm.prefix
_, exists := tm.funcToTyps[funcName]
_, isreserved := tm.reserved[funcName]
for exists || isreserved {
if i > len(name) {
funcName = tm.prefix + "_" + name + strconv.Itoa(i)
} else {
funcName = tm.prefix + "_" + name[:i]
}
i++
_, exists = tm.funcToTyps[funcName]
_, isreserved = tm.reserved[funcName]
}
return funcName
}
func eq(this, that []types.Type) bool {
if len(this) != len(that) {
return false
}
for i, t := range this {
if !types.AssignableTo(types.Default(t), types.Default(that[i])) {
return false
}
}
return true
}
func (tm *typesMap) nameOf(typs []types.Type) (string, bool) {
for _, t := range typs {
if n, ok := t.(*types.Named); ok {
pkg := n.Obj().Pkg()
if pkg != nil {
tm.qual(pkg)
}
}
}
for name, ts := range tm.funcToTyps {
if eq(typs, ts) {
return name, true
}
}
return "", false
}
func (tm *typesMap) Generating(typs ...types.Type) {
name, ok := tm.nameOf(typs)
if !ok {
panic(fmt.Sprintf("generating unknown %s for types: %v", tm.prefix, typs))
}
tm.generated[name] = true
}
func (tm *typesMap) isGenerated(typs []types.Type) bool {
name, ok := tm.nameOf(typs)
if !ok {
return false
}
return tm.generated[name]
}
func (tm *typesMap) ToGenerate() [][]types.Type {
typss := make([][]types.Type, 0, len(tm.typss))
for i, typs := range tm.typss {
if !tm.isGenerated(typs) {
typss = append(typss, tm.typss[i])
}
}
return typss
}
func (tm *typesMap) Done() bool {
for _, typs := range tm.typss {
if !tm.isGenerated(typs) {
return false
}
}
return true
}