From 04e8143491e376bb2dc26a978487053cb3563373 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Mon, 3 Aug 2015 15:12:35 +0200 Subject: [PATCH] bind: first impl of a symbol table Change-Id: I01a4811170436d0c3129ce9cf512790585bab979 --- bind/package.go | 7 +++++ bind/symtab.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 bind/symtab.go diff --git a/bind/package.go b/bind/package.go index 803801f..cae0c28 100644 --- a/bind/package.go +++ b/bind/package.go @@ -18,6 +18,7 @@ type Package struct { pkg *types.Package doc *doc.Package + syms symtab objs map[string]Object consts []Const vars []Var @@ -30,6 +31,7 @@ func NewPackage(pkg *types.Package, doc *doc.Package) (*Package, error) { p := &Package{ pkg: pkg, doc: doc, + syms: newSymtab(), objs: map[string]Object{}, } err := p.process() @@ -161,6 +163,8 @@ func (p *Package) process() error { continue } + p.syms.addSymbol(obj) + switch obj := obj.(type) { case *types.Const: p.addConst(obj) @@ -234,6 +238,9 @@ func (p *Package) process() error { p.addFunc(fct) } + for n, sym := range p.syms.syms { + fmt.Printf("--> [%s]: %#v\n", n, sym) + } return err } diff --git a/bind/symtab.go b/bind/symtab.go new file mode 100644 index 0000000..0e24695 --- /dev/null +++ b/bind/symtab.go @@ -0,0 +1,82 @@ +// 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 ( + "golang.org/x/tools/go/types" +) + +// symbol is an exported symbol in a go package +type symbol struct { + obj types.Object + goname string + cgoname string + cpyname string +} + +// symtab is a table of symbols in a go package +type symtab struct { + syms map[string]symbol +} + +func newSymtab() symtab { + return symtab{ + syms: make(map[string]symbol), + } +} + +func (sym *symtab) addSymbol(obj types.Object) { + n := obj.Name() + pkg := obj.Pkg() + id := pkg.Name() + "_" + n + switch obj.(type) { + case *types.Const: + sym.syms[n] = symbol{ + obj: obj, + goname: n, + cgoname: "cgopy_const_" + id, + cpyname: "cgopy_const_" + id, + } + + case *types.Var: + sym.syms[n] = symbol{ + obj: obj, + goname: n, + cgoname: "cgopy_var_" + id, + cpyname: "cgopy_var_" + id, + } + sym.addType(obj, obj.Type()) + + case *types.Func: + sym.syms[n] = symbol{ + obj: obj, + goname: n, + cgoname: "cgopy_func_" + id, + cpyname: "cgopy_func_" + id, + } + + case *types.TypeName: + sym.addType(obj, obj.Type()) + } +} + +func (sym *symtab) addType(obj types.Object, t types.Type) { + n := obj.Name() + pkg := obj.Pkg() + id := pkg.Name() + "_" + n + switch typ := obj.(type) { + case *types.TypeName: + named := typ.Type().(*types.Named) + switch named.Underlying().(type) { + case *types.Struct: + sym.syms[n] = symbol{ + obj: obj, + goname: n, + cgoname: "cgopy_type_" + id, + cpyname: "cgopy_type_" + id, + } + } + } +}