2015-07-24 14:16:31 +00:00
|
|
|
// 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 (
|
2015-07-29 09:43:31 +00:00
|
|
|
"reflect"
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
"golang.org/x/tools/go/types"
|
|
|
|
)
|
|
|
|
|
2015-07-28 12:33:47 +00:00
|
|
|
type Object interface {
|
2015-07-28 10:38:11 +00:00
|
|
|
Package() *Package
|
2015-07-28 10:26:33 +00:00
|
|
|
ID() string
|
|
|
|
Doc() string
|
2015-07-28 12:33:47 +00:00
|
|
|
GoName() string
|
2015-07-28 10:26:33 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:33:47 +00:00
|
|
|
type Type interface {
|
|
|
|
Object
|
|
|
|
GoType() types.Type
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
func needWrapType(typ types.Type) bool {
|
|
|
|
switch typ.(type) {
|
|
|
|
case *types.Struct:
|
|
|
|
return true
|
|
|
|
case *types.Named:
|
|
|
|
switch typ.Underlying().(type) {
|
|
|
|
case *types.Struct:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func cTypeName(typ types.Type) string {
|
|
|
|
switch typ := typ.(type) {
|
|
|
|
case *types.Basic:
|
|
|
|
kind := typ.Kind()
|
|
|
|
o, ok := typedescr[kind]
|
|
|
|
if ok {
|
|
|
|
return o.ctype
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return typ.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cgoTypeName(typ types.Type) string {
|
|
|
|
switch typ := typ.(type) {
|
|
|
|
case *types.Basic:
|
|
|
|
kind := typ.Kind()
|
|
|
|
o, ok := typedescr[kind]
|
|
|
|
if ok {
|
|
|
|
return o.cgotype
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return typ.String()
|
|
|
|
}
|
|
|
|
|
2015-07-25 00:05:11 +00:00
|
|
|
func pyTypeName(typ types.Type) string {
|
|
|
|
switch typ := typ.(type) {
|
|
|
|
case *types.Basic:
|
|
|
|
kind := typ.Kind()
|
|
|
|
o, ok := typedescr[kind]
|
|
|
|
if ok {
|
|
|
|
return o.pysig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "object"
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
type typedesc struct {
|
|
|
|
ctype string
|
|
|
|
cgotype string
|
|
|
|
pyfmt string
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig string
|
2015-07-31 14:51:30 +00:00
|
|
|
c2py string // name of converter helper C->py
|
|
|
|
py2c string // name of converter helper for py->c
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (td typedesc) hasConverter() bool {
|
|
|
|
return td.c2py != "" || td.py2c != ""
|
2015-07-24 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 09:43:31 +00:00
|
|
|
var (
|
|
|
|
intsize = reflect.TypeOf(int(0)).Size()
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if intsize == 8 {
|
|
|
|
typedescr[types.Int] = typedesc{
|
|
|
|
ctype: "int64_t",
|
|
|
|
cgotype: "GoInt",
|
|
|
|
pyfmt: "k",
|
|
|
|
pysig: "int",
|
|
|
|
}
|
|
|
|
|
|
|
|
typedescr[types.Uint] = typedesc{
|
|
|
|
ctype: "uint64_t",
|
|
|
|
cgotype: "GoUint",
|
|
|
|
pyfmt: "K",
|
|
|
|
pysig: "int",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
var typedescr = map[types.BasicKind]typedesc{
|
|
|
|
types.Bool: typedesc{
|
|
|
|
ctype: "_Bool",
|
2015-07-31 14:51:30 +00:00
|
|
|
cgotype: "GoUint8",
|
|
|
|
pyfmt: "O&",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "bool",
|
2015-07-31 14:51:30 +00:00
|
|
|
c2py: "_cgopy_cnv_c2py_bool",
|
|
|
|
py2c: "_cgopy_cnv_py2c_bool",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Int: typedesc{
|
|
|
|
ctype: "int",
|
|
|
|
cgotype: "GoInt",
|
|
|
|
pyfmt: "i",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "int",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Int8: typedesc{
|
|
|
|
ctype: "int8_t",
|
|
|
|
cgotype: "GoInt8",
|
|
|
|
pyfmt: "c",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "int",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Int16: typedesc{
|
|
|
|
ctype: "int16_t",
|
|
|
|
cgotype: "GoInt16",
|
|
|
|
pyfmt: "h",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "int",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Int32: typedesc{
|
|
|
|
ctype: "int32_t",
|
|
|
|
cgotype: "GoInt32",
|
|
|
|
pyfmt: "i",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "long",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Int64: typedesc{
|
|
|
|
ctype: "int64_t",
|
|
|
|
cgotype: "GoInt64",
|
|
|
|
pyfmt: "k",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "long",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Uint: typedesc{
|
|
|
|
ctype: "unsigned int",
|
|
|
|
cgotype: "GoUint",
|
|
|
|
pyfmt: "I",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "int",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Uint8: typedesc{
|
|
|
|
ctype: "uint8_t",
|
|
|
|
cgotype: "GoUint8",
|
|
|
|
pyfmt: "b",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "int",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Uint16: typedesc{
|
|
|
|
ctype: "uint16_t",
|
|
|
|
cgotype: "GoUint16",
|
|
|
|
pyfmt: "H",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "int",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Uint32: typedesc{
|
|
|
|
ctype: "uint32_t",
|
|
|
|
cgotype: "GoUint32",
|
|
|
|
pyfmt: "I",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "long",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Uint64: typedesc{
|
|
|
|
ctype: "uint64_t",
|
|
|
|
cgotype: "GoUint64",
|
|
|
|
pyfmt: "K",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "long",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Float32: typedesc{
|
|
|
|
ctype: "float",
|
|
|
|
cgotype: "float",
|
|
|
|
pyfmt: "f",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "float",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Float64: typedesc{
|
|
|
|
ctype: "double",
|
|
|
|
cgotype: "double",
|
|
|
|
pyfmt: "d",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "float",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Complex64: typedesc{
|
|
|
|
ctype: "float complex",
|
|
|
|
cgotype: "GoComplex64",
|
|
|
|
pyfmt: "D",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "float",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.Complex128: typedesc{
|
|
|
|
ctype: "double complex",
|
|
|
|
cgotype: "GoComplex128",
|
|
|
|
pyfmt: "D",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "float",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.String: typedesc{
|
|
|
|
ctype: "const char*",
|
|
|
|
cgotype: "GoString",
|
2015-07-31 14:51:30 +00:00
|
|
|
pyfmt: "O&",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "str",
|
2015-07-31 14:51:30 +00:00
|
|
|
c2py: "_cgopy_cnv_c2py_string",
|
|
|
|
py2c: "_cgopy_cnv_py2c_string",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
types.UnsafePointer: typedesc{
|
|
|
|
ctype: "void*",
|
|
|
|
cgotype: "void*",
|
2015-07-31 14:51:30 +00:00
|
|
|
pyfmt: "O&",
|
2015-07-25 00:05:11 +00:00
|
|
|
pysig: "object",
|
2015-07-24 14:16:31 +00:00
|
|
|
},
|
|
|
|
}
|
2015-07-31 08:13:25 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
typedescr[types.UntypedBool] = typedescr[types.Bool]
|
|
|
|
typedescr[types.UntypedInt] = typedescr[types.Int]
|
|
|
|
typedescr[types.UntypedRune] = typedescr[types.Rune] // FIXME(sbinet)
|
|
|
|
typedescr[types.UntypedFloat] = typedescr[types.Float64]
|
|
|
|
typedescr[types.UntypedComplex] = typedescr[types.Complex128]
|
|
|
|
typedescr[types.UntypedString] = typedescr[types.String]
|
|
|
|
typedescr[types.UntypedNil] = typedescr[types.UnsafePointer] // FIXME(sbinet)
|
|
|
|
}
|