This repository was archived by the owner on Oct 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
629 lines (530 loc) · 12 KB
/
types.go
File metadata and controls
629 lines (530 loc) · 12 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
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
// Copyright 2015 The go-python 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 bind
import (
"fmt"
"go/types"
"sort"
"strconv"
)
type Object interface {
Package() *Package
ID() string
Doc() string
GoName() string
}
type Type interface {
Object
GoType() types.Type
}
func needWrapType(typ types.Type) bool {
switch typ := typ.(type) {
case *types.Basic:
return false
case *types.Struct:
return true
case *types.Named:
switch ut := typ.Underlying().(type) {
case *types.Basic:
return false
default:
return needWrapType(ut)
}
case *types.Array:
return true
case *types.Map:
return true
case *types.Slice:
return true
case *types.Interface:
wrap := true
if typ.Underlying() == universe.syms["error"].GoType().Underlying() {
wrap = false
}
return wrap
case *types.Signature:
return true
case *types.Pointer:
return needWrapType(typ.Elem())
}
return false
}
///////////////////////////////////////////////////////////////////////////////////
// Struct
// Protocol encodes the various protocols a python type may implement
type Protocol int
const (
ProtoStringer Protocol = 1 << iota
)
// Struct collects information about a go struct.
type Struct struct {
pkg *Package
sym *symbol
obj *types.TypeName
id string
doc string
ctors []*Func
meths []*Func
idx int // index position in list of structs
prots Protocol
}
func newStruct(p *Package, obj *types.TypeName) (*Struct, error) {
sym := p.syms.symtype(obj.Type())
if sym == nil {
return nil, fmt.Errorf("no such object [%s] in symbols table", obj.Id())
}
sym.doc = p.getDoc("", obj)
s := &Struct{
pkg: p,
sym: sym,
obj: obj,
}
return s, nil
}
func (s *Struct) Obj() *types.TypeName {
return s.obj
}
func (s *Struct) Package() *Package {
return s.pkg
}
func (s *Struct) ID() string {
return s.sym.id
}
func (s *Struct) Doc() string {
return s.sym.doc
}
func (s *Struct) GoType() types.Type {
return s.sym.GoType()
}
func (s *Struct) GoName() string {
return s.sym.goname
}
func (s *Struct) Struct() *types.Struct {
return s.sym.GoType().Underlying().(*types.Struct)
}
// FirstEmbed returns the first field if it is embedded,
// supporting convention of placing embedded "parent" types first
func (s *Struct) FirstEmbed() *symbol {
st := s.Struct()
numFields := st.NumFields()
if numFields == 0 {
return nil
}
f := s.Struct().Field(0)
if !f.Embedded() {
return nil
}
ftyp := current.symtype(f.Type())
if ftyp == nil {
return nil
}
return ftyp
}
///////////////////////////////////////////////////////////////////////////////////
// Interface
// Interface collects information about a go interface.
type Interface struct {
pkg *Package
sym *symbol
obj *types.TypeName
id string
doc string
meths []*Func
}
func newInterface(p *Package, obj *types.TypeName) (*Interface, error) {
sym := p.syms.symtype(obj.Type())
if sym == nil {
return nil, fmt.Errorf("no such object [%s] in symbols table", obj.Id())
}
sym.doc = p.getDoc("", obj)
s := &Interface{
pkg: p,
sym: sym,
obj: obj,
}
return s, nil
}
func (it *Interface) Package() *Package {
return it.pkg
}
func (it *Interface) ID() string {
return it.sym.id
}
func (it *Interface) Doc() string {
return it.sym.doc
}
func (it *Interface) GoType() types.Type {
return it.sym.GoType()
}
func (it *Interface) GoName() string {
return it.sym.goname
}
func (it *Interface) Interface() *types.Interface {
return it.sym.GoType().Underlying().(*types.Interface)
}
///////////////////////////////////////////////////////////////////////////////////
// Slice
// Slice collects information about a go slice.
type Slice struct {
pkg *Package
sym *symbol
obj *types.TypeName
id string
doc string
meths []*Func
prots Protocol
}
func newSlice(p *Package, obj *types.TypeName) (*Slice, error) {
sym := p.syms.symtype(obj.Type())
if sym == nil {
return nil, fmt.Errorf("no such object [%s] in symbols table", obj.Id())
}
sym.doc = p.getDoc("", obj)
s := &Slice{
pkg: p,
sym: sym,
obj: obj,
}
return s, nil
}
func (it *Slice) Package() *Package {
return it.pkg
}
func (it *Slice) ID() string {
return it.sym.id
}
func (it *Slice) Doc() string {
return it.sym.doc
}
func (it *Slice) GoType() types.Type {
return it.sym.GoType()
}
func (it *Slice) GoName() string {
return it.sym.goname
}
func (it *Slice) Slice() *types.Slice {
return it.sym.GoType().Underlying().(*types.Slice)
}
///////////////////////////////////////////////////////////////////////////////////
// Map
// Map collects information about a go map.
type Map struct {
pkg *Package
sym *symbol
obj *types.TypeName
id string
doc string
meths []*Func
prots Protocol
}
func newMap(p *Package, obj *types.TypeName) (*Map, error) {
sym := p.syms.symtype(obj.Type())
if sym == nil {
return nil, fmt.Errorf("no such object [%s] in symbols table", obj.Id())
}
sym.doc = p.getDoc("", obj)
s := &Map{
pkg: p,
sym: sym,
obj: obj,
}
return s, nil
}
func (it *Map) Package() *Package {
return it.pkg
}
func (it *Map) ID() string {
return it.sym.id
}
func (it *Map) Doc() string {
return it.sym.doc
}
func (it *Map) GoType() types.Type {
return it.sym.GoType()
}
func (it *Map) GoName() string {
return it.sym.goname
}
func (it *Map) Map() *types.Map {
return it.sym.GoType().Underlying().(*types.Map)
}
///////////////////////////////////////////////////////////////////////////////////
// Signature
// A Signature represents a (non-builtin) function or method type.
type Signature struct {
ret []*Var
args []*Var
recv *Var
}
func newSignatureFrom(pkg *Package, sig *types.Signature) (*Signature, error) {
var recv *Var
var err error
if sig.Recv() != nil {
recv, err = newVarFrom(pkg, sig.Recv())
if err != nil {
return nil, err
}
}
rv, err := newVarsFrom(pkg, sig.Results())
if err != nil {
return nil, err
}
av, err := newVarsFrom(pkg, sig.Params())
if err != nil {
return nil, err
}
return &Signature{
ret: rv,
args: av,
recv: recv,
}, nil
}
func newSignature(pkg *Package, recv *Var, params, results []*Var) *Signature {
return &Signature{
ret: results,
args: params,
recv: recv,
}
}
func (sig *Signature) Results() []*Var {
return sig.ret
}
func (sig *Signature) Params() []*Var {
return sig.args
}
func (sig *Signature) Recv() *Var {
return sig.recv
}
///////////////////////////////////////////////////////////////////////////////////
// Func
// Func collects information about a go func/method.
type Func struct {
pkg *Package
sig *Signature
typ types.Type
obj types.Object
name string
id string
doc string
ret types.Type // return type, if any
err bool // true if original go func has comma-error
ctor bool // true if this is a newXXX function
hasfun bool // true if this function has a function argument
isVariadic bool // True, if this is a variadic function.
}
func newFuncFrom(p *Package, parent string, obj types.Object, sig *types.Signature) (*Func, error) {
ret, haserr, hasfun, err := isPyCompatFunc(sig)
if err != nil {
return nil, err
}
id := obj.Pkg().Name() + "_" + obj.Name()
if parent != "" {
id = obj.Pkg().Name() + "_" + parent + "_" + obj.Name()
}
sv, err := newSignatureFrom(p, sig)
if err != nil {
return nil, err
}
return &Func{
obj: obj,
pkg: p,
sig: sv,
typ: obj.Type(),
name: obj.Name(),
id: id,
doc: p.getDoc(parent, obj),
ret: ret,
err: haserr,
hasfun: hasfun,
isVariadic: sig.Variadic(),
}, nil
// TODO: could optimize by generating code once for each type of callback
// but probably not worth the effort required to link everything up..
// if hasfun {
// args := sv.args
// for i := range args {
// arg := args[i]
// if arg.sym.isSignature() {
// // TODO: need to make sure not already on the list, etc
// p.calls = append(p.calls, arg)
// }
// }
// }
} //
func (f *Func) Obj() types.Object {
return f.obj
}
func (f *Func) Package() *Package {
return f.pkg
}
func (f *Func) ID() string {
return f.id
}
func (f *Func) Doc() string {
return f.doc
}
func (f *Func) GoType() types.Type {
return f.typ
}
func (f *Func) GoName() string {
return f.name
}
func (f *Func) GoFmt() string {
return f.pkg.Name() + "." + f.name
}
func (f *Func) Signature() *Signature {
return f.sig
}
func (f *Func) Return() types.Type {
return f.ret
}
///////////////////////////////////////////////////////////////////////////////////
// Const
type Const struct {
pkg *Package
sym *symbol
obj *types.Const
id string
doc string
val string
}
func newConst(p *Package, o *types.Const) (*Const, error) {
pkg := o.Pkg()
sym := p.syms.symtype(o.Type())
id := pkg.Name() + "_" + o.Name()
doc := p.getDoc("", o)
val := o.Val().String()
return &Const{
pkg: p,
sym: sym,
obj: o,
id: id,
doc: doc,
val: val,
}, nil
}
func (c *Const) ID() string { return c.id }
func (c *Const) Doc() string { return c.doc }
func (c *Const) GoName() string { return c.obj.Name() }
func (c *Const) GoType() types.Type { return c.obj.Type() }
///////////////////////////////////////////////////////////////////////////////////
// Enum
type Enum struct {
pkg *Package
sym *symbol
obj *types.Const // first one -- random..
typ *types.Named
id string
doc string
items []*Const
}
func newEnum(p *Package, o *types.Const) (*Enum, error) {
pkg := o.Pkg()
sym := p.syms.symtype(o.Type())
id := pkg.Name() + "_" + o.Name()
typ := o.Type().(*types.Named)
doc := p.getDoc("", typ.Obj())
e := &Enum{
pkg: p,
sym: sym,
obj: o,
typ: typ,
id: id,
doc: doc,
}
e.AddConst(p, o)
return e, nil
}
func (e *Enum) ID() string { return e.id }
func (e *Enum) Doc() string { return e.doc }
func (e *Enum) GoName() string { return e.obj.Name() }
func (e *Enum) GoType() types.Type { return e.obj.Type() }
func (e *Enum) AddConst(p *Package, o *types.Const) (*Const, error) {
c, err := newConst(p, o)
if err == nil {
e.items = append(e.items, c)
}
return c, err
}
func (e *Enum) SortConsts() {
sort.Slice(e.items, func(i, j int) bool {
iv, _ := strconv.Atoi(e.items[i].val)
jv, _ := strconv.Atoi(e.items[j].val)
return iv < jv
})
}
///////////////////////////////////////////////////////////////////////////////////
// Var
type Var struct {
pkg *Package
sym *symbol // symbol associated with var's type
id string
doc string
name string
}
func (v *Var) Name() string {
return v.name
}
func newVar(p *Package, typ types.Type, objname, name, doc string) (*Var, error) {
sym := p.syms.symtype(typ)
if sym == nil {
typname, _, _ := p.syms.typeNamePkg(typ)
typobj := p.syms.pkg.Scope().Lookup(typname)
if typobj != nil {
p.syms.addSymbol(typobj)
} else {
if ntyp, ok := typ.(*types.Named); ok {
p.syms.addType(ntyp.Obj(), typ)
} else {
p.syms.addType(nil, typ)
}
}
sym = p.syms.symtype(typ)
if sym == nil {
return nil, fmt.Errorf("could not find symbol for type: %s!", typ.String())
}
}
return &Var{
pkg: p,
sym: sym,
id: p.Name() + "_" + objname,
doc: doc,
name: name,
}, nil
}
func newVarsFrom(p *Package, tuple *types.Tuple) ([]*Var, error) {
vars := make([]*Var, 0, tuple.Len())
var lsterr error
for i := 0; i < tuple.Len(); i++ {
nv, err := newVarFrom(p, tuple.At(i))
if err != nil {
lsterr = err
} else {
vars = append(vars, nv)
}
}
return vars, lsterr
}
func newVarFrom(p *Package, v *types.Var) (*Var, error) {
return newVar(p, v.Type(), v.Name(), v.Name(), p.getDoc("", v))
}
func getTypeString(t types.Type) string {
return types.TypeString(t, func(*types.Package) string { return " " })
}
func (v *Var) GoType() types.Type {
return v.sym.gotyp
}
func (v *Var) CType() string {
return v.sym.cpyname
}
func (v *Var) CGoType() string {
return v.sym.cgoname
}
func (v *Var) isGoString() bool {
switch typ := v.GoType().(type) {
case *types.Basic:
return typ.Kind() == types.String
}
return false
}