2019-02-23 09:20:04 +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 (
|
|
|
|
"go/types"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// goPackage is the fake package that contains all our standard slice / map
|
|
|
|
// types that we export
|
|
|
|
var goPackage *Package
|
|
|
|
|
|
|
|
// makeGoPackage
|
|
|
|
func makeGoPackage() {
|
|
|
|
gopk := types.NewPackage("go", "go")
|
|
|
|
goPackage = &Package{pkg: gopk, syms: universe, objs: map[string]Object{}}
|
|
|
|
Packages = append(Packages, goPackage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// addStdSliceMaps adds std Slice and Map types to universe
|
|
|
|
func addStdSliceMaps() {
|
|
|
|
makeGoPackage()
|
|
|
|
gopk := goPackage.pkg
|
2019-02-24 09:22:19 +00:00
|
|
|
sltyps := []string{"int", "int64", "int32", "int16", "int8", "uint", "uint64", "uint32", "uint16", "uint8", "bool", "byte", "rune", "float64", "float32", "string"}
|
|
|
|
for _, tn := range sltyps {
|
|
|
|
universe.addSliceType(gopk, nil, types.NewSlice(universe.sym(tn).gotyp), skType, "Slice_"+tn, "[]"+tn)
|
|
|
|
}
|
2019-02-23 09:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stdBasicTypes returns the basic int, float etc types as symbols
|
|
|
|
func stdBasicTypes() map[string]*symbol {
|
|
|
|
look := types.Universe.Lookup
|
|
|
|
syms := map[string]*symbol{
|
|
|
|
"bool": {
|
|
|
|
gopkg: look("bool").Pkg(),
|
|
|
|
goobj: look("bool"),
|
|
|
|
gotyp: look("bool").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "bool",
|
|
|
|
id: "bool",
|
|
|
|
cgoname: "C.char",
|
2019-03-06 10:41:03 +00:00
|
|
|
cpyname: "bool",
|
2019-02-23 09:20:04 +00:00
|
|
|
pysig: "bool",
|
|
|
|
go2py: "boolGoToPy",
|
|
|
|
py2go: "boolPyToGo",
|
|
|
|
zval: "false",
|
2019-06-07 09:55:03 +00:00
|
|
|
pyfmt: "", // TODO:
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"byte": {
|
|
|
|
gopkg: look("byte").Pkg(),
|
|
|
|
goobj: look("byte"),
|
|
|
|
gotyp: look("byte").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "byte",
|
|
|
|
id: "int",
|
|
|
|
cpyname: "uint8_t",
|
|
|
|
cgoname: "C.char",
|
|
|
|
pysig: "int", // FIXME(sbinet) py2/py3
|
|
|
|
go2py: "C.char",
|
|
|
|
py2go: "byte",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "b",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"int": {
|
|
|
|
gopkg: look("int").Pkg(),
|
|
|
|
goobj: look("int"),
|
|
|
|
gotyp: look("int").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "int",
|
|
|
|
id: "int",
|
|
|
|
cpyname: "int",
|
|
|
|
cgoname: "C.long", // see below for 64 bit version
|
|
|
|
pysig: "int",
|
|
|
|
go2py: "C.long",
|
|
|
|
py2go: "int",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "i",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"int8": {
|
|
|
|
gopkg: look("int8").Pkg(),
|
|
|
|
goobj: look("int8"),
|
|
|
|
gotyp: look("int8").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "int8",
|
|
|
|
id: "int",
|
|
|
|
cpyname: "int8_t",
|
|
|
|
cgoname: "C.char",
|
|
|
|
pysig: "int",
|
|
|
|
go2py: "C.char",
|
|
|
|
py2go: "int8",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "b",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"int16": {
|
|
|
|
gopkg: look("int16").Pkg(),
|
|
|
|
goobj: look("int16"),
|
|
|
|
gotyp: look("int16").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "int16",
|
|
|
|
id: "int16",
|
|
|
|
cpyname: "int16_t",
|
|
|
|
cgoname: "C.short",
|
|
|
|
pysig: "int",
|
|
|
|
go2py: "C.short",
|
|
|
|
py2go: "int16",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "h",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"int32": {
|
|
|
|
gopkg: look("int32").Pkg(),
|
|
|
|
goobj: look("int32"),
|
|
|
|
gotyp: look("int32").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "int32",
|
|
|
|
id: "int",
|
|
|
|
cpyname: "int32_t",
|
|
|
|
cgoname: "C.long",
|
|
|
|
pysig: "int",
|
|
|
|
go2py: "C.long",
|
|
|
|
py2go: "int32",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "i",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"int64": {
|
|
|
|
gopkg: look("int64").Pkg(),
|
|
|
|
goobj: look("int64"),
|
|
|
|
gotyp: look("int64").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "int64",
|
|
|
|
id: "int64",
|
|
|
|
cpyname: "int64_t",
|
|
|
|
cgoname: "C.longlong",
|
|
|
|
pysig: "long",
|
|
|
|
go2py: "C.longlong",
|
|
|
|
py2go: "int64",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "k",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"uint": {
|
|
|
|
gopkg: look("uint").Pkg(),
|
|
|
|
goobj: look("uint"),
|
|
|
|
gotyp: look("uint").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "uint",
|
|
|
|
id: "uint",
|
|
|
|
cpyname: "unsigned int",
|
|
|
|
cgoname: "C.uint",
|
|
|
|
pysig: "int",
|
|
|
|
go2py: "C.uint",
|
|
|
|
py2go: "uint",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "I",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"uint8": {
|
|
|
|
gopkg: look("uint8").Pkg(),
|
|
|
|
goobj: look("uint8"),
|
|
|
|
gotyp: look("uint8").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "uint8",
|
|
|
|
id: "uint8",
|
|
|
|
cpyname: "uint8_t",
|
|
|
|
cgoname: "C.uchar",
|
|
|
|
pysig: "int",
|
|
|
|
go2py: "C.uchar",
|
|
|
|
py2go: "uint8",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "B",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"uint16": {
|
|
|
|
gopkg: look("uint16").Pkg(),
|
|
|
|
goobj: look("uint16"),
|
|
|
|
gotyp: look("uint16").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "uint16",
|
|
|
|
id: "uint16",
|
|
|
|
cpyname: "uint16_t",
|
|
|
|
cgoname: "C.ushort",
|
|
|
|
pysig: "int",
|
|
|
|
go2py: "C.ushort",
|
|
|
|
py2go: "uint16",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "H",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"uint32": {
|
|
|
|
gopkg: look("uint32").Pkg(),
|
|
|
|
goobj: look("uint32"),
|
|
|
|
gotyp: look("uint32").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "uint32",
|
|
|
|
id: "uint32",
|
|
|
|
cpyname: "uint32_t",
|
|
|
|
cgoname: "C.ulong",
|
|
|
|
pysig: "long",
|
|
|
|
go2py: "C.ulong",
|
|
|
|
py2go: "uint32",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "I",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"uint64": {
|
|
|
|
gopkg: look("uint64").Pkg(),
|
|
|
|
goobj: look("uint64"),
|
|
|
|
gotyp: look("uint64").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "uint64",
|
|
|
|
id: "uint64",
|
|
|
|
cpyname: "uint64_t",
|
|
|
|
cgoname: "C.ulonglong",
|
|
|
|
pysig: "long",
|
|
|
|
go2py: "C.ulonglong",
|
|
|
|
py2go: "uint64",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "K",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
2019-02-24 09:22:19 +00:00
|
|
|
"uintptr": {
|
|
|
|
gopkg: look("uintptr").Pkg(),
|
|
|
|
goobj: look("uintptr"),
|
|
|
|
gotyp: look("uintptr").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "uintptr",
|
|
|
|
id: "uintptr",
|
|
|
|
cpyname: "uint64_t",
|
|
|
|
cgoname: "C.ulonglong",
|
|
|
|
pysig: "long",
|
|
|
|
go2py: "C.ulonglong",
|
|
|
|
py2go: "uintptr",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "K",
|
2019-02-24 09:22:19 +00:00
|
|
|
},
|
|
|
|
|
2019-02-23 09:20:04 +00:00
|
|
|
"float32": {
|
|
|
|
gopkg: look("float32").Pkg(),
|
|
|
|
goobj: look("float32"),
|
|
|
|
gotyp: look("float32").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "float32",
|
|
|
|
id: "float32",
|
|
|
|
cpyname: "float",
|
|
|
|
cgoname: "C.float",
|
|
|
|
pysig: "float",
|
|
|
|
go2py: "C.float",
|
|
|
|
py2go: "float32",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "f",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"float64": {
|
|
|
|
gopkg: look("float64").Pkg(),
|
|
|
|
goobj: look("float64"),
|
|
|
|
gotyp: look("float64").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "float64",
|
|
|
|
id: "float64",
|
|
|
|
cpyname: "double",
|
|
|
|
cgoname: "C.double",
|
|
|
|
pysig: "float",
|
|
|
|
go2py: "C.double",
|
|
|
|
py2go: "float64",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "d",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"complex64": {
|
|
|
|
gopkg: look("complex64").Pkg(),
|
|
|
|
goobj: look("complex64"),
|
|
|
|
gotyp: look("complex64").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "complex64",
|
|
|
|
id: "complex64",
|
2019-03-06 10:41:03 +00:00
|
|
|
cpyname: "PyObject*",
|
|
|
|
cgoname: "*C.PyObject",
|
2019-02-23 09:20:04 +00:00
|
|
|
pysig: "complex",
|
2019-03-06 10:41:03 +00:00
|
|
|
go2py: "complex64GoToPy",
|
|
|
|
py2go: "complex64PyToGo",
|
2019-02-23 09:20:04 +00:00
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "O&",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"complex128": {
|
|
|
|
gopkg: look("complex128").Pkg(),
|
|
|
|
goobj: look("complex128"),
|
|
|
|
gotyp: look("complex128").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "complex128",
|
|
|
|
id: "complex128",
|
2019-03-06 10:41:03 +00:00
|
|
|
cpyname: "PyObject*",
|
|
|
|
cgoname: "*C.PyObject",
|
2019-02-23 09:20:04 +00:00
|
|
|
pysig: "complex",
|
2019-03-06 10:41:03 +00:00
|
|
|
go2py: "complex128GoToPy",
|
|
|
|
py2go: "complex128PyToGo",
|
2019-02-23 09:20:04 +00:00
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "O&",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"string": {
|
|
|
|
gopkg: look("string").Pkg(),
|
|
|
|
goobj: look("string"),
|
|
|
|
gotyp: look("string").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "string",
|
|
|
|
id: "string",
|
|
|
|
cpyname: "char*",
|
|
|
|
cgoname: "*C.char",
|
|
|
|
pysig: "str",
|
|
|
|
go2py: "C.CString",
|
|
|
|
py2go: "C.GoString",
|
|
|
|
zval: `""`,
|
2019-02-28 19:19:14 +00:00
|
|
|
pyfmt: "s",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"rune": { // FIXME(sbinet) py2/py3
|
|
|
|
gopkg: look("rune").Pkg(),
|
|
|
|
goobj: look("rune"),
|
|
|
|
gotyp: look("rune").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "rune",
|
|
|
|
id: "rune",
|
|
|
|
cpyname: "int32_t",
|
|
|
|
cgoname: "C.long",
|
|
|
|
pysig: "str",
|
|
|
|
go2py: "C.long",
|
|
|
|
py2go: "rune",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "i",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"error": {
|
|
|
|
gopkg: look("error").Pkg(),
|
|
|
|
goobj: look("error"),
|
|
|
|
gotyp: look("error").Type(),
|
|
|
|
kind: skType | skInterface,
|
|
|
|
goname: "error",
|
|
|
|
id: "error",
|
|
|
|
cpyname: "char*",
|
|
|
|
cgoname: "*C.char",
|
|
|
|
pysig: "str",
|
|
|
|
go2py: "C.CString",
|
|
|
|
py2go: "C.GoString",
|
|
|
|
zval: `""`,
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "O&",
|
2019-02-23 09:20:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.TypeOf(int(0)).Size() == 8 {
|
|
|
|
syms["int"] = &symbol{
|
|
|
|
gopkg: look("int").Pkg(),
|
|
|
|
goobj: look("int"),
|
|
|
|
gotyp: look("int").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "int",
|
|
|
|
id: "int",
|
|
|
|
cpyname: "int64_t",
|
|
|
|
cgoname: "C.longlong",
|
|
|
|
pysig: "int",
|
|
|
|
go2py: "C.longlong",
|
|
|
|
py2go: "int",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "k",
|
2019-02-23 09:20:04 +00:00
|
|
|
}
|
|
|
|
syms["uint"] = &symbol{
|
|
|
|
gopkg: look("uint").Pkg(),
|
|
|
|
goobj: look("uint"),
|
|
|
|
gotyp: look("uint").Type(),
|
|
|
|
kind: skType | skBasic,
|
|
|
|
goname: "uint",
|
|
|
|
id: "uint",
|
|
|
|
cpyname: "uint64_t",
|
|
|
|
cgoname: "C.ulonglong",
|
|
|
|
pysig: "int",
|
|
|
|
go2py: "C.ulonglong",
|
|
|
|
py2go: "uint",
|
|
|
|
zval: "0",
|
2019-02-28 06:44:20 +00:00
|
|
|
pyfmt: "K",
|
2019-02-23 09:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// these are defined in: https://godoc.org/go/types
|
|
|
|
for _, o := range []struct {
|
|
|
|
kind types.BasicKind
|
|
|
|
tname string
|
|
|
|
uname string
|
|
|
|
}{
|
|
|
|
{types.UntypedBool, "bool", "bool"},
|
|
|
|
{types.UntypedInt, "int", "int"},
|
|
|
|
{types.UntypedRune, "rune", "rune"},
|
|
|
|
{types.UntypedFloat, "float64", "float"},
|
|
|
|
{types.UntypedComplex, "complex128", "complex"},
|
|
|
|
{types.UntypedString, "string", "string"},
|
|
|
|
//FIXME(sbinet): what should be the python equivalent?
|
|
|
|
//{types.UntypedNil, "nil", "nil"},
|
|
|
|
} {
|
|
|
|
sym := *syms[o.tname]
|
|
|
|
n := "untyped " + o.uname
|
|
|
|
syms[n] = &sym
|
|
|
|
}
|
|
|
|
|
|
|
|
return syms
|
|
|
|
}
|