-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproto.go
More file actions
274 lines (247 loc) · 8.16 KB
/
proto.go
File metadata and controls
274 lines (247 loc) · 8.16 KB
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
package types
import (
"fmt"
"go/ast"
"reflect"
"regexp"
"strings"
"github.com/redneckbeard/thanos/bst"
)
// Holds data for methods implemented "natively" in Thanos, initially targeting
// built-in methods on native Ruby datastructures and types. This of course
// means all the stuff that comes in Enumerable, which involves doing things
// with blocks that in theory _could_ be done with anonymous functions in Go
// but translates most efficiently and idiomatically to simple for loops.
// KwargSpec describes a single keyword argument accepted by a MethodSpec.
// Transforms receive kwargs appended after positional args in KwargsSpec order.
// A zero TypeExpr (Expr == nil) means the kwarg was not provided by the caller.
type KwargSpec struct {
Name string
Type Type
Default ast.Expr // nil = kwarg not provided
}
type MethodSpec struct {
// When inferring the return types for methods that take a block, we must
// consider the return type of the block (since blocks cannot explicitly
// return, this means resolving the type of the last expression in the block
// -- no plans to support `break` or `next` yet as I'm honestly unsure if
// I've ever seen them in the wild) and the receiver, since in a composite
// type the inner type will determine or factor into the return type. Args
// are often not given with Enumerable but when they are that can determine
// the return type.
ReturnType ReturnTypeFunc
// For any methods where we're creating a MethodSpec, we don't have the
// implementation to examine in Ruby and see what types of args get passed to
// `block.call`. Therefore we must first provide a way to compute what the
// types of the args will be so that we can use them to seed inference of the
// return type of the block.
blockArgs func(Type, []Type) []Type
TransformAST TransformFunc
TransformStmtAST TransformFunc
// RefineVariable allows methods to update the type of their receiver variable
// This is used for operations like << on empty arrays to refine Array(AnyType) to concrete types
RefineVariable RefineVariableFunc
// KwargsSpec declares named keyword arguments this method accepts.
// Transforms receive [positional..., kwarg1, kwarg2, ...] where kwargs
// are in KwargsSpec declaration order. Missing kwargs have zero TypeExpr.
KwargsSpec []KwargSpec
}
func (ms *MethodSpec) SetBlockArgs(f func(Type, []Type) []Type) {
ms.blockArgs = f
}
func (ms *MethodSpec) BlockArgs(r Type, args []Type) []Type {
if ms.blockArgs == nil {
return nil
}
return ms.blockArgs(r, args)
}
type ReturnTypeFunc func(receiverType Type, blockReturnType Type, args []Type) (Type, error)
type TransformFunc func(TypeExpr, []TypeExpr, *Block, bst.IdentTracker) Transform
type RefineVariableFunc func(receiverName string, newType Type, scope interface{})
type Transform struct {
Stmts []ast.Stmt
Expr ast.Expr
Imports []string
Finalizers []ast.Stmt // appended to end of main (e.g., http.ListenAndServe)
}
type proto struct {
class, parent string
methods map[string]MethodSpec
bracketAliases map[Type]string
registry *classRegistry
initialized bool
_type Type
}
func newProto(class, parent string, registry *classRegistry) *proto {
p := &proto{
class: class,
parent: parent,
methods: make(map[string]MethodSpec),
bracketAliases: make(map[Type]string),
registry: registry,
}
return p
}
func (p *proto) ClassName() string { return p.class }
func (p *proto) UserDefined() bool { return false }
func (p *proto) Methods() map[string]MethodSpec { return p.methods }
func (p *proto) SelfDef(m string, spec MethodSpec) {
go func() {
for {
class, err := p.registry.Get(p.class)
if err == nil {
class.Def(m, spec)
break
}
}
}()
}
func (p *proto) Resolve(m string, classMethod bool) (MethodSpec, bool) {
method, has := p.methods[m]
if !has {
className := p.class
if className == "" {
className = "Object"
}
if p.registry == nil {
panic("tried to Resolve method but registry not set")
}
class, err := p.registry.Get(className)
if err != nil {
panic(err)
}
for class.parent != nil {
class = class.parent
if classMethod {
method, has = class.proto.methods[m]
} else {
method, has = class.Instance.Methods()[m]
}
if has {
return method, has
}
}
// Kernel is the final fallback for instance methods, matching Ruby's
// method resolution order where Kernel is included in every object.
if !classMethod && p.class != "Kernel" {
method, has = KernelType.proto.methods[m]
}
}
return method, has
}
func (p *proto) MustResolve(m string, classMethod bool) MethodSpec {
method, has := p.Resolve(m, classMethod)
methodType := "instance"
if classMethod {
methodType = "class"
}
if !has {
panic(fmt.Errorf("Could not resolve %s method '%s' on class '%s'", methodType, m, p.class))
}
return method
}
func (p *proto) HasMethod(m string, classMethod bool) bool {
_, has := p.Resolve(m, classMethod)
return has
}
func (p *proto) Def(m string, spec MethodSpec) {
p.methods[m] = spec
}
func (p *proto) Alias(existingMethod, newMethod string) {
panic("client types must call `MakeAlias`")
}
func (p *proto) MakeAlias(existingMethod, newMethod string, classMethod bool) {
p.methods[newMethod] = p.methods[existingMethod]
}
func (p *proto) IsMultiple() bool { return false }
func (p *proto) GetMethodSpec(m string) (MethodSpec, bool) {
return p.Resolve(m, false)
}
func (p *proto) GenerateMethods(iface interface{}, exclusions ...string) {
t := reflect.TypeOf(iface)
// generics aren't yet something that can be introspected with reflect, so we
// make the probably bad assumption here that the type parameter supplied in
// the interface{} value above is the only type parameter this type has,
// allowing us to use it as a stand-in for a true type parameter provided by
// the reflect package.
name, typeParam := typeName(t)
for i := 0; i < t.NumMethod(); i++ {
m := t.Method(i)
var excluded bool
for _, exclusion := range exclusions {
if m.Name == exclusion {
excluded = true
break
}
}
if excluded {
continue
}
mt := m.Type
v := reflect.Indirect(reflect.ValueOf(iface)).Type()
methodName := ToSnakeCase(m.Name)
if strings.HasSuffix(methodName, "_q") {
methodName = strings.TrimSuffix(methodName, "_q") + "?"
}
p.Def(methodName, MethodSpec{
ReturnType: func(mt reflect.Type, receiverName, typeParam string) ReturnTypeFunc {
return func(receiverType Type, blockReturnType Type, args []Type) (Type, error) {
var retType Type
if mt.NumOut() > 1 {
multiple := Multiple{}
for j := 0; j < mt.NumOut(); j++ {
rt := mt.Out(j)
if tt := getGenericType(rt, receiverType, typeParam); tt != nil {
multiple = append(multiple, tt)
} else {
multiple = append(multiple, reflectTypeToThanosType(rt))
}
}
retType = multiple
} else {
rt := mt.Out(0)
if tt := getGenericType(rt, receiverType, typeParam); tt != nil {
retType = tt
} else {
retType = reflectTypeToThanosType(rt)
}
}
return retType, nil
}
}(mt, name, typeParam),
TransformAST: func(name, path string) TransformFunc {
return func(rcvr TypeExpr, args []TypeExpr, blk *Block, it bst.IdentTracker) Transform {
argExprs := []ast.Expr{}
for _, a := range args {
argExprs = append(argExprs, a.Expr)
}
return Transform{
Expr: bst.Call(rcvr.Expr, name, argExprs...),
Imports: []string{path},
}
}
}(m.Name, v.PkgPath()),
})
}
}
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
func ToSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
func reflectTypeToThanosType(t reflect.Type) Type {
switch t.Kind() {
case reflect.Array, reflect.Slice:
return NewArray(reflectTypeToThanosType(t.Elem()))
case reflect.Map:
return NewHash(reflectTypeToThanosType(t.Key()), reflectTypeToThanosType(t.Elem()))
default:
if tt, exists := goTypeMap[t.Kind()]; exists {
return tt
} else {
return nil
}
}
}