bind: initial support for top-level consts

Fixes #10
This commit is contained in:
Sebastien Binet 2015-07-31 10:13:25 +02:00
parent 6cd8c71b94
commit d9d855f797
4 changed files with 87 additions and 8 deletions

View File

@ -65,6 +65,10 @@ func (g *cpyGen) gen() error {
g.genFunc(f)
}
for _, c := range g.pkg.consts {
g.genConst(c)
}
g.impl.Printf("static PyMethodDef cpy_%s_methods[] = {\n", g.pkg.pkg.Name())
g.impl.Indent()
for _, f := range g.pkg.funcs {
@ -87,6 +91,13 @@ func (g *cpyGen) gen() error {
}
}
for _, c := range g.pkg.consts {
name := c.GoName()
g.impl.Printf("{%[1]q, %[2]s, METH_VARARGS, %[3]q},\n",
"Get"+name, "gopy_get_"+c.ID(), c.Doc(),
)
}
g.impl.Printf("{NULL, NULL, 0, NULL} /* Sentinel */\n")
g.impl.Outdent()
g.impl.Printf("};\n\n")
@ -781,6 +792,10 @@ func (g *cpyGen) genStructTPStr(cpy Struct) {
g.impl.Printf("}\n\n")
}
func (g *cpyGen) genConst(o Const) {
g.genFunc(o.f)
}
func (g *cpyGen) genPreamble() {
n := g.pkg.pkg.Name()
g.decl.Printf(cPreamble, n, g.pkg.pkg.Path(), filepath.Base(n))

View File

@ -88,6 +88,10 @@ func (g *goGen) gen() error {
g.genFunc(f)
}
for _, c := range g.pkg.consts {
g.genConst(c)
}
g.Printf("// buildmode=c-shared needs a 'main'\nfunc main() {}\n\n")
g.Printf("func init() {\n")
g.Indent()
@ -342,6 +346,20 @@ func (g *goGen) genMethodBody(s Struct, m Func) {
}
func (g *goGen) genConst(o Const) {
pkgname := o.obj.Pkg().Name()
tname := types.TypeString(o.obj.Type(), types.RelativeTo(o.obj.Pkg()))
if strings.HasPrefix(tname, "untyped ") {
tname = string(tname[len("untyped "):])
}
g.Printf("//export GoPy_get_%s\n", o.id)
g.Printf("func GoPy_get_%[1]s() %[2]s {\n", o.id, tname)
g.Indent()
g.Printf("return %s.%s\n", pkgname, o.obj.Name())
g.Outdent()
g.Printf("}\n\n")
}
func (g *goGen) genPreamble() {
n := g.pkg.pkg.Name()
g.Printf(goPreamble, n, g.pkg.pkg.Path(), filepath.Base(n))

View File

@ -20,6 +20,8 @@ type Package struct {
doc *doc.Package
objs map[string]Object
consts []Const
vars []*types.Var
structs []Struct
funcs []Func
}
@ -237,17 +239,11 @@ func (p *Package) process() error {
}
func (p *Package) addConst(obj *types.Const) {
//TODO(sbinet)
fmt.Fprintf(os.Stderr, "no yet supported: %v (%T)\n", obj, obj)
return
panic(fmt.Errorf("not yet supported: %v (%T)", obj, obj))
p.consts = append(p.consts, newConst(p, obj))
}
func (p *Package) addVar(obj *types.Var) {
//TODO(sbinet)
fmt.Fprintf(os.Stderr, "no yet supported: %v (%T)\n", obj, obj)
return
panic(fmt.Errorf("not yet supported: %v (%T)", obj, obj))
p.vars = append(p.vars, obj)
}
func (p *Package) addStruct(s Struct) {
@ -447,3 +443,43 @@ func (f Func) Signature() *Signature {
func (f Func) Return() types.Type {
return f.ret
}
type Const struct {
pkg *Package
obj *types.Const
id string
doc string
f Func
}
func newConst(p *Package, o *types.Const) Const {
pkg := o.Pkg()
id := pkg.Name() + "_" + o.Name()
doc := p.getDoc("", o)
res := []*Var{newVar(p, o.Type(), "ret", o.Name(), doc)}
sig := newSignature(p, nil, nil, res)
fct := Func{
pkg: p,
sig: sig,
typ: nil,
name: o.Name(),
id: "get_" + id,
doc: doc,
ret: o.Type(),
err: false,
}
return Const{
pkg: p,
obj: o,
id: id,
doc: doc,
f: fct,
}
}
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() }

View File

@ -220,3 +220,13 @@ var typedescr = map[types.BasicKind]typedesc{
pysig: "object",
},
}
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)
}